-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.cpp
More file actions
350 lines (302 loc) · 12.6 KB
/
Interface.cpp
File metadata and controls
350 lines (302 loc) · 12.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <string>
#include <SFML/Graphics.hpp>
#include "Interface.h"
#include "RadixSort.h"
#include "MergeSort.h"
#include <ctime>
#include <iomanip>
#include <chrono>
using namespace std;
///Use our own knowledge and online resources
void InterfaceUI::ExtractBMIsFromFile() {
this->patients.clear();
ifstream file("../heart_attack_dataset.csv");
if (!file.is_open()) {
cerr << "Error: Could not open file 'ONE'." << endl;
return;
}
string line;
getline(file, line); // skip header
int count = 0;
while (getline(file, line)) {
size_t start = 0;
size_t end = 0;
int index = 0;
float bmi = 0;
int stressLevel = 0;
int outcome = 0;
string token;
while (end != string::npos && index <= 31) {
end = line.find(',', start);
token = line.substr(start, end - start);
if (index == 5) {
bmi = stof(token);
}
else if(index == 13){
stressLevel = stoi(token);
}
else if (index == 31) {
outcome = (token == "Heart Attack") ? 1 : 0;
break;
}
start = end + 1;
++index;
}
Patient* P = new Patient(bmi, outcome, stressLevel);
patients.push_back(P);
++index;
count++;
}
file.close();
}
double InterfaceUI::GetAverageScore(int filter) {
int targetBMI = filter;
int positive = 0;
int total = 0;
vector<Patient*> BMI_Group = {};
cout << "patient size :"<< patients.size() << endl;
cout << "target BMI :"<< targetBMI << endl;
for(int i = 0; i < this->patients.size(); i++){
if(this->patients[i]->BMI < targetBMI){
//i++;
}
else if(this->patients[i]->BMI == targetBMI){
BMI_Group.push_back(this->patients[i]);
//i++;
}
else{
break;
}
}
for(Patient* P : BMI_Group){
if(P->HAO == 1){
positive++;
}
total++;
}
cout << "total" << total << " - " << "positive " << positive << endl;
double average = (positive*1.0) / total;
return average * 100;
}
pair<string, string> InterfaceUI::MergeSortMethod( int filterValue) {
auto start = std::chrono::high_resolution_clock::now();
mergeSort(this->patients, 0, this->patients.size() - 1);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double result = GetAverageScore(filterValue);
stringstream ss;
ss << fixed << setprecision(2) << result;
string resultStr = ss.str();
return make_pair(resultStr + "%", to_string(duration.count()) + " microseconds.");
}
pair<string, string> InterfaceUI::RadixSortMethod( int filterValue) {
auto start = std::chrono::high_resolution_clock::now();
radixSort(this->patients);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double result = GetAverageScore(filterValue);
stringstream ss;
ss << fixed << setprecision(2) << result;
string resultStr = ss.str();
return make_pair(resultStr + "%", to_string(duration.count()) + " microseconds.");
}
void InterfaceUI::SFMLInterface() {
string fontPath = "../resources/Fonts/arial.ttf";
// Create the main window
sf::RenderWindow window(sf::VideoMode({800, 500}), "DIM Team - Sort Algorithms", sf::Style::Titlebar | sf::Style::Close);
//Set main banner
sf::Font fontTitle;
fontTitle.loadFromFile(fontPath);
sf::Text textTitle; // a font is required to make a text object
textTitle.setFont(fontTitle);
textTitle.setString("Merge Sort vs Radix Sort");
textTitle.setCharacterSize(36); // in pixels, not points!
textTitle.setFillColor(sf::Color::Blue);
textTitle.setStyle(sf::Text::Bold | sf::Text::Underlined);
textTitle.setPosition((window.getSize().x / 2)- 220, 50);
//Enter Value Label
sf::Font fontLabel;
fontLabel.loadFromFile(fontPath);
sf::Text textLabel; // a font is required to make a text object
textLabel.setFont(fontLabel);
textLabel.setString("Enter Value Between 18 to 40: ");
textLabel.setCharacterSize(20); // in pixels, not points!
textLabel.setFillColor(sf::Color::Blue);
textLabel.setStyle(sf::Text::Bold);
textLabel.setPosition(150, 150);
//Enter Value Text
sf::Font fontInput;
fontInput.loadFromFile(fontPath);
sf::Text textInput; // a font is required to make a text object
textInput.setFont(fontInput);
textInput.setString("00");
textInput.setCharacterSize(20); // in pixels, not points!
textInput.setFillColor(sf::Color::Blue);
textInput.setPosition(470, 150);
//Result Label Average
sf::Font fontResultLabel;
fontResultLabel.loadFromFile(fontPath);
sf::Text textResultLabel; // a font is required to make a text object
textResultLabel.setFont(fontResultLabel);
textResultLabel.setString("Your chances of a heart attack is: ");
textResultLabel.setCharacterSize(20); // in pixels, not points!
textResultLabel.setFillColor(sf::Color::Black);
textResultLabel.setStyle(sf::Text::Bold);
textResultLabel.setPosition(100, 370);
//Result Value Average
sf::Font fontResultValue;
fontResultValue.loadFromFile(fontPath);
sf::Text textResultValue; // a font is required to make a text object
textResultValue.setFont(fontResultValue);
textResultValue.setCharacterSize(20); // in pixels, not points!
textResultValue.setFillColor(sf::Color::Black);
textResultValue.setStyle(sf::Text::Bold);
textResultValue.setPosition(450, 370);
//Result Label Sort Elapsed Time
sf::Font fontElapsedTimeLabel;
fontElapsedTimeLabel.loadFromFile(fontPath);
sf::Text textElapsedTimeLabel; // a font is required to make a text object
textElapsedTimeLabel.setFont(fontElapsedTimeLabel);
textElapsedTimeLabel.setString("Sort - Elapsed Time : ");
textElapsedTimeLabel.setCharacterSize(20); // in pixels, not points!
textElapsedTimeLabel.setFillColor(sf::Color::Black);
textElapsedTimeLabel.setStyle(sf::Text::Bold);
textElapsedTimeLabel.setPosition(100, 420);
//Result Value Average
sf::Font fontElapsedTimeValue;
fontElapsedTimeValue.loadFromFile(fontPath);
sf::Text textElapsedTimeValue; // a font is required to make a text object
textElapsedTimeValue.setFont(fontElapsedTimeValue);
textElapsedTimeValue.setCharacterSize(20); // in pixels, not points!
textElapsedTimeValue.setFillColor(sf::Color::Black);
textElapsedTimeValue.setStyle(sf::Text::Bold);
textElapsedTimeValue.setPosition(350, 420);
//Radix Button
sf::Texture radixTexture;
radixTexture.loadFromFile("../resources/images/btnradixsort.png");
sf::Sprite radixSprite(radixTexture);
radixSprite.setPosition(sf::Vector2f(100,250));
//Merge Button
sf::Texture mergeTexture;
mergeTexture.loadFromFile("../resources/images/btnmergesort.png");
sf::Sprite mergeSprite(mergeTexture);
mergeSprite.setPosition(sf::Vector2f(468,250));
//Warning
sf::Font fontWarningLabel;
fontWarningLabel.loadFromFile(fontPath);
sf::Text textWarningLabel; // a font is required to make a text object
textWarningLabel.setFont(fontWarningLabel);
textWarningLabel.setString("* The number entered is not valid!!");
textWarningLabel.setCharacterSize(15); // in pixels, not points!
textWarningLabel.setFillColor(sf::Color::Red);
textWarningLabel.setStyle(sf::Text::Italic);
textWarningLabel.setPosition(150, 175);
//Warning
sf::Font fontTeamLabel;
fontTeamLabel.loadFromFile(fontPath);
sf::Text textTeamLabel; // a font is required to make a text object
textTeamLabel.setFont(fontTeamLabel);
textTeamLabel.setString("D(aniel) I(vana) M(arian) Team");
textTeamLabel.setCharacterSize(15); // in pixels, not points!
textTeamLabel.setFillColor(sf::Color::Black);
textTeamLabel.setStyle(sf::Text::Bold);
textTeamLabel.setPosition(530, 480);
string input_text = "";
pair<string, string> output_text;
sf::Clock clock;
bool validNumber = true;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
if ((mousePos.x >= 100 ) && (mousePos.x <= 332 )
&& (mousePos.y >= 250 ) && (mousePos.y <= 332 )) {
if (input_text != "") {
if (stoi(input_text) >=18 && stoi(input_text) <=40) {
radixTexture.loadFromFile("../resources/images/sorting.png");
window.draw(radixSprite);
window.display();
output_text =RadixSortMethod(stoi(input_text));
textResultValue.setString(output_text.first);
textElapsedTimeValue.setString(output_text.second);
ExtractBMIsFromFile();
radixTexture.loadFromFile("../resources/images/btnradixsort.png");
} else { validNumber = false; }
}
}
if ((mousePos.x >= 468 ) && (mousePos.x <= 700 )
&& (mousePos.y >= 250 ) && (mousePos.y <= 332 )) {
if (input_text != "") {
if (stoi(input_text) >=18 && stoi(input_text) <=40) {
mergeTexture.loadFromFile("../resources/images/sorting.png");
window.draw(mergeSprite);
window.display();
output_text =MergeSortMethod(stoi(input_text));
textResultValue.setString(output_text.first);
textElapsedTimeValue.setString(output_text.second);
ExtractBMIsFromFile();
mergeTexture.loadFromFile("../resources/images/btnmergesort.png");
} else { validNumber = false; }
}
}
}
}
if (event.type == sf::Event::TextEntered) {
if (event.text.unicode > 47 && event.text.unicode < 58) {
validNumber = true;
if (std::isprint(event.text.unicode))
input_text += event.text.unicode;
}
}
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::BackSpace) {
validNumber = true;
if (!input_text.empty())
input_text.pop_back();
}
//if (event.key.code == sf::Keyboard::Return) {
// input_text += '\n';
//}
}
}
static sf::Time text_effect_time;
static bool show_cursor;
text_effect_time += clock.restart();
if (text_effect_time >= sf::seconds(0.5f))
{
show_cursor = !show_cursor;
text_effect_time = sf::Time::Zero;
}
textInput.setString(input_text + (show_cursor ? '_' : ' '));
// Clear screen
window.clear();
// Update the window
window.clear(sf::Color::White);
if (!validNumber) {
window.draw(textWarningLabel);
radixTexture.loadFromFile("../resources/images/disbtnradixsort.png");
mergeTexture.loadFromFile("../resources/images/disbtnmergesort.png");
}
else {
radixTexture.loadFromFile("../resources/images/btnradixsort.png");
mergeTexture.loadFromFile("../resources/images/btnmergesort.png");
}
window.draw(textTitle);
window.draw(textLabel);
window.draw(textInput);
window.draw(radixSprite);
window.draw(mergeSprite);
window.draw(textResultLabel);
window.draw(textResultValue);
window.draw(textElapsedTimeLabel);
window.draw(textElapsedTimeValue);
window.draw(textTeamLabel);
window.display();
}
}