-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (124 loc) · 3.62 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (124 loc) · 3.62 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
#include "shape.h"
#include "triangle.h"
#include "isosceles.h"
#include "ellipse.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
using std::vector;
size_t Menu();
void DeleteShapes(vector<Shape*>& shapes);
int main() {
vector<Shape*> shapes;
int menuOption;
while ( (menuOption = Menu()) != 11){
if (menuOption == 1){ // Ellipse
double a, b;
cout << "Input major semi-axis length: ";
cin >> a;
cout << "Input minor semi-axis length: ";
cin >> b;
shapes.push_back(new Ellipse(a, b));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 2){ // Circle
double radius;
cout << "Input radius length: ";
cin >> radius;
shapes.push_back(new Circle(radius));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 3){ // Triangle
double a, b, c;
cout << "Input length of three sides: ";
cin >> a >> b >> c;
shapes.push_back(new Triangle(a, b, c));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 4){ // Isosceles
double equalSidesLength, otherSideLength;
cout << "Input equal side length: ";
cin >> equalSidesLength;
cout << "Input other side length: ";
cin >> otherSideLength;
shapes.push_back(new Isosceles(equalSidesLength, otherSideLength));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 5){ // Rectangle
double base, height;
cout << "Input base length: ";
cin >> base;
cout << "Input height length: ";
cin >> height;
shapes.push_back(new Rectangle(base, height));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 6){ // Square
double length;
cout << "Input side length: ";
cin >> length;
shapes.push_back(new Square(length));
cout << "Inserted... " << endl << endl;
}else if (menuOption == 7){ // List all Ellipses
cout << "Only Ellipses" << endl;
for (Shape* shape: shapes){
if (dynamic_cast<Ellipse*>(shape) != nullptr) {
Ellipse* ellipse = dynamic_cast<Ellipse*>(shape);
cout << shape->ToString() << endl;
cout << "\tPerimeter: " << shape->Perimeter() << endl;
cout << "\tArea: " << shape->Area() << endl;
cout << "\tEccentricity: " << ellipse->Eccentricity() << endl;
cout << endl;
}
}
}else if (menuOption == 8){ // List all Triangles
}else if (menuOption == 9){ // List all Rectangles
}else if (menuOption == 10){ // List all shapes
for (Shape* shape: shapes){
cout << shape->ToString() << endl;
cout << "\tPerimeter: " << shape->Perimeter() << endl;
cout << "\tArea: " << shape->Area() << endl;
cout << endl;
}
}else{
cerr << "This should never happen!" << endl;
break;
}
}
DeleteShapes(shapes);
return 0;
}
size_t Menu(){
int option = 0;
while(true){
cout << "1. Add an Ellipse" << endl;
cout << "2. Add a Circle" << endl;
cout << "3. Add a Triangle" << endl;
cout << "4. Add an Isosceles Triangle" << endl;
cout << "5. Add a Rectangle" << endl;
cout << "6. Add a Square" << endl;
cout << "7. List all Ellipses" << endl;
cout << "8. List all Triangles" << endl;
cout << "9. List all Rectangles" << endl;
cout << "10. List all Shapes" << endl;
cout << "11. Exit" << endl;
cin >> option;
if (cin.fail()){
cin.clear();
cin.ignore(255, '\n');
cerr << "Incorrect input!" << endl;
continue;
}
if (option < 1 || option > 11){
cerr << "Incorrect menu option!" << endl;
continue;
}
break;
}
return static_cast<size_t>(option);
}
void DeleteShapes(vector<Shape*>& shapes){
for (Shape* shape : shapes)
delete shape;
}