Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- enum Color{RED , GREEN , BLUE};
- class Shape
- {
- private:
- Color color;
- char id [5];
- char *description;
- double area;
- void copy(const Shape &s)
- {
- this->color=s.color;
- strcpy(this->id,s.id);
- this->area=s.area;
- this->description= new char[strlen(s.description)+1];
- strcpy(this->description,s.description);
- }
- public:
- Shape(char *id=" ", char *description=" " , Color color=RED , double area=0.0)
- {
- strcpy(this->id,id);
- this->description= new char[strlen(description)+1];
- strcpy(this->description,description);
- this->color=color;
- this->area=area;
- }
- Shape(const Shape &s)
- {
- copy(s);
- }
- Shape &operator=(const Shape &s)
- {
- if(this!=&s)
- {
- delete [] description;
- copy(s);
- }
- return *this;
- }
- char *printColor() const
- {
- if(color==RED){return "RED";}
- else if(color==GREEN){return "GREEN";}
- else return "BLUE";
- }
- friend ostream & operator <<(ostream &out , const Shape &s)
- {
- out<<s.id<<" "<<s.printColor()<<" "<<s.area<<" ["<<s.description<<"]"<<endl;
- return out;
- }
- friend istream &operator >> (istream &in , Shape &s)
- {
- in >>s.id>>s.description;
- int color;
- in>>color>>s.area;
- s.color=(Color)color;
- return in ;
- }
- Shape &operator *=(double coefficient)
- {
- this->area*=coefficient;
- return *this;
- }
- bool operator == (char *id)
- {
- return (strcmp(this->id,id)==0);
- }
- bool operator == (Color color)
- {
- return this->color==color;
- }
- bool operator !=(char*id)
- {
- return (strcmp(this->id,id)!=0);
- }
- bool operator !=(Color color){
- return this->color != color;
- }
- bool operator > (const Shape & other)
- {
- return this->area > other.area;
- }
- Shape operator +(Shape shape)
- {
- double sum = this->area+shape.area;
- if(this->area>= shape.area){
- return Shape(this->id, this->description ,this->color, sum);
- }
- else
- return Shape(shape.id , shape.description , shape.color , sum);
- }
- // ~Shape(){delete [] description; }
- double getArea(){return this->area;}
- // friend class Canvas;
- };
- class Canvas{
- protected:
- Shape *shapes;
- int n ;
- void copy(const Canvas &c)
- {
- this->n=c.n;
- shapes=new Shape [this->n];
- for (int i = 0 ; i < n ; i ++)
- {
- shapes[i]=c.shapes[i];
- }
- }
- public:
- Canvas (){
- this->n=0;
- shapes=new Shape[0];
- }
- Canvas(const Canvas &c){copy(c);}
- Canvas &operator =(const Canvas &c)
- {
- if(this!=&c){
- delete[]shapes;
- copy(c);
- }
- return *this;
- }
- Canvas &operator +=(const Shape &s){
- Shape *tmp=new Shape[n+1];
- for(int i = 0 ; i < n ; i ++)
- {
- tmp[i]=shapes[i];
- }
- tmp[n]=s;
- n++;
- delete [] shapes;
- shapes=tmp;
- return *this;
- }
- Canvas &operator-=(char*id){
- bool flag = false;
- for(int i = 0 ; i < n ; i ++){
- if(shapes[i]==id){
- flag = true;
- }
- }
- if(!flag)
- {
- return *this;
- }
- Shape *tmp = new Shape[n-1];
- int s = 0;
- for(int i = 0 ; i < n ; i ++){
- if(shapes[i]!=id){
- tmp[s]=shapes[i];
- s++;
- }
- }
- delete[] shapes;
- shapes=tmp;
- n--;
- return *this;
- }
- Canvas &operator -=(Color color){
- int count = 0 ;
- for(int i = 0 ; i < n ; i ++){
- if(shapes[i]==color){
- count ++;
- }
- }
- if(count==0)
- {
- return *this;
- }
- Shape *tmp= new Shape[n-count];
- int j = 0 ;
- for(int i = 0 ; i < n ; i ++){
- if(shapes[i]!=color){
- tmp[j]=shapes[i];
- j++;
- }
- }
- delete []shapes;
- shapes=tmp;
- n-=count;
- return *this;
- }
- friend ostream &operator <<(ostream &out , Canvas &c){
- for(int i = 0 ; i < c.n ; i ++){
- out<<c.shapes[i];
- }
- return out;
- }
- Shape &operator [](int inx){
- return shapes[inx];
- }
- double totalArea() {
- double s=0.0;
- for (int i=0; i<n; i++) {
- s+=shapes[i].getArea();
- }
- return s;
- }
- Canvas getHalfCanvas() {
- Canvas halfCanvas(*this);
- for(int i=0; i<n; i++) {
- halfCanvas.shapes[i]*=0.5;
- }
- return halfCanvas;
- }
- Canvas &expandAll(double percent) {
- for(int i=0; i<n; i++) {
- shapes[i]*=(1+percent);
- }
- return *this;
- }
- Shape sum (){
- Shape shape;
- for(int i = 0 ; i < n ; i ++){
- shape=shape+shapes[i];
- }
- return shape;
- }
- };
- int main () {
- int n;
- cin>>n;
- Canvas canvas;
- cout<<"TEST FOR >> AND << IN SHAPE CLASS"<<endl;
- Shape s;
- cin>>s;
- cout<<s;
- cout<<"TEST FOR += OF CLASS CANVAS"<<endl;
- canvas+=s;
- for (int i=1;i<n;i++) {
- cin>>s;
- canvas+=s;
- }
- cout<<"TESTING + FOR CLASS SHAPE AND [] FOR CLASS CANVAS"<<endl;
- cout<<(canvas[0]+canvas[3]);
- cout<<"TESTING = FOR CLASS CANVAS"<<endl;
- Canvas c;
- c = canvas;
- cout<<"TESTING << FOR CLASS CANVAS"<<endl;
- cout<<c;
- cout<<"TESTING TOTAL AREA"<<endl;
- cout<<c.totalArea()<<endl;
- cout<<"TESTING HALF CANVAS"<<endl;
- Canvas reducedCanvas = c.getHalfCanvas();
- cout<<c.totalArea()<<" "<<reducedCanvas.totalArea()<<endl;
- if (reducedCanvas.totalArea() == c.totalArea()) {
- cout<<"DO NOT CHANGE THE AREA OF THE OBJECT IN THE CURRENT CANVAS. JUST IN THE NEW CANVAS"<<endl;
- }
- cout<<"TESTING EXPAND ALL"<<endl;
- c.expandAll(0.10);
- cout<<c.totalArea()<<endl;
- cout<<"TESTING OPERATOR -= (REMOVE BY ID)"<<endl;
- c-=("A1");
- c-=("Z11");
- cout<<c;
- cout<<"TESTING OPERATOR -= (REMOVE BY COLOR)"<<endl;
- c-=(RED);
- cout<<c;
- cout<<"TESTING SUM METHOD AND + OPERATOR IN CLASS SHAPE"<<endl;
- cout<<c.sum();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement