-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaCalculator.cpp
159 lines (133 loc) · 4.34 KB
/
AreaCalculator.cpp
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
#include <iostream>
#include <cmath> // for pow() function
using namespace std;
int main()
{
const float PI = 3.14159;
int user_choice;
cout << "\nEnter your choice (1-4): \n"
<< "\n1. Calculate the Area of a Circle\n"
<< "2. Calculate the Area of a Rectangle\n"
<< "3. Calculate the Area of a Triangle\n"
<< "4. Calculate the area of a Trapezoid\n"
<< "5. Quit\n";
cin >> user_choice;
cout << endl;
switch (user_choice)
{
float area;
case 1:
int radius;
cout << "What is the radius: ";
cin >> radius;
if (radius < 0)
{
cout << "\nWe're sorry \nthe radius must be "
<< "a positive number.\n"
<< "Rerun the program and try again.\n"
<< endl;
}
else
{
area = PI * pow (radius, 2);
cout << "The area of the Circle is " << area << endl;
}
break;
case 2:
float width, length;
cout << "What is the length? " << endl;
cin >> length;
if (length > 0)
{
cout << "What is the width? " << endl;
cin >> width;
if (width > 0)
{
area = length * width;
cout << "The area of Rectangle is "<< area << endl;
}
else
{
cout << "\nWe're sorry \nWidth must be greater than 0." << endl;
cout << "Rerun the program and try again."<< endl;
}
}
else
{
cout << "\nWe're sorry \nLength must be greater than 0." << endl;
cout << "Rerun the program and try again." << endl;
}
break;
case 3:
float height,
base;
cout << "What is base base? ";
cin >> base;
if (base > 0)
{
cout << "What is the height? ";
cin >> height;
if (height > 0)
{
area = (base * height) * 1/2;
cout << "The area of Triangle is "
<< area
<< endl;
}
else
{
cout << "\nWe're sorry \nHeight must be greater than 0.\n" << "Rerun the program and"
<< "try again."
<< endl;
}
}
else
{
cout << "\nWe're sorry. \nBase length must"
<< " be greater than 0.\n"
<< "Rerun the program and try again." << endl;
}
break;
case 4:
float verticalHeight, baseA, baseB;
cout << "What is base A? ";
cin >> baseA;
cout << "What is base B? ";
cin >> baseB;
if (baseA > 0 and baseB >0)
{
cout << "What is the vertical height? ";
cin >> verticalHeight;
if (verticalHeight > 0)
{
area = (baseA + baseB)/2 * verticalHeight;
cout << "The area of Trapezoid is "
<< area << endl;
}
else
{
cout << "\nWe're sorry \nVertical height must be greater than 0.\n" << "Rerun the program and"
<< "try again."
<< endl;
}
}
else
{
cout << "\nWe're sorry \nBase length must"
<< " be greater than 0.\n"
<< "Rerun the program and try again." << endl;
}
break;
case 5:
cout << "Good-bye prof. Xu" << endl;
break;
default:
cout << "We're sorry \nYour choice must "
<< "between 1 and 4.\n"
<< "Rerun the program and try again."
<< endl;
break;
}
cout << endl;
return 0;
}