Advertisement
StefiIOE

Shapes 3.0

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