StefiIOE

shapes 4.0

May 14th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.70 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. {
  8.     protected:
  9.     Color color;
  10.     char id[5];
  11.     char *description;
  12.     void copy(const Shape &s)
  13.     {
  14.     this->color=s.color;
  15.     strcpy(this->id,s.id);
  16.     this->description= new char[strlen(s.description)+1];
  17.     strcpy(this->description,s.description);
  18.     }
  19.     public:
  20.     Shape( char *id="", char *description="",Color color =RED )
  21.     {
  22.     this->color=color;
  23.     strcpy(this->id,id);
  24.     this->description= new char[strlen(description)+1];
  25.     strcpy(this->description,description);
  26.     }
  27.     virtual double area()=0;
  28.     virtual double perimeter()=0;
  29.     virtual void draw()=0;
  30.     virtual char *getType()=0;
  31.     ~Shape(){delete [] description;}        
  32.     //method void draw() that will print information for the shape in the format
  33.     //[ID color description type: (circle/square/rectangle) perimeter area]
  34.  
  35.      virtual char *printColor() const
  36.     {
  37.         if(color==RED){return "RED";}
  38.         else if(color==GREEN){return "GREEN";}
  39.         else return "BLUE";
  40.     }
  41. };
  42.  
  43. bool operator > (Shape & left, Shape & right) {
  44.    
  45.     return left.perimeter() > right.perimeter();
  46. }
  47. class Circle : public Shape
  48. {
  49.     private:
  50.     double radius;
  51.     public:
  52.     Circle(char *id="", char *description="",Color color =RED, double radius=0.0)
  53.         : Shape( id, description, color)
  54.         {
  55.         this->radius=radius;
  56.         }
  57.     Circle(const Circle &c):Shape(c)
  58.     {
  59.     this->radius=c.radius;
  60.     }
  61.     double area()
  62.     {
  63.     return 3.14 * radius*radius;
  64.     }
  65.     double perimeter()
  66.     {
  67.     return 2*radius *3.14;
  68.     }
  69.     char *getType(){
  70.     return "circle";
  71.     }
  72.     void draw()
  73.     {
  74.         cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
  75.     }
  76. };
  77.  
  78. class Square : public Shape
  79. {
  80.     private:
  81.     double a ;
  82.     public:
  83.     Square(char *id="", char *description="",Color color =RED, double a=0.0)
  84.         : Shape( id, description, color)
  85.         {
  86.         this->a=a;
  87.         }
  88.     Square(const Square &s):Shape(s)
  89.     {
  90.     this->a=s.a;
  91.     }
  92.     double area()
  93.     {
  94.     return a*a;
  95.     }
  96.     double perimeter()
  97.     {
  98.     return 4*a;
  99.     }
  100.     void draw()
  101.     {
  102.         cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
  103.     }
  104.     char*getType()
  105.     {
  106.     return "square";
  107.     }
  108. };
  109. class Rectangle : public Shape
  110. {
  111.     private:
  112.     double a ,b;
  113.     public:
  114.     Rectangle(char *id="", char *description="",Color color =RED, double a=0.0, double b = 0.0)
  115.         : Shape( id, description, color)
  116.         {
  117.         this->a=a;
  118.         this->b=b;
  119.         }
  120.     Rectangle(const Rectangle &r):Shape(r)
  121.     {
  122.     this->a=r.a;
  123.     this->b=r.b;
  124.     }
  125.     double area()
  126.     {
  127.     return a*b;
  128.     }
  129.     double perimeter()
  130.     {
  131.     return 2*(a+b);
  132.     }
  133.     void draw()
  134.     {
  135.     cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
  136.     }
  137.     char*getType()
  138.     {
  139.     return "rectangle";
  140.     }
  141.    
  142. };
  143. double totalArea(Shape**shape , int n )
  144. {
  145.     double sum=0.0;
  146.     for(int i = 0 ; i< n ; i++)
  147.     {
  148.     sum+=shape[i]->area();
  149.     }
  150.     return sum;
  151. }
  152. Shape *maxPerimeter(Shape **shape, int n )
  153. {
  154.     Shape*maxPerimeter=shape[0];
  155.     for(int i = 0 ; i < n ; i++)
  156.     {
  157.         if(*shape[i]>*maxPerimeter){
  158.         maxPerimeter=shape[i];
  159.         }
  160.     }
  161.     return maxPerimeter;
  162. }
  163.  
  164. void drawAllFromType (Shape **shape , int n, char *str)
  165. {
  166.     int count = 0 , count1= 0, count2= 0;
  167.  
  168.    
  169.     for(int i = 0 ; i < n ; i ++){
  170.    Circle*c=dynamic_cast<Circle*>(shape[i]);
  171.        
  172.         if(c!=0 && strcmp(str,"circle")==0) {
  173.             shape[i]->draw();
  174.             count++;
  175.         }  
  176.         Square*s=dynamic_cast<Square*>(shape[i]);
  177.             if(s!=0 && strcmp(str,"square")==0)
  178.             {                
  179.                 shape[i]->draw();
  180.                 count1++;
  181.             }
  182.            
  183.        
  184.         Rectangle *r=dynamic_cast<Rectangle*>(shape[i]);
  185.             if(r!=0 && strcmp(str,"rectangle")==0)
  186.             {
  187.                 shape[i]->draw();
  188.                 count2++;
  189.             }
  190.        
  191.  
  192. }
  193.         if(count==0 && strcmp(str,"circle")==0)
  194.         {
  195.         cout<<"There are no shapes from type circle"<<endl;
  196.         }
  197.         else if(count1==0 && strcmp(str,"square")==0)
  198.         {
  199.         cout<<"There are no shapes from type square"<<endl;
  200.         }
  201.         else if(count2==0 && strcmp(str,"rectangle")==0)
  202.         cout<<"There are no shapes from type rectangle"<<endl;
  203. }
  204.  
  205. int main () {
  206.  
  207.     int n;
  208.     cin>>n;
  209.     //cout<<n;
  210.     Shape ** shapes = new Shape * [n];
  211.  
  212.     int type;
  213.     char id [5];
  214.     char description[20];
  215.     int color;
  216.     double radius, a, b;
  217.     for (int i=0;i<n;i++) {
  218.         cin>>type>>id>>description>>color;
  219.         //cout<<i;
  220.         switch(type) {
  221.             case 0: //circle
  222.             cin>>radius;
  223.             shapes[i] = new Circle (id, description, (Color) color, radius);
  224.             break;
  225.             case 1: //square
  226.             cin>>a;
  227.             shapes[i] = new Square (id, description, (Color) color, a);
  228.             break;
  229.             case 2: //rectangle
  230.             cin>>a>>b;
  231.             shapes[i] = new Rectangle (id, description, (Color) color, a, b);
  232.             break;
  233.             default:
  234.             break;
  235.  
  236.         }
  237.     }
  238.  
  239.  
  240.     cout<<"Total area: "<<totalArea(shapes,n)<<endl;
  241.     cout<<"Max perimeter of a shape is: "<<(maxPerimeter(shapes,n))->perimeter()<<endl;
  242.  
  243.     cout<<"Draw only circles"<<endl;
  244.     drawAllFromType(shapes,n,"circle");
  245.     cout<<"Draw only squares"<<endl;
  246.     drawAllFromType(shapes,n,"square");
  247.  
  248.     cout<<"Draw only rectangle"<<endl;
  249.     drawAllFromType(shapes,n,"rectangle");
  250.  
  251.     return 0;
  252.  
  253. }
Add Comment
Please, Sign In to add comment