-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.h
61 lines (54 loc) · 1.72 KB
/
factory.h
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
#pragma once
#include "Shape.h"
#include "My_forward_list.h"
#include <random>
Shape* random_factory(const std::string &type)//заданный тип
{
static std::random_device rd;
std::uniform_int_distribution<int> gen_coord(0, 1000), gen_r(1, 100), gen_num(0, 12);
if(type == std::string("Point"))
return new Point(gen_coord(rd), gen_coord(rd));
else if(type == std::string("Circle"))
return new Circle(Point(gen_coord(rd), gen_coord(rd)), gen_r(rd));//Circle
else if(type == std::string("Rect"))
{
int x1=gen_coord(rd), y1=gen_coord(rd), x2, y2;
do
{
x2=gen_coord(rd);
}while(x1==x2);
do
{
y2=gen_coord(rd);
}while(y1==y2);
return new Rect(Point(x1, y1), Point(x2, y2));//Rect
}
else if(type == std::string("Square"))
{
int x1=gen_coord(rd), y1=gen_coord(rd), x2, y2;
do
{
x2=gen_coord(rd);
y2=gen_coord(rd);
}while(x1==x2 && y1==y2);
return new Square(Point(x1, y1), Point(x2, y2));//Square
}
else if(type == std::string("Polyline"))
{
My_forward_list<Point> a= My_forward_list<Point>();
int n=gen_num(rd);
for(int i=0; i<n; i++)
a.add_in_beginning(Point(gen_coord(rd), gen_coord(rd)));
return new Polyline(a);
}
else if(type == std::string("Polygon"))
{
My_forward_list<Point> a= My_forward_list<Point>();
int n=gen_num(rd);
for(int i=0; i<n; i++)
a.add_in_beginning(Point(gen_coord(rd), gen_coord(rd)));
return new Polygon(a);
}
else
return NULL;
}