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 {
- protected:
- Color color;
- char id[5];
- char *description;
- double area;
- void copy(const Shape &s) {
- strcpy(this->id,s.id);
- this->description=new char[strlen(s.description)+1];
- strcpy(this->description,s.description);
- this->color=s.color;
- this->area=s.area;
- }
- 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 [] this->description;
- copy(s);
- }
- return *this;
- }
- char *printColor () {
- if (color == RED) {
- return "RED";
- }
- else if (color == GREEN) {
- return "GREEN";
- }
- else {
- return "BLUE";
- }
- }
- void draw() {
- cout<<id<<" "<<printColor()<<" "<<area<<" ["<<description<<"]"<<endl;
- // cout<<id<<" "<<printColor()<<" "<<area<<endl;
- }
- double getArea() {
- return this->area;
- }
- Color getColor() {
- return this->color;
- }
- char *getID() {
- return this->id;
- }
- void setID(char *id) {
- strcpy(this->id,id);
- }
- void setArea(double area) {
- this->area=area;
- }
- void reduceArea() {
- area/=2;
- }
- ~Shape() {
- delete [] description;
- }
- friend class Canvas;
- };
- class Canvas {
- protected:
- Shape *shapes;
- int n;
- public:
- Canvas() {
- n=0;
- shapes=new Shape[0];
- }
- Canvas (const Canvas &c) {
- this->n=c.n;
- this->shapes=new Shape[n];
- for(int i=0; i<n; i++) {
- shapes[i]=c.shapes[i];
- }
- }
- Canvas &operator = (const Canvas &c) {
- if(this!=&c)
- delete[]shapes;
- {
- this->n=c.n;
- this->shapes=new Shape[n];
- for(int i=0; i<n; i++) {
- shapes[i]=c.shapes[i];
- }
- }
- return *this;
- }
- int getN() {
- return this->n;
- }
- void setN(int n) {
- this->n=n;
- }
- void addShape(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;
- }
- void removeShapesByColor (Color color) {
- int newCount = 0;
- for (int i = 0; i < n; i ++) {
- if (((!shapes[i].getColor())==color)) {
- newCount++;
- }
- }
- Shape* tmp = new Shape[newCount++];
- newCount = 0;
- for (int i = 0; i < n; i++) {
- if (((!shapes[i].getColor())==color)) {
- tmp[newCount++] = shapes[i];
- }
- }
- delete [] shapes;
- shapes = tmp;
- n = newCount ;
- } void removeShapeById (char * id)
- {
- bool flag = false;
- for (int i = 0 ; i < n ; i ++) {
- if(strcmp(shapes[i].getID(),id)==0) {
- flag = true;
- }
- }
- if (flag) {
- Shape * tmp = new Shape[n-1];
- int j = 0;
- for (int i = 0 ; i < n ; i ++) {
- if (strcmp(shapes[i].getID(),id)!=0) {
- tmp[j] = shapes[i];
- ++j;
- }
- }
- delete [] shapes;
- shapes = tmp;
- --n;
- }
- }
- double totalArea() {
- double count=0.0;
- for(int i = 0 ; i < n ; i ++) {
- count+=shapes[i].getArea();
- }
- return count;
- }
- Canvas & expandAll (double percent) {
- for(int i = 0 ; i < n ; i ++) {
- shapes[i].setArea(shapes[i].getArea()*1.1);
- }
- return *this;
- }
- Canvas getHalfCanvas() {
- Canvas c = Canvas(*this);
- for (int i=0; i<c.n; i++) {
- c.shapes[i].reduceArea();
- }
- return c;
- }
- void drawAll() {
- for(int i = 0 ; i < n ; i ++) {
- shapes[i].draw();
- }
- }
- void drawOnly(Color color) {
- for(int i = 0 ; i < n ; i ++) {
- if(shapes[i].getColor()==color) {
- shapes[i].draw();
- }
- }
- }
- };
- int main () {
- int n;
- cin>>n;
- cout<<"ADITION OF SHAPES IN THE CANVAS"<<endl;
- Canvas canvas;
- for (int i=0; i<n; i++) {
- char id [5];
- int color;
- double area;
- char description [100];
- cin>>id>>description>>color>>area;
- Shape s (id, description, (Color) color, area);
- canvas.addShape(s);
- }
- cout<<"TESTING = FOR CLASS CANVAS"<<endl;
- Canvas c;
- c = canvas;
- cout<<"TESTING DRAW ALL"<<endl;
- c.drawAll();
- cout<<"TESTING DRAW RED SHAPES"<<endl;
- c.drawOnly(RED);
- 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 REMOVE BY ID"<<endl;
- c.removeShapeById("A1");
- c.removeShapeById("Z11");
- c.drawAll();
- cout<<"TESTING REMOVE BY COLOR"<<endl;
- c.removeShapesByColor(RED);
- c.drawAll();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement