Advertisement
StefiIOE

shapes 2.0

Apr 11th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5. enum Color {RED ,GREEN ,BLUE};
  6. class Shape {
  7. protected:
  8.     Color color;
  9.     char id[5];
  10.     char *description;
  11.     double area;
  12.  
  13.     void copy(const Shape &s) {
  14.         strcpy(this->id,s.id);
  15.         this->description=new char[strlen(s.description)+1];
  16.         strcpy(this->description,s.description);
  17.         this->color=s.color;
  18.         this->area=s.area;
  19.  
  20.     }
  21.  
  22. public:
  23.  
  24.     Shape(char *id="" , char *description="" ,Color color=RED, double area=0.0) {
  25.         strcpy(this->id,id);
  26.         this->description=new char[strlen(description)+1];
  27.         strcpy(this->description,description);
  28.         this->color=color;
  29.         this->area=area;
  30.     }
  31.     Shape (const Shape &s) {
  32.         copy(s);
  33.  
  34.     }
  35.     Shape &operator =(const Shape &s) {
  36.         if(this!=&s) {
  37.             delete [] this->description;
  38.             copy(s);
  39.         }
  40.         return *this;
  41.     }
  42.  
  43.     char *printColor () {
  44.         if (color == RED) {
  45.             return "RED";
  46.         }
  47.  
  48.         else if (color == GREEN) {
  49.             return "GREEN";
  50.         }
  51.  
  52.         else {
  53.             return "BLUE";
  54.         }
  55.     }
  56.  
  57.     void draw() {
  58.         cout<<id<<" "<<printColor()<<" "<<area<<" ["<<description<<"]"<<endl;
  59.         // cout<<id<<" "<<printColor()<<" "<<area<<endl;
  60.     }
  61.     double getArea() {
  62.         return this->area;
  63.     }
  64.     Color getColor() {
  65.         return this->color;
  66.     }
  67.     char *getID() {
  68.         return this->id;
  69.     }
  70.     void setID(char *id) {
  71.         strcpy(this->id,id);
  72.     }
  73.     void setArea(double area) {
  74.         this->area=area;
  75.     }
  76.  
  77.     void reduceArea() {
  78.         area/=2;
  79.     }
  80.  
  81.  
  82.     ~Shape() {
  83.         delete [] description;
  84.     }
  85.     friend class Canvas;
  86. };
  87. class Canvas {
  88. protected:
  89.     Shape *shapes;
  90.     int n;
  91. public:
  92.     Canvas() {
  93.         n=0;
  94.         shapes=new Shape[0];
  95.     }
  96.     Canvas (const Canvas &c) {
  97.         this->n=c.n;
  98.         this->shapes=new Shape[n];
  99.         for(int i=0; i<n; i++) {
  100.             shapes[i]=c.shapes[i];
  101.         }
  102.     }
  103.  
  104.     Canvas &operator = (const Canvas &c) {
  105.         if(this!=&c)
  106.             delete[]shapes;
  107.         {
  108.             this->n=c.n;
  109.             this->shapes=new Shape[n];
  110.             for(int i=0; i<n; i++) {
  111.                 shapes[i]=c.shapes[i];
  112.             }
  113.         }
  114.         return *this;
  115.     }
  116.  
  117.  
  118.     int getN() {
  119.         return this->n;
  120.     }
  121.     void setN(int n) {
  122.         this->n=n;
  123.     }
  124.  
  125.     void addShape(const Shape s) {
  126.         Shape *tmp = new Shape[n+1];
  127.         for(int i = 0 ; i < n ; i++) {
  128.             tmp[i]=shapes[i];
  129.  
  130.         }
  131.         tmp[n]=s;
  132.         n++;
  133.         delete [] shapes;
  134.         shapes=tmp;
  135.     }
  136.  
  137.  void removeShapesByColor (Color color) {
  138.         int newCount = 0;
  139.         for (int i = 0; i < n; i ++) {
  140.             if (((!shapes[i].getColor())==color)) {
  141.                 newCount++;
  142.             }
  143.         }
  144.         Shape* tmp = new Shape[newCount++];
  145.         newCount = 0;
  146.         for (int i = 0; i < n; i++) {
  147.             if (((!shapes[i].getColor())==color)) {
  148.                 tmp[newCount++] = shapes[i];
  149.             }
  150.         }
  151.         delete [] shapes;
  152.         shapes = tmp;
  153.         n = newCount ;
  154.     }    void removeShapeById (char * id)
  155.  
  156.     {
  157.  
  158.         bool flag = false;
  159.         for (int i = 0 ; i < n ; i ++) {
  160.             if(strcmp(shapes[i].getID(),id)==0) {
  161.                 flag = true;
  162.             }
  163.         }
  164.  
  165.         if (flag) {
  166.  
  167.             Shape * tmp = new Shape[n-1];
  168.  
  169.             int j = 0;
  170.             for (int i = 0 ; i < n ; i ++) {
  171.                 if (strcmp(shapes[i].getID(),id)!=0) {
  172.                     tmp[j] = shapes[i];
  173.                     ++j;
  174.                 }
  175.             }
  176.             delete [] shapes;
  177.             shapes = tmp;
  178.             --n;
  179.         }
  180.     }
  181.  
  182.  
  183.  
  184.  
  185. double totalArea() {
  186.     double count=0.0;
  187.     for(int i = 0 ; i < n ; i ++) {
  188.         count+=shapes[i].getArea();
  189.     }
  190.     return count;
  191. }
  192.  
  193.  
  194.  Canvas & expandAll (double percent) {  
  195.  
  196.         for(int i = 0 ; i < n ; i ++) {
  197.             shapes[i].setArea(shapes[i].getArea()*1.1);
  198.  
  199.         }
  200.         return *this;
  201.  
  202.     }
  203.  
  204.  
  205.  
  206. Canvas getHalfCanvas() {
  207.  
  208.     Canvas c = Canvas(*this);
  209.     for (int i=0; i<c.n; i++) {
  210.         c.shapes[i].reduceArea();
  211.     }
  212.     return c;
  213. }
  214.  
  215.  
  216. void drawAll() {
  217.     for(int i = 0 ; i < n ; i ++) {
  218.         shapes[i].draw();
  219.     }
  220. }
  221. void drawOnly(Color color) {
  222.     for(int i = 0 ; i < n ; i ++) {
  223.         if(shapes[i].getColor()==color) {
  224.             shapes[i].draw();
  225.         }
  226.     }
  227. }
  228.  
  229.  
  230.  
  231.  
  232. };
  233. int main () {
  234.  
  235.     int n;
  236.     cin>>n;
  237.     cout<<"ADITION OF SHAPES IN THE CANVAS"<<endl;
  238.     Canvas canvas;
  239.  
  240.     for (int i=0; i<n; i++) {
  241.         char id [5];
  242.         int color;
  243.         double area;
  244.         char description [100];
  245.  
  246.         cin>>id>>description>>color>>area;
  247.         Shape s (id, description, (Color) color, area);
  248.         canvas.addShape(s);
  249.     }
  250.  
  251.     cout<<"TESTING = FOR CLASS CANVAS"<<endl;
  252.     Canvas c;
  253.     c = canvas;
  254.  
  255.     cout<<"TESTING DRAW ALL"<<endl;
  256.  
  257.     c.drawAll();
  258.  
  259.     cout<<"TESTING DRAW RED SHAPES"<<endl;
  260.  
  261.     c.drawOnly(RED);
  262.  
  263.     cout<<"TESTING TOTAL AREA"<<endl;
  264.  
  265.     cout<<c.totalArea()<<endl;
  266.  
  267.     cout<<"TESTING HALF CANVAS"<<endl;
  268.     Canvas reducedCanvas = c.getHalfCanvas();
  269.     cout<<c.totalArea()<<" "<<reducedCanvas.totalArea()<<endl;
  270.  
  271.     if (reducedCanvas.totalArea() == c.totalArea()) {
  272.         cout<<"DO NOT CHANGE THE AREA OF THE OBJECT IN THE CURRENT CANVAS. JUST IN THE NEW CANVAS"<<endl;
  273.     }
  274.  
  275.     cout<<"TESTING EXPAND ALL"<<endl;
  276.     c.expandAll(0.10);
  277.     cout<<c.totalArea()<<endl;
  278.  
  279.     cout<<"TESTING REMOVE BY ID"<<endl;
  280.     c.removeShapeById("A1");
  281.     c.removeShapeById("Z11");
  282.  
  283.     c.drawAll();
  284.  
  285.     cout<<"TESTING REMOVE BY COLOR"<<endl;
  286.     c.removeShapesByColor(RED);
  287.  
  288.     c.drawAll();
  289.  
  290.     return 0;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement