Monday, June 3, 2013

Some C++ Problems for Beginners (5)


This Post has some Previous Posts, you can access the first Main Post here: Some C++ Problems for Beginners (1)
The code was written and tested on Visual Studio 2010 –> “Empty Windows Console Application” . if you don’t have Visual Studio, you can get the latest version from :
-Visual C++ 2010 Express:
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express

note that, you can also use another IDEs like:
-Visual Studio Express 2012 for Windows Desktop: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products#product-express-summary
-CodeBlocks: http://sourceforge.net/projects/codeblocks
-GNU C++ compiler GCC(g++): http://gcc.gnu.org/
-MingW GCC port for windows: http://www.mingw.org/
-C++ Builder (Borland): http://www.embarcadero.com/products/cbuilder
-ShapesAreas.cpp:
Problem Definition:
Write a program that take from the user "1" if Circle, "2" if Rectangle, "3" if Triangle,
"4" if Square & "5" if Parallelogram
then Calculate and Print the Area of the chosen Shape after taking the inputs from the
user.
Note:
Area of a:
                Circle = π × r2               (read r from the user and consider that π = 3.14)
                Rectangle = w x h                       (read w and h from the user)
                Triangle = 1/2 base x h         (read base and h from the user)
                Square = L2                             (read L from the user)
                Parallelogram = base * h         (read base and h from the user)

[C++]
  1 /*  About The Simple Code "ShapesAreas.cpp"
  2     Copyright (C) 2013  Eng. Mohamed Adel (http://www.med-adel.com)
  3 
  4     This simple code is a free simple: you can redistribute it and/or modify 
  5     it under the terms of the GNU General Public License as published by 
  6     the Free Software Foundation, either version 3 of the License, or 
  7     (at your option) any later version. 
  8   
  9     This program is distributed in the hope that it will be useful, 
 10     but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12     GNU General Public License for more details. 
 13   
 14     You should have received a copy of the GNU General Public License 
 15     along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
 16 
 17 
 18 #include <iostream>
 19 #define PI 3.14
 20 using namespace std;            
 21 
 22 /*Problem Definition
 23 Write a program that take from the user "1" if Circle, 
 24 "2" if Rectangle, "3" if Triangle, "4" if Square & "5" if Parallelogram
 25 then Calculate and Print the Area of the chosen Shape after taking the inputs from the user.
 26 Note:
 27 Area of a:
 28     Circle = π × r2               (read r from the user and consider that π = 3.14)
 29     Rectangle = w x h    (read w and h from the user)
 30     Triangle = 1/2 base x h  (read base and h from the user)
 31     Square = L2     (read L from the user)
 32     Parallelogram = base * h  (read base and h from the user)
 33 */
 34 
 35 //Prototype(s)
 36 void ReadCalculatePrintArea(int choice);
 37 float CalculateCircleArea(int r);
 38 float CalculateRectangleArea(int w, int h);
 39 float CalculateTriangleArea(int base, int h);
 40 float CalculateSquareArea(int L);
 41 float CalculateParallelogramArea(int base, int h);
 42 
 43 void main()
 44 {
 45  int choice;
 46  cout<<"Please, enter 1 if Circle, 2 if Rectangle, 3 if Triangle, 4 if Square and 5 if Parallelogram : "<<endl;
 47     cin>>choice;             
 48  ReadCalculatePrintArea(choice);         
 49 }
 50 
 51 //ReadCalculatePrintArea is a function that take the Choice from the User then 
 52 //1. Reads the inputs from the user based on the chosen choice
 53 //2. Calls the Specific CalculateArea Function based on the chosen shape
 54 //3. Prints on the screen the Final Area Result taken from the specified Called CalculateArea Function
 55 void ReadCalculatePrintArea(int choice)
 56 {
 57 
 58 float Area;
 59 switch(choice)
 60  {
 61  case 1:                  
 62   int r;                 
 63   cout<<"Please Enter the Radius of the Circle : "<<endl;
 64   cin>>r;                 
 65   Area = CalculateCircleArea(r);           
 66   cout<<"The Area of the specified Circle is : "<<Area<<endl;    
 67   break;
 68 
 69  case 2:                  
 70   int w,h;                
 71   cout<<"Please Enter the Width and the Height of the Rectangle :"<<endl;
 72   cin>>w>>h;                
 73   Area = CalculateRectangleArea(w,h);          
 74   cout<<"The Area of the specified Rectangle is : "<<Area<<endl;   
 75   break;
 76 
 77  case 3:                  
 78   int base,h2;               
 79   cout<<"Please Enter the Base and the Height of the Triangle :"<<endl;
 80   cin>>base>>h2;               
 81   Area = CalculateTriangleArea(base,h2);         
 82   cout<<"The Area of the specified Triangle is : "<<Area<<endl;   
 83   break;
 84 
 85  case 4:                  
 86   int L;                 
 87   cout<<"Please Enter the Lenght of the Square : ";
 88   cin>>L;                 
 89   Area = CalculateSquareArea(L);           
 90   cout<<"The Area of the specified Square is : "<<Area<<endl;    
 91   break;
 92 
 93  case 5:                  
 94   int base2,h3;               
 95   cout<<"Please Enter the Base and the Height of the Parallelogram";
 96   cin>>base2>>h3;               
 97   Area = CalculateParallelogramArea(base2,h3);       
 98   cout<<"The Area of the specified Parallelogram is : "<<Area<<endl;  
 99   break;
100 
101  default:                 
102   cout<<"Invalid Choice";
103   break;
104  }
105 }
106 
107 //CalculateCircleArea is a function that return the Circle Area
108 float CalculateCircleArea(int r)
109 {
110  return PI * r * r;
111 }
112 
113 //CalculateRectangleArea is a function that return the Rectangle Area
114 float CalculateRectangleArea(int w, int h)
115 {
116  return w * h;
117 }
118 
119 //CalculateTriangleArea is a function that return the Triangle Area
120 float CalculateTriangleArea(int base, int h)
121 {
122  return 0.5 * base * h;
123 }
124 
125 //CalculateSquareArea is a function that return the Square Area
126 float CalculateSquareArea(int L)
127 {
128  return L * L;
129 }
130 
131 //CalculateParallelogramArea is a function that return the Parallelogram Area
132 float CalculateParallelogramArea(int base, int h)
133 {
134  return base * h;
135 }

download the ShapesAreas.cpp  (Commented, Explained Code)

Download all the C++ Simples I’ve created : C++ Problems

No comments:

Post a Comment

Search This Blog