-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialCode.cpp
74 lines (60 loc) · 1.6 KB
/
initialCode.cpp
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
Refactor the code.
Paste your solution here or provide a link to the Github repository.
#include <stdio.h>
class Feature
{
public:
enum FeatureType {eUnknown, eCircle, eTriangle, eSquare};
Feature() : type(eUnknown), points(0) { }
~Feature()
{
if (points)
delete points;
}
bool isValid()
{
return type != eUnknown;
}
bool read(FILE* file)
{
if (fread(&type, sizeof(FeatureType), 1, file) != sizeof(FeatureType))
return false;
short n = 0;
switch (type)
{
case eCircle: n = 3; break;
case eTriangle: n = 6; break;
case eSquare: n = 8; break;
default: type = eUnknown; return false;
}
points = new double[n];
if (!points)
return false;
return fread(&points, sizeof(double), n, file) == n*sizeof(double);
}
void draw()
{
switch (type)
{
case eCircle: drawCircle(points[0], points[1], points[2]); break;
case eTriangle: drawPolygon(points, 6); break;
case eSquare: drawPolygon(points, 8); break;
}
}
protected:
void drawCircle(double centerX, double centerY, double radius);
void drawPolygon(double* points, int size);
double* points;
FeatureType type;
};
int main(int argc, char* argv[])
{
Feature feature;
FILE* file = fopen("features.dat", "r");
feature.read(file);
if (!feature.isValid())
return 1;
return 0;
}
Refactor the code.
Paste your solution here or provide a link to the Github repository.