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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
| #include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class point{//класс точки
public:
float x, y;
float operator %(const point &p) {
return sqrt(pow(this->x - p.x, 2) + pow(this->y - p.y, 2));
}
};
class shape{
protected:
char * name; //название фигуры
unsigned short int count;
point * points;
float lengths;
public:
shape() {//конструктор
this -> name = "";
this -> count = 0;
this -> points = 0;
}
~shape() {//деструктор
if (this -> points) delete [] this->points;
if (this -> name) delete [] this->name;
}
shape(unsigned short int k) {
this -> count = k;
this -> points = new point[k];
for(int i = 0; i < k; i++) {
this -> points[i].x = 0;
this -> points[i].y = 0;
}
}
void enterPoints() {
cout << "Entering points for " << this->name << endl;
while (this -> count <= 2) {//точек не может быть две
cout << "Enter points count: ";
cin >> this -> count;
}
if (!this -> points){ //если массив не создан
this -> points = new point[this -> count];//создаем массив для точек
}
for (int i = 0; i < this -> count; i++) {
cout << "Enter X for point " << i << ": ";
while (!(cin >> this -> points[i].x)){
cin.clear();
while ( cin.get() != '\n' ) ;
cout << "Enter again: ";
}
cout << "Enter Y for point " << i << ": ";
while (!(cin >> this -> points[i].y)){
cin.clear();
while ( cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
}
void printShape() {
if (this -> count <= 0) { //если точек нету
cout << "No shape" << endl;
return;
}
cout << "Shape" << this -> name << " has " << this -> count << " points with next coordinates:" << endl;
for (int i = 0; i < this -> count; i++) {
cout << "Point " << i << " - X: " << this -> points[i].x << " Y: " << this -> points[i].y << endl;
}
}
float calcL(unsigned short int k, unsigned short int d) {
point * p1 = &this -> points[k];
point * p2 = &this -> points[d];
this -> lengths = *p1 % *p2;
return this->lengths;
}
void move(){
float xx = 0, yy = 0;
if (this -> count <= 0) { //если точек нету
cout << "No shape" << endl;
return;
}
cout << "Enter how much X you want to move a shape" << endl;
cin >> xx;
cout << "Enter how much Y you want to move a shape" << endl;
cin >> yy;
for (int i = 0; i < this -> count; i++) {
this -> points[i].x = this -> points[i].x + xx;
this -> points[i].y = this -> points[i].y + yy;
}
}
};
class Quadrate: public shape {
public:
Quadrate() : shape(4) {//вызываем в начале shape с 4 точками, для создания массива под хранение 4 точек
char * n = "Quadrate";
this -> name = new char[strlen(n) + 1];
strcpy(this -> name, n);//в строку this->name записываем n
}
void enterPoints() {
bool check = false;
do {
check = false;
shape::enterPoints();
point * p1 = &this->points[0];
point * p2 = &this->points[1];
point * p3 = &this->points[2];
point * p4 = &this->points[3];
float a = *p1 % *p2;
float b = *p2 % *p3;
float c = *p3 % *p1;//гипотенуза
float p1p3 = sqrt(pow(a, 2) + pow(b, 2)); //гипотенуза
check = (c == p1p3 && c > 0);
a = *p2 % *p3;
b = *p3 % *p4;
c = *p4 % *p2;
p1p3 = sqrt(pow(a, 2) + pow(b, 2));
check = (check && (c == p1p3));
if (!check) {
cout << "Wrong coordinates. It is not a Quadrate!" << endl;
}
} while(!check);
}
};
class Pentagon: public shape {
public:
Pentagon() : shape(5) {//вызываем в начале shape с 4 точками, для создания массива под хранение 4 точек
char * n = "Pentagon";
this -> name = new char[strlen(n) + 1];
strcpy(this -> name, n);//в строку this->name записываем n
}
void enterPoints() {
bool check = true;
do{
check = true;
shape::enterPoints();
float a = this -> calcL(0, 1);
float b = this -> calcL(1, 2);
float c = this -> calcL(2, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(0, 3);
b = this -> calcL(3, 4);
c = this -> calcL(4, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(0, 1);
b = this -> calcL(1, 4);
c = this -> calcL(4, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(0, 2);
b = this -> calcL(2, 4);
c = this -> calcL(4, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(0, 2);
b = this -> calcL(2, 3);
c = this -> calcL(3, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(0, 1);
b = this -> calcL(1, 3);
c = this -> calcL(3, 0);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(1, 2);
b = this -> calcL(2, 3);
c = this -> calcL(3, 1);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(2, 3);
b = this -> calcL(3, 4);
c = this -> calcL(4, 2);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
a = this -> calcL(1, 3);
b = this -> calcL(3, 4);
c = this -> calcL(4, 1);
check = check && ((a + b) > c);
check = check && ((b + c) > a);
check = check && ((c + a) > b);
}while(!check);
}
};
int main(){
int kol = 0, o = 0, p = 0, pp = 0, oo = 0, ppp = 0, q = 0, w = 0;
bool case1 = false, case2 = false, n = false;
while (o < 1){ //количество квадратов
cout << "How much Quadrate you want?" << endl;
while (!(cin >> o)){
cin.clear();
while ( cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
Quadrate ** so = new Quadrate * [o];//создаем массив, хранящий квадраты
while (p < 1){ //количество пятиугольников
cout << "How much Pentagon you want?" << endl;
while (!(cin >> p)){
cin.clear();
while ( cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
Pentagon ** sp = new Pentagon * [p];//создаем массив, хранящий пятиугольники
for (int i = 0; i < o; i++){//создание объектов - квадрат
so[i] = new Quadrate;
}
for (int i = 0; i < p; i++){//создание объектов пятиугольник
sp[i] = new Pentagon;
}
bool ot = false;
do{
int k = 0, n = -1;
cout << "Menu" << endl;
cout << "1 - Create Quadrate" << endl;
cout << "2 - Create Pentagon" << endl;
cout << "3 - Cout Quadrate" << endl;
cout << "4 - Cout Pentagon" << endl;
cout << "5 - IsIntersect" << endl;
cout << "6 - Move Quadrate " << endl;
cout << "7 - Move Pentagon " << endl;
cout << "8 - Exit" << endl;
cout << "Enter the number" << endl;
while (k < 1 || k > 6){
while (!(cin >> k)){
cin.clear();
while (cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
switch (k){
case 1:
if (oo == o){
cout << "Memory is full" << endl;
break;
}
so[oo] -> enterPoints();
oo++;
case1 = true;
break;
case 2:
if (pp == p){
cout << "Memory is full" << endl;
break;
}
sp[pp] -> enterPoints();
pp++;
case2 = true;
break;
case 3:
q = -1;
if (!case1){
cout << "There is not Quadrate" << endl;
break;
}
cout << "Enter the number which Quadrate output";
while (q < 0 || q > oo){
while (!(cin >> q)){
cin.clear();
while (cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
so[q] -> printShape();
break;
case 4:
w = -1;
if (!case2){
cout << "There is not Pentagon" << endl;
break;
}
cout << "Enter the number which Pentagon output";
while (w < 0 || w > pp-1){
while (!(cin >> w)){
cin.clear();
while (cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
sp[w] -> printShape();
break;
case 5:
sp[w] -> insert(*so[0], *sp[0]);
break;
case 6:
q = -1;
cout << "Enter the number which Quadrate move";
while (q < 0 || q > oo-1){
while (!(cin >> q)){
cin.clear();
while (cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
so[q] -> move();
break;
case 7:
w = -1;
cout << "Enter the number which Pentagon move";
while (w < 0 || w > pp){
while (!(cin >> w)){
cin.clear();
while (cin.get() != '\n' ) ;
cout << "Enter again: ";
}
}
sp[w] -> move();
break;
case 8:
ot = true;
break;
}
}while(!ot);
delete sp;
delete sp;
return 0;
} |