-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (147 loc) · 3.86 KB
/
main.cpp
File metadata and controls
168 lines (147 loc) · 3.86 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
#include <iostream>
#include <ctime>
#include <sys/stat.h>
#include <string>
#include <cstring>
#include "timer.hpp"
struct Score {
double total;
double average;
};
bool score_dump(Score& score, FILE* file);
bool score_load(Score& score, FILE* file);
inline bool exists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
static void show_score(const char* filename){
Score score;
FILE* file = fopen(filename, "rb");
if (file == NULL) {
std::cout << "There is no " << filename << " in this folder\n";
return;
}
fread(&score, sizeof(Score), 1, file);
/*
if (score.total == 0 || score.average == 0) {
std::cout << "There is no " << filename << " in this folder";
}
*/
std::cout << "total: " << score.total << std::endl;
std::cout << "average: " << score.average << std::endl;
fclose(file);
}
int main(int argc, char** argv) {
using std::cout;
using std::endl;
using std::cin;
std::string score_filename = "score.data";
if (argc == 2) {
if (!strcmp(argv[1], "score")) {
show_score(score_filename.c_str());
return EXIT_SUCCESS;
}
}
srand(time(NULL));
static const char operations[] = {'+', '-'};
int level;
int num1, num2, result;
int limit;
int uAnswer; /* user answer */
cout << "Welcome to the Math In Mind\nChoose your level:\n";
cout << "1. Easy: 10-100\n"
<< "2. Medium: 10 - 1000\n"
<< "3. Hard: 10 - 10000\n";
cin >> level;
while (level > 3 || level < 1) {
cout << "Please, enter a number from 1 to 3: ";
cin >> level;
}
switch (level) {
case 1: /* easy level */
limit = 100;
break;
case 2: /* Medium level*/
limit = 1000;
break;
case 3: /* Hard level */
limit = 10000;
}
Timer t;
for (int i = 1; i <= 10; i++) {
// generate numbers between 10 and $limit
num1 = (rand() % limit - 11) + 10;
num2 = (rand() % limit - 11) + 10;
char curr_operation = operations[rand() % 2];
switch (curr_operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
}
cout << i << "/10) " << num1 << " " << curr_operation << " " << num2 << " = ";
cin >> uAnswer;
while (uAnswer != result) {
cout << "Wrong answer, try again\n";
cout << num1 << " " << curr_operation << " " << num2 << " = ";
cin >> uAnswer;
}
}
double left = t.elapsed();
double average = left * 0.1;
cout << "Total time for 10 tasks: " << left << endl;
cout << "Average for 1 task: " << average << endl;
Score score_current{left, average};
Score temp;
// calculate and dump best result
if (exists(score_filename)) {
FILE* score_file = fopen(score_filename.c_str(), "rb");
if (score_file == NULL) {
perror("opening score_file");
exit(EXIT_FAILURE);
}
score_load(temp, score_file);
if (left < temp.total) { // this result is better than previous
cout << "Wow! That result is better then previous\n";
cout << "previous:\n" << temp.total << endl << temp.average << endl;
freopen(score_filename.c_str(), "wb", score_file);
if (!score_dump(score_current, score_file)) {
std::cout << "error while dumping your best result.\n";
}
}
else { // this result is worse than previous
cout << "Best result:\n";
cout << "total: " << temp.total << endl;
cout << "average: "<< temp.average << endl;
}
fclose(score_file);
}
else {
FILE* score_file = fopen(score_filename.c_str(), "wb");
if (score_file == NULL) {
perror("opening score_file");
exit(EXIT_FAILURE);
}
if (!score_dump(score_current, score_file)) {
std::cout << "error while dumping your first result.\n";
}
fclose(score_file);
}
return 0;
}
bool score_dump(Score &score, FILE* file) {
if (!fwrite(&score, sizeof(Score), 1, file)) {
perror("score_dump");
return false;
}
return true;
}
bool score_load(Score& score, FILE* file) {
if (!fread(&score, sizeof(Score), 1, file)) {
perror("score_load");
return false;
}
return true;
}