StefiIOE

Shapes

Apr 3rd, 2020
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  enum Color {RED, GREEN, BLUE};
  5. class Shape
  6.  {
  7.  private:
  8.  char id[5];
  9.  double area;
  10.  Color color;
  11.  public:
  12.  // Shape(){}
  13.  Shape(char *id = "", Color color = RED, double area = 0.0)
  14. { this->area=area;
  15. strcpy(this->id,id);
  16.  this->color=color;
  17.  }
  18.  Shape(const Shape &s)
  19.  {
  20.  this->area=s.area;
  21. strcpy(this->id,s.id);
  22.  this->color=s.color;
  23. }
  24. double getArea() { return this->area; }
  25. char *getId() { return this->id; }
  26.  void setArea(double area) { this->area=area; }
  27.  void setId(char *id) { strcpy(this->id,id); }
  28. char * printColor ()
  29.  {
  30. if (color == RED) return "RED";
  31. else if (color == GREEN) return "GREEN";
  32.  else return "BLUE";
  33. }
  34.  void draw() {
  35.  cout<<id<<" "<<printColor()<<" "<<area<<endl;
  36. }
  37.  void reduceArea ()
  38. { area/=2; }
  39. friend class Canvas;
  40.  };
  41.  class Canvas
  42. {
  43.  private: Shape shape [100];
  44. int n;
  45.  public: Canvas()
  46. {
  47. this->n=0;
  48.  }
  49.  Canvas(const Canvas &c)
  50.  {
  51.  this->n=c.n;
  52. for(int i = 0; i < n ; i++)
  53.  {
  54.  shape[i]=c.shape[i];
  55. }
  56. }
  57.  void setN(int n) { this->n=n; }
  58.  int getN() { return n; }
  59. void addShape (Shape s)
  60. {
  61. shape[n] = s;
  62.  n++;
  63.  }
  64. void drawAll()
  65. {
  66. for(int i = 0; i < n ; i++)
  67.  { shape[i].draw();
  68. }
  69. }
  70. void drawOnly(Color c)
  71.  {
  72.  for(int i = 0; i < n ; i++)
  73.  {
  74. if (shape[i].color == c)
  75.  shape[i].draw();
  76. }
  77. }
  78.  double totalArea()
  79.  {
  80. double sum = 0.0;
  81.  for(int i = 0; i < n ; i++)
  82.  {
  83. sum += shape[i].area;
  84. }
  85. return sum;
  86. }
  87. Canvas getHalfCanvas()
  88.  {
  89. Canvas copy (*this);
  90.  for (int i=0;i<copy.n;i++)
  91. {
  92. copy.shape[i].reduceArea();
  93.  }
  94.  return copy;
  95.  }
  96.  };
  97. int main ()
  98. {
  99.  int n;
  100.  cin>>n;
  101. //cout<<n;
  102.  cout<<"ADITION OF SHAPES IN THE CANVAS"<<endl;
  103.  Canvas canvas;
  104.  for (int i=0; i<n; i++)
  105.  {
  106. char id [5];
  107. int color;
  108.  double area;
  109.  cin>>id>>color>>area;
  110.  //cout<<id<<color<<area<<n<<endl;
  111.  Shape s (id, (Color) color, area);
  112. //s.draw();
  113.  cout<<endl;
  114.  canvas.addShape(s);
  115.  //s.draw();
  116.  }
  117.  cout<<"TESTING DRAW ALL"<<endl;
  118. canvas.drawAll();
  119.  cout<<"TESTING DRAW RED SHAPES"<<endl;
  120.  canvas.drawOnly(RED);
  121.  cout<<"TESTING TOTAL AREA"<<endl;
  122.  cout<<canvas.totalArea()<<endl;
  123. cout<<"TESTING HALF CANVAS"<<endl;
  124. Canvas reducedCanvas = canvas.getHalfCanvas();
  125.  cout<<canvas.totalArea()<<" "<<reducedCanvas.totalArea()<<endl;
  126.  if (reducedCanvas.totalArea() == canvas.totalArea())
  127.  {
  128.  cout<<"DO NOT CHANGE THE AREA OF THE OBJECT IN THE CURRENT CANVAS. JUST IN THE NEW CANVAS"<<endl;
  129. }
  130.  return 0;
RAW Paste Data