1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <iomanip>
constexpr auto pi = 3.1415;
using namespace std;
// Function prototypes
char getShapeType();
double getAreaOfRectangle(double, double);
double getAreaOfCircle(double);
double getAreaOfTriangle(double, double);
double getAreaOfSquare(double);
int main()
{
char shapeType; // R=rectangle, T=triangle, C=circle, S= square
double areaOfRectangle; //variable to store the area of rectangle
double areaOfCircle; //variable to store the area of circle
double areaOfTriangle; //variable to store the area of triangle
double areaOfSquare; //variable to store the area of circle
// Get the shape type.
shapeType = getShapeType();
if (shapeType == 'R')
{
double width;
double length;
do
{
cout << "Enter width and length of the rectangle separated by space: ";
cin >> width >> length;
} while (width <= 0 || length <= 0);
areaOfRectangle = getAreaOfRectangle(width, length);
cout << "The area of the rectangle with width "
<< width << " and length " << length
<< " is " << areaOfRectangle << endl;
}
if (shapeType == 'C')
{
double radius;
do
{
cout << "Enter radius of the circle: ";
cin >> radius;
} while (radius <= 0);
areaOfCircle = getAreaOfCircle(radius);
cout << "The area of the circle with radius "
<< radius << 'is' << areaOfCircle << endl;
}
if (shapeType == 'T')
{
double base;
double height;
do
{
cout << "Enter base and height of the triangle separated by space: ";
cin >> base >> height;
} while (base <= 0 || height <= 0);
areaOfTriangle = getAreaOfTriangle(base, height);
cout << "The area of the triangle with base "
<< base << " and height " << height
<< " is " << areaOfTriangle << endl;
}
if (shapeType == 'S')
{
double width;
do
{
cout << "Enter width of the square: ";
cin >> width;
} while (width <= 0);
areaOfSquare = getAreaOfSquare(width);
cout << "The area of the square with width "
<< width << 'is' << areaOfSquare << endl;
}
system("pause");
return 0;
}
//*********************************************************
// The getShapeType function returns 'C' or 'R' or 'S' or 'T' to *
// indicate circle or rectangle or square or triangle. *
//*********************************************************
char getShapeType()
{
char type; // The shape type
// Get the shape type, R, C, T, S
cout << "This program will compute area of a shape.\n"
<< "What is the shape type?\n"
<< "Circle or Rectangle or Square or Triangle? (C or R or S or T): ";
cin >> type;
// Validate the shape type.
while (type != 'C' && type != 'c' &&
type != 'R' && type != 'r' && type != 'S' && type != 's'
&& type != 'T' && type != 't')
{
cout << "Please enter C or R or S or T: ";
cin >> type;
}
// Convert lowercase to uppercase.
if (type == 'c')
type = 'C';
else if (type == 'r')
type = 'R';
else if (type == 's')
type = 'S';
else if (type == 't')
type = 'T';
return type;
}
//*****************************************
//Function to calculate the are of rectangle
// Area = width*length
//******************************************
double getAreaOfRectangle(double width, double length)
{
double area = width * length;
return area;
}
//*****************************************
//Function to calculate the area of circle
// Area = pi * r * r
//******************************************
double getAreaOfCircle(double radius)
{
double area = pi * radius * radius;
return area;
}
//*****************************************
//Function to calculate the area of square
// Area = width*width
//******************************************
double getAreaOfSquare(double width)
{
double area = width * width;
return area;
}
//*****************************************
//Function to calculate the area of triangle
//Area = (base * height)/2
//****************************************
double getAreaOfTriangle(double base, double height)
{
double area = (base * height) / 2;
return area;
}
|