Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include "math.h"
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // task:
  9. // 1) implement triangle struct
  10. // 2) implement methon double area() and use it to find shape with biggest area
  11.  
  12. struct Shape {
  13. virtual void print() const {
  14. cout << "unknown shape" << endl;
  15. }
  16. virtual double area() {
  17. cout << "Error" << endl;
  18. return 0;
  19. }
  20. };
  21.  
  22. struct Rectangle : Shape {
  23. int left_x;
  24. int right_x;
  25.  
  26. int lower_y;
  27. int upper_y;
  28.  
  29. Rectangle(const int x1, const int y1, const int x2, const int y2) {
  30. left_x = min(x1, x2);
  31. right_x = max(x1, x2);
  32.  
  33. lower_y = min(y1, y2);
  34. upper_y = max(y1, y2);
  35. }
  36.  
  37. virtual void print() const {
  38. cout << "Rectangle:" << endl;
  39. cout << "\tLeft upper corner x = " << left_x << " y = " << upper_y << endl;
  40. cout << "\tRight lower corner x = " << right_x << " y = " << lower_y << endl;
  41. cout << endl;
  42. }
  43.  
  44. virtual double area() {
  45. double a = right_x - left_x;
  46. double b = upper_y - lower_y;
  47. return a*b;
  48. }
  49. };
  50.  
  51. struct Circle : Shape {
  52. int center_x;
  53. int center_y;
  54. int radius;
  55.  
  56. Circle(const int x, const int y, const int r) {
  57. center_x = x;
  58. center_y = y;
  59. radius = r;
  60. }
  61.  
  62. virtual void print() const {
  63. cout << "Circle:" << endl;
  64. cout << "\tCenter x = " << center_x << " y = " << center_y << endl;
  65. cout << "\tRadius = " << radius << endl;
  66. cout << endl;
  67. }
  68.  
  69. virtual double area() {
  70. double s = radius*radius*3.1415926535897;
  71. return s;
  72. }
  73. };
  74.  
  75. struct triangle : Shape {
  76. int x_1;
  77. int x_2;
  78. int x_3;
  79. int y_1;
  80. int y_2;
  81. int y_3;
  82.  
  83. triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
  84. x_1 = x1;
  85. x_2 = x2;
  86. x_3 = x3;
  87. y_1 = y1;
  88. y_2 = y2;
  89. y_3 = y3;
  90. }
  91. virtual void print() const {
  92. cout << "Triangle" << endl;
  93. cout << "\tfirst point x = " << x_1 << " y = " << y_1 << endl;
  94. cout << "\tsecond point x = " << x_2 << " y = " << y_2 << endl;
  95. cout << "\tthird point x = " << x_3 << " y = " << y_3 << endl;
  96. }
  97.  
  98. virtual double area() {
  99. return abs(x_1*(y_2 - y_3) + x_2*(y_3 - y_1) + x_3*(y_1 - y_3)) / 2;
  100. }
  101. };
  102.  
  103. int main() {
  104. vector<Shape *> shapes;
  105. int choice;
  106. int x1, x2, x3, y1, y2, y3;
  107.  
  108. do {
  109. cout << "What do you want to create?" << endl;
  110. cout << "1) Rectangle" << endl;
  111. cout << "2) Circle" << endl;
  112. cout << "3) Nothing" << endl;
  113. cout << "4) Triangle" << endl;
  114.  
  115. cin >> choice;
  116.  
  117. switch (choice) {
  118. case 1:
  119. cout << "enter x1" << endl;
  120. cin >> x1;
  121. cout << "enter y1" << endl;
  122. cin >> y1;
  123.  
  124. cout << "enter x2" << endl;
  125. cin >> x2;
  126. cout << "enter y2" << endl;
  127. cin >> y2;
  128.  
  129. shapes.push_back(new Rectangle(x1, y1, x2, y2));
  130. break;
  131.  
  132.  
  133. case 2:
  134. int x, y, radius;
  135. cout << "enter x" << endl;
  136. cin >> x;
  137. cout << "enter y" << endl;
  138. cin >> y;
  139. cout << "enter radius" << endl;
  140. cin >> radius;
  141.  
  142. shapes.push_back(new Circle(x, y, radius));
  143. break;
  144.  
  145. case 3:
  146. break;
  147.  
  148. case 4:
  149. cout << "Enter x of the first point\n";
  150. cin >> x1;
  151. cout << "Enter x of the second point\n";
  152. cin >> x2;
  153. cout << "Enter x of the third point\n";
  154. cin >> x3;
  155. cout << "Enter y of the first point\n";
  156. cin >> y1;
  157. cout << "Enter y of the second point\n";
  158. cin >> y2;
  159. cout << "Enter y of the third point\n";
  160. cin >> y3;
  161.  
  162. shapes.push_back(new triangle(x1, y1, x2, y2, x3, y3));
  163.  
  164. break;
  165. default:
  166. cout << "Incorrect choice. You should enter 1, 2 or 3" << endl;
  167. break;
  168. }
  169. } while (choice != 3);
  170.  
  171. bool printRectangles = false;
  172. bool printCircles = false;
  173. bool printTriangles = false;
  174. bool printMaxarea = false;
  175. double max = 0;
  176. do {
  177. cout << "What should we print?" << endl;
  178. cout << "1) Rectangle only" << endl;
  179. cout << "2) Circles only" << endl;
  180. cout << "3) Triangles only" << endl;
  181. cout << "4) everything" << endl;
  182. cout << "5) Maximum area shape" << endl;
  183.  
  184. cin >> choice;
  185. switch (choice) {
  186. case 1:
  187. printRectangles = true;
  188. break;
  189.  
  190. case 2:
  191. printCircles = true;
  192. break;
  193.  
  194. case 3:
  195. printTriangles = true;
  196. break;
  197.  
  198. case 4:
  199. printRectangles = true;
  200. printCircles = true;
  201. printTriangles = true;
  202. printMaxarea = true;
  203. break;
  204.  
  205. case 5:
  206. for (size_t i = 0; i < shapes.size(); ++i) {
  207. if (shapes[i]->area() > max) {
  208. max = shapes[i]->area();
  209. }
  210. }
  211. printMaxarea = true;
  212. break;
  213.  
  214. default:
  215. cout << "Incorrect choice. You should enter 1, 2, 3, 4 or 5" << endl;
  216. break;
  217. }
  218. } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5);
  219.  
  220. for (unsigned int i = 0; i < shapes.size(); ++i) {
  221. if (dynamic_cast<Rectangle *>(shapes[i]) != NULL && printRectangles) {
  222. shapes[i]->print();
  223. }
  224.  
  225. if (dynamic_cast<Circle *>(shapes[i]) != NULL && printCircles) {
  226. shapes[i]->print();
  227. }
  228. if (dynamic_cast<triangle *>(shapes[i]) != NULL && printTriangles) {
  229. shapes[i]->print();
  230. }
  231.  
  232. }
  233. if (printMaxarea) {
  234. cout << max << endl;
  235. }
  236. return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement