-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercises.cpp
173 lines (136 loc) · 4.96 KB
/
Exercises.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
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
#include <list>
#include <iostream>
#include <cmath>
class Tree {
public:
float value;
std::list<Tree> children;
};
#include "Exercises.h"
////////////////////////////// Exercise 2
float TreeTraversal(const Tree &t, TreeVisitor &visitor, bool countOnlyEvenLevels) {
return visitor.visitTree(t, countOnlyEvenLevels);
}
///////////////////////////// Exercise 3
Complex ComplexOperation(Complex c1, Complex c2) {
// return Complex(0, 0);
return (c1 - c2) * (c1 + c2);
}
void fillTree(Tree &tree, int level) {
tree.value = 2.f * level;
if (level >= 4)
return;
Tree child;
fillTree(child, level + 1);
for (int i = 0; i < level + 2; ++i) {
tree.children.push_back(child);
}
}
#define EPSILON 0.01;
bool equalFloats(float f1, float f2) {
return fabs(f1 - f2) < EPSILON;
}
/////////////////////////////////
///////////////////////////////// Main function
/////////////////////////////////
int main() {
////////////// Test functions
////////////////////////// Exercise 1
std::list<float> stats;
stats.push_back(-4.f);
stats.push_back(-2.f);
stats.push_back(0.f);
stats.push_back(0.f);
stats.push_back(0.f);
stats.push_back(2.f);
stats.push_back(2.f);
stats.push_back(2.f);
std::pair<float, float> statsAnswer = Statistics(stats);
if (equalFloats(statsAnswer.first, 0.f) && equalFloats(statsAnswer.second, 2.0)) {
std::cout << "Exercise 1: Correct" << std::endl;
} else {
std::cout << "Exercise 1: Incorrect" << std::endl;
}
///////////////////////////////////////////////////////////
////////////////////////// Exercise 2
Tree root;
fillTree(root, 0);
TreeVisitor visitor;
float visitAnswer1 = visitor.visitTree(root, false);
float visitAnswer2 = visitor.visitTree(root, true);
std::cout << visitAnswer1 << " " << visitAnswer2 << "\n";
if (equalFloats(visitAnswer1, 1132.f) && equalFloats(visitAnswer2, 984.f)) {
std::cout << "Exercise 2: Correct" << std::endl;
} else {
std::cout << "Exercise 2: Incorrect" << std::endl;
}
///////////////////////////////////////////////////////////
////////////////////////// Exercise 3
Complex c1(1.f, 2.f);
Complex c2(5.f, -3.f);
Complex complexAnswer = ComplexOperation(c1, c2);
if (equalFloats(complexAnswer.real, -19.f) && equalFloats(complexAnswer.im, 34.f)) {
std::cout << "Exercise 3: Correct" << std::endl;
} else {
std::cout << complexAnswer.real << " " << complexAnswer.im << std::endl;
std::cout << "Exercise 3: Incorrect" << std::endl;
}
///////////////////////////////////////////////////////////
////////////////////////// Exercise 4
std::list<float> heights;
heights.push_back(7);
heights.push_back(6);
heights.push_back(4);
heights.push_back(7);
heights.push_back(6);
heights.push_back(9);
heights.push_back(3);
heights.push_back(1);
heights.push_back(5);
heights.push_back(3);
std::list<float> test1;
test1.push_back(6);
test1.push_back(3);
test1.push_back(1);
test1.push_back(5);
test1.push_back(9);
std:: cout<<WaterLevels(test1) << " djksas" << std::endl;
std::list<float> test2;
test2.push_back(3);
test2.push_back(3);
test2.push_back(3);
std:: cout<<WaterLevels(test2) << " djksas" << std::endl;
std::list<float> test3;
test3.push_back(7);
test3.push_back(8);
test3.push_back(9);
std:: cout<<WaterLevels(test3) << " djksas" << std::endl;
float waterAnswer = WaterLevels(heights);
if (equalFloats(waterAnswer, 11.f)) {
std::cout << "Exercise 4: Correct" << std::endl;
} else {
std::cout << "Exercise 4: Incorrect" << std::endl;
}
///////////////////////////////////////////////////////////
////////////////////////// Exercise 4
std::set<std::pair<location, location> > labyrinth;
labyrinth.insert(std::pair<location, location>(location(0, 0), location(1, 0)));
labyrinth.insert(std::pair<location, location>(location(0, 1), location(1, 1)));
labyrinth.insert(std::pair<location, location>(location(0, 2), location(0, 3)));
labyrinth.insert(std::pair<location, location>(location(1, 1), location(1, 2)));
labyrinth.insert(std::pair<location, location>(location(1, 2), location(2, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 3), location(3, 3)));
labyrinth.insert(std::pair<location, location>(location(2, 2), location(3, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 1), location(3, 1)));
int labAnswer = Labyrinth(labyrinth, 4);
std::cout << "LAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaa" << std::endl;
if (labAnswer == 13) {
std::cout << "Exercise 5: Correct" << std::endl;
} else {
std::cout << "Exercise 5: Incorrect" << std::endl;
}
///////////////////////////////////////////////////////////
std::cout << "All done!" << std::endl;
system("pause");
return 0;
}