Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include "math.h"
- #include <vector>
- using namespace std;
- // task:
- // 1) implement triangle struct
- // 2) implement methon double area() and use it to find shape with biggest area
- struct Shape {
- virtual void print() const {
- cout << "unknown shape" << endl;
- }
- virtual double area() {
- cout << "Error" << endl;
- return 0;
- }
- };
- struct Rectangle : Shape {
- int left_x;
- int right_x;
- int lower_y;
- int upper_y;
- Rectangle(const int x1, const int y1, const int x2, const int y2) {
- left_x = min(x1, x2);
- right_x = max(x1, x2);
- lower_y = min(y1, y2);
- upper_y = max(y1, y2);
- }
- virtual void print() const {
- cout << "Rectangle:" << endl;
- cout << "\tLeft upper corner x = " << left_x << " y = " << upper_y << endl;
- cout << "\tRight lower corner x = " << right_x << " y = " << lower_y << endl;
- cout << endl;
- }
- virtual double area() {
- double a = right_x - left_x;
- double b = upper_y - lower_y;
- return a*b;
- }
- };
- struct Circle : Shape {
- int center_x;
- int center_y;
- int radius;
- Circle(const int x, const int y, const int r) {
- center_x = x;
- center_y = y;
- radius = r;
- }
- virtual void print() const {
- cout << "Circle:" << endl;
- cout << "\tCenter x = " << center_x << " y = " << center_y << endl;
- cout << "\tRadius = " << radius << endl;
- cout << endl;
- }
- virtual double area() {
- double s = radius*radius*3.1415926535897;
- return s;
- }
- };
- struct triangle : Shape {
- int x_1;
- int x_2;
- int x_3;
- int y_1;
- int y_2;
- int y_3;
- triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
- x_1 = x1;
- x_2 = x2;
- x_3 = x3;
- y_1 = y1;
- y_2 = y2;
- y_3 = y3;
- }
- virtual void print() const {
- cout << "Triangle" << endl;
- cout << "\tfirst point x = " << x_1 << " y = " << y_1 << endl;
- cout << "\tsecond point x = " << x_2 << " y = " << y_2 << endl;
- cout << "\tthird point x = " << x_3 << " y = " << y_3 << endl;
- }
- virtual double area() {
- return abs(x_1*(y_2 - y_3) + x_2*(y_3 - y_1) + x_3*(y_1 - y_3)) / 2;
- }
- };
- int main() {
- vector<Shape *> shapes;
- int choice;
- int x1, x2, x3, y1, y2, y3;
- do {
- cout << "What do you want to create?" << endl;
- cout << "1) Rectangle" << endl;
- cout << "2) Circle" << endl;
- cout << "3) Nothing" << endl;
- cout << "4) Triangle" << endl;
- cin >> choice;
- switch (choice) {
- case 1:
- cout << "enter x1" << endl;
- cin >> x1;
- cout << "enter y1" << endl;
- cin >> y1;
- cout << "enter x2" << endl;
- cin >> x2;
- cout << "enter y2" << endl;
- cin >> y2;
- shapes.push_back(new Rectangle(x1, y1, x2, y2));
- break;
- case 2:
- int x, y, radius;
- cout << "enter x" << endl;
- cin >> x;
- cout << "enter y" << endl;
- cin >> y;
- cout << "enter radius" << endl;
- cin >> radius;
- shapes.push_back(new Circle(x, y, radius));
- break;
- case 3:
- break;
- case 4:
- cout << "Enter x of the first point\n";
- cin >> x1;
- cout << "Enter x of the second point\n";
- cin >> x2;
- cout << "Enter x of the third point\n";
- cin >> x3;
- cout << "Enter y of the first point\n";
- cin >> y1;
- cout << "Enter y of the second point\n";
- cin >> y2;
- cout << "Enter y of the third point\n";
- cin >> y3;
- shapes.push_back(new triangle(x1, y1, x2, y2, x3, y3));
- break;
- default:
- cout << "Incorrect choice. You should enter 1, 2 or 3" << endl;
- break;
- }
- } while (choice != 3);
- bool printRectangles = false;
- bool printCircles = false;
- bool printTriangles = false;
- bool printMaxarea = false;
- double max = 0;
- do {
- cout << "What should we print?" << endl;
- cout << "1) Rectangle only" << endl;
- cout << "2) Circles only" << endl;
- cout << "3) Triangles only" << endl;
- cout << "4) everything" << endl;
- cout << "5) Maximum area shape" << endl;
- cin >> choice;
- switch (choice) {
- case 1:
- printRectangles = true;
- break;
- case 2:
- printCircles = true;
- break;
- case 3:
- printTriangles = true;
- break;
- case 4:
- printRectangles = true;
- printCircles = true;
- printTriangles = true;
- printMaxarea = true;
- break;
- case 5:
- for (size_t i = 0; i < shapes.size(); ++i) {
- if (shapes[i]->area() > max) {
- max = shapes[i]->area();
- }
- }
- printMaxarea = true;
- break;
- default:
- cout << "Incorrect choice. You should enter 1, 2, 3, 4 or 5" << endl;
- break;
- }
- } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5);
- for (unsigned int i = 0; i < shapes.size(); ++i) {
- if (dynamic_cast<Rectangle *>(shapes[i]) != NULL && printRectangles) {
- shapes[i]->print();
- }
- if (dynamic_cast<Circle *>(shapes[i]) != NULL && printCircles) {
- shapes[i]->print();
- }
- if (dynamic_cast<triangle *>(shapes[i]) != NULL && printTriangles) {
- shapes[i]->print();
- }
- }
- if (printMaxarea) {
- cout << max << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement