-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.cpp
More file actions
138 lines (114 loc) · 4.42 KB
/
shape.cpp
File metadata and controls
138 lines (114 loc) · 4.42 KB
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
/* Name: Ermithe Tilusca
Date: 1/29/2025
Assignment: #2
Due Date: 2/4/2025
About this project: A program that calculates the area and perimeter of
different geometric shapes. The user can choose between a circle,
rectangle, and triable, and the program will calculate and display the
area and the perimeter of the chosen shape using appropriate formulas and
if/else if/else statements.
Assumptions: Assumes that the user will always enter positive, non-zero
numerial values for all numerical entries. Assumes that the user will
only enter single characters (not words) when prompted for menu choices.
All work below was performed by Ermithe Tilusca */
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//constant variable, PI is always th same value
const double PI = 3.14159;
//char variable for user's shape choice
char shape;
//double variables for circle
double radius;
double Cperimeter;
double Carea;
//variables for rectangle
double Rlength;
int Rwidth;
double Rperimeter;
double Rarea;
//double variables for triangle
double lengthBigSide;
double lengthSmSide1;
double lengthSmSide2;
double semiPerimeter;
double Tperimeter;
double Tarea;
//present menu
cout << "Choose a shape:" << endl;
cout << "C - Circle" << endl;
cout << "T - Triangle" << endl;
cout << "R - Rectangle" << endl;
cout << "Choice: ";
//Users' shape choice
cin >> shape;
cout << endl;
//set one decimal place
cout << fixed << showpoint << setprecision(1);
//if user choice is a circle
if (shape == 'C' )
{
//the circle's radius
cout << "Radius: ";
cin >> radius;
//the circle's perimeter
Cperimeter = 2 * PI * radius;
cout << "PERIMETER: " << Cperimeter << endl;
//the circle's area
Carea = PI * (radius*radius);
cout << "AREA: " << Carea << endl;
}
else if (shape == 'R')
{
//the rectangle's length and width
cout << "Length: ";
cin >> Rlength;
cout << "Width: ";
cin >> Rwidth;
//the rectangle's perimeter
Rperimeter = 2 * (Rlength + Rwidth);
cout << "PERIMETER: " << Rperimeter << endl;
//the rectangle's area
Rarea = Rlength * Rwidth;
cout << "AREA: " << Rarea << endl;
}
else if (shape == 'T')
{
//triangle's side information
cout << "Length of the Biggest side: ";
cin >> lengthBigSide;
cout << "Length of the two smaller sides: ";
cin >> lengthSmSide1 >> lengthSmSide2;
//error, biggest side is not larger than smaller sides
if (lengthBigSide < lengthSmSide1 || lengthBigSide < lengthSmSide2)
{
cout << "Invalid entries, ";
cout <<"make sure you enter the biggest side value first." << endl;
}
//sides entered by user do not form a valid triangle
else if ((lengthSmSide1 + lengthSmSide2)< lengthBigSide)
{
cout << "It is impossible for the side lengths of";
cout << lengthSmSide1 << lengthSmSide2 << "and";
cout << lengthBigSide << "to form the sides of a triangle." << endl;
}
else
{
//no error, print out the triagle's perimeter and area
Tperimeter = lengthSmSide1 + lengthSmSide2 + lengthBigSide;
semiPerimeter = (lengthSmSide1 + lengthSmSide2 + lengthBigSide)/2;
Tarea = sqrt( semiPerimeter * (semiPerimeter - lengthSmSide1) * (semiPerimeter - lengthSmSide2) * (semiPerimeter - lengthBigSide));
cout << "PERIMETER: " << Tperimeter << endl;
cout << "AREA: " << Tarea << endl;
}
}
else
{
//the user choose something that is not C, R, nor T
cout << "Invalid menu choice. Exiting program." << endl;
}
return 0;
}