include iostream include vector include stdlib include cmath using nam

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <cmath>
using namespace std;
struct Point{
double x,y;
};
class Shape {
public:
vector<Point> arc;
char ID;
/* Поля и методы */
virtual double GetArea(){return 0;}//Вычисление площади фигуры
virtual void CreateShape(){}
virtual void move(){}
virtual void rotate(){}
virtual ~Shape(){}
};
class Rectangle : public Shape {
public:
Rectangle ()
{
ID='R';
arc.resize(4);
}
void CreateShape()
{
cout<<"Input 2 Rectangle coordinate points"<<endl;
cin>>arc[0].x; /*вводим все координаты*/
cin>>arc[0].y;
cin>>arc[3].x;
cin>>arc[3].y;
arc[1].x = arc[3].x;
arc[1].y = arc[0].y;
arc[2].x = arc[0].x;
arc[2].y = arc[3].y;
}
void move(double step, double angle)
{
for(int i=0; i<4; i++)
{
arc[i].x+=step*cos(angle);
arc[i].y+=step*sin(angle);
}
}
void rotate(Point point, double angle)
{
double *ort = new double[3];
double *fi = new double[3];
for(int i=0; i<4; i++)
{
double newX = arc[i].x-point.x;
double newY = arc[i].y-point.y;
ort[i] = sqrt(newX*newX+newY*newY);
if(newX>0 && newY>=0) fi[i] = atan(newY/newX);
if(newX>0 && newY<0) fi[i] = atan(newY/newX)+2*M_PI;
if(newX<0) fi[i] = atan(newY/newX)+M_PI;
if(newX==0 && newY>0) fi[i] = M_PI/2;
if(newX==0 && newY<0) fi[i] = 3*M_PI/2;
if(newX==0 && newY==0) fi[i] = 0;
fi[i]+=angle;
arc[i].x = point.x+ort[i]*cos(fi[i]);
arc[i].y = point.y+ort[i]*sin(fi[i]);
}
}
double GetArea()
{
/*вычисление конкретной площади*/return abs((arc[0].x-arc[3].x)*(arc[0].y-arc[3].y));
}
~Rectangle(){}
};
class Triangle : public Shape {
public:
Triangle ()
{
arc.resize(3);
ID='T';
}
void CreateShape()
{
cout<<"Input Triangle coordinate points"<<endl;
for(int i=0; i<3; i++)
{
cin>>arc[i].x; /*вводим все координаты*/
cin>>arc[i].y;
}
}
void rotate(Point point, double angle)
{
double *ort = new double[3];
double *fi = new double[3];
for(int i=0; i<3; i++)
{
double newX = arc[i].x-point.x;
double newY = arc[i].y-point.y;
ort[i] = sqrt(newX*newX+newY*newY);
if(newX>0 && newY>=0) fi[i] = atan(newY/newX);
if(newX>0 && newY<0) fi[i] = atan(newY/newX)+2*M_PI;
if(newX<0) fi[i] = atan(newY/newX)+M_PI;
if(newX==0 && newY>0) fi[i] = M_PI/2;
if(newX==0 && newY<0) fi[i] = 3*M_PI/2;
if(newX==0 && newY==0) fi[i] = 0;
fi[i]+=angle;
arc[i].x = point.x+ort[i]*cos(fi[i]);
arc[i].y = point.y+ort[i]*sin(fi[i]);
}
}
void move(double step, double angle)
{
for(int i=0; i<3; i++)
{
arc[i].x+=step*cos(angle);
arc[i].y+=step*sin(angle);
}
}
double GetArea()
{
/*вычисление конкретной площади*/
return abs(arc[0].x*(arc[1].y-arc[2].y)+arc[1].x*(arc[2].y-arc[0].y)+arc[2].x*(arc[0].y-arc[1].y))/2;
}
~Triangle(){}
};
class Octagon : public Shape {
public:
Octagon(){arc.resize(8); ID='O';}
void CreateShape(){/*...*/}
/* .....*/
double GetArea(){/*вычисление конкретной площади*/return 8;}
void move(){}
void rotate(){}
~Octagon(){}
};
class Square : public Shape {
public:
Square(){arc.resize(4); ID='S';}
void CreateShape(){/*...*/}
/* .....*/
double GetArea(){/*вычисление конкретной площади*/return 8;}
void move(){}
void rotate(){}
~Square(){}
};
class Parallelogram : public Shape {
public:
Parallelogram(){arc.resize(4); ID='P';}
void CreateShape(){/*...*/}
/* .....*/
double GetArea(){/*вычисление конкретной площади*/return 8;}
void move(){}
void rotate(){}
~Parallelogram(){}
};
class trapeze : public Shape {
public:
trapeze(){arc.resize(4); ID='t';}
void CreateShape(){/*...*/}
/* .....*/
double GetArea(){/*вычисление конкретной площади*/return 8;}
void move(){}
void rotate(){}
~trapeze(){}
};
class hexagon : public Shape {
public:
hexagon(){arc.resize(6); ID='h';}
void CreateShape(){/*...*/}
/* .....*/
double GetArea(){/*вычисление конкретной площади*/return 8;}
void move(){}
void rotate(){}
~hexagon(){}
};
class Operation
{ //Класс, инкапсулирующий методы обработки всех объектов иерархии
public:
void Compare(Shape* s1, Shape* s2)
{// Проверка правильности подбора типов фигур
if ((s1->ID=='T' && s2->ID=='R')||(s2->ID=='T' && s1->ID=='R'))
cout<<"Correct choice"<<endl;
else {cout<<"Not such operation"<<endl; return;}
//Вычисления
if (s1->GetArea()> s2->GetArea()) cout<<"Area "<<s1->ID<<" > "<<s2->ID<<endl;
if (s1->GetArea()==s2->GetArea()) cout<<"Area "<<s1->ID<<" = "<<s2->ID<<endl;
if (s1->GetArea()< s2->GetArea()) cout<<"Area "<<s1->ID<<" < "<<s2->ID<<endl;
}
void IsInclude(Shape* s1, Shape* s2)
{
}
void IsIntersect(Shape* s1, Shape* s2)
{/* */}
};
class FactoryShape{// Класс–фабрика объектов, производных от Shape
public:
Shape* generator()
{ switch(rand() % 3) {
case 0:
return new Triangle;
case 1:
return new Rectangle;
case 2:
return new Octagon;
/* */
}
}
};
int main()
{
//Создаем 2 указателя на базовый класс
Shape *p1=0,*p2=0;
//Создаем заданные объекты
FactoryShape f;
cout<<"Input 1-t shape"<<endl;
char cond='y';
while (cond=='y')
{
if (p1) delete p1;
p1=f.generator();
cout<<p1->ID<<endl;
cout<<"Continue choice?(y/…)"<<endl;
cin>>cond;
}
p1->CreateShape();
cout<<"Input 2-d shape"<<endl;
cond='y';
while (cond=='y')
{if (p2) delete p2;
p2=f.generator();
cout<<p2->ID<<endl;
cout<<"Continue choice? (y/…)"<<endl;
cin>>cond;
}
p2->CreateShape();
Operation op;
op.Compare(p1,p2); //Сравниваем площади
if(p1) delete p1;
if(p2) delete p2;
return 0;
}