-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculation.cpp
More file actions
365 lines (259 loc) · 6.98 KB
/
calculation.cpp
File metadata and controls
365 lines (259 loc) · 6.98 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int RandomNumber(int from, int to)
{
int randNum = rand() % (to - from + 1) + from;
return randNum;
}
enum finalreslt { pass = 1, faile = 2 };
enum levels { easy = 1, mid = 2, hard = 3, mixerr = 4 };
enum opretation { add = 1, sub = 2, mul = 3, dive = 4, mixer = 5 };
string getoptypesymbol(opretation optype)
{
switch (optype)
{
case opretation::add:
return "+";
case opretation::sub:
return "-";
case opretation::mul:
return "*";
case opretation::dive:
return "/";
default:
return "mix";
}
}
string getQuestionlevel(levels questionlevel)
{
string arrquestiotext[4] = { " easy ", "mid", "hard", "mixerr" };
return arrquestiotext[questionlevel - 1];
}
void setscreenColor(bool rigth)
{
if (rigth)
system("color 2f");
else
{
system("color 4f");
cout << "\a";
}
}
short readtheQuestion()
{
int Question;
cout << "how mony Question do you want answer ?\n";
cin >> Question;
return Question;
}
levels ReadQuestionlevels()
{
short questionlevels = 0;
do
{
cout << "enter the level [1] easy , [2] med , [3] hared , [4] mix ? ";
cin >> questionlevels;
} while (questionlevels < 1 || questionlevels > 4);
return levels(questionlevels);
}
opretation Readoptype()
{
short optype;
cout << "enter the operation [1] add , [2] sub , [3] mul , [4] div , [5] mix ?\n";
cin >> optype;
return opretation(optype);
}
struct stQuestion
{
int number1 = 0;
int number2 = 0;
opretation operation;
levels questinlevel;
int correctanswer = 0;
int playeranswer = 0;
bool Answerreaslt = false;
};
struct stQuizz
{
stQuestion Questionlist[100];
short numberofquestion;
levels levelgame;
opretation op;
int numberofwronganswer = 0;
int numberofrigthanswer = 0;
bool ispasse = false;
};
int simplecalculat(int num1, int num2, opretation op)
{
switch (op)
{
case opretation::add:
return num1 + num2;
case opretation::sub:
return num1 - num2;
case opretation::mul:
return num1 * num2;
case opretation::dive:
return num1 / num2;
default:
return num1 + num2;
}
}
opretation Getrandomoperationtype()
{
int op = RandomNumber(1, 4);
return (opretation)op;
}
int readthemonyQuestion()
{
int Question;
cout << "how mony Question do you want answer ?\n";
cin >> Question;
return Question;
}
stQuestion GenaratQuestion(levels Questinlevel, opretation optype)
{
stQuestion Question;
if (Questinlevel == levels::mixerr)
{
Questinlevel = (levels)RandomNumber(1, 3);
}
if (optype == opretation::mixer)
{
optype = Getrandomoperationtype();
}
Question.operation = optype;
switch (Questinlevel)
{
case levels::easy:
Question.number1 = RandomNumber(1, 10);
Question.number2 = RandomNumber(1, 10);
Question.correctanswer = simplecalculat(Question.number1, Question.number2, Question.operation);
Question.questinlevel = Questinlevel;
return Question;
case levels::mid:
Question.number1 = RandomNumber(10, 50);
Question.number2 = RandomNumber(10, 50);
Question.correctanswer = simplecalculat(Question.number1, Question.number2, Question.operation);
Question.questinlevel = Questinlevel;
return Question;
case levels::hard:
Question.number1 = RandomNumber(50, 100);
Question.number2 = RandomNumber(50, 100);
Question.correctanswer = simplecalculat(Question.number1, Question.number2, Question.operation);
Question.questinlevel = Questinlevel;
return Question;
case levels::mixerr:
Question.number1 = RandomNumber(50, 100);
Question.number2 = RandomNumber(50, 100);
Question.correctanswer = simplecalculat(Question.number1, Question.number2, Question.operation);
Question.questinlevel = Questinlevel;
return Question;
}
return Question;
}
void Genaratequestion(stQuizz& Quizz)
{
for (short question = 0; question < Quizz.numberofquestion; question++)
{
Quizz.Questionlist[question] = GenaratQuestion(Quizz.levelgame, Quizz.op);
}
}
int ReadquestionAnswer()
{
int answer = 0;
cin >> answer;
return answer;
}
void printtheQuestion(stQuizz& Quizz, short questionnuber)
{
cout << "\n";
cout << "Question [" << questionnuber + 1 << "/" << Quizz.numberofquestion << "] \n\n";
cout << Quizz.Questionlist[questionnuber].number1 << endl;
cout << Quizz.Questionlist[questionnuber].number2 << " ";
cout << getoptypesymbol(Quizz.Questionlist[questionnuber].operation);
cout << "\n_" << endl;
}
void CorrecttheQuestionAnswer(stQuizz& Quizz, short questionnuber)
{
if (Quizz.Questionlist[questionnuber].playeranswer != Quizz.Questionlist[questionnuber].correctanswer)
{
Quizz.Questionlist[questionnuber].Answerreaslt = false;
Quizz.numberofwronganswer++;
cout << "Worng Answer :-( \n";
cout << "The right answer is: ";
cout << Quizz.Questionlist[questionnuber].correctanswer;
cout << "\n";
}
else
{
Quizz.Questionlist[questionnuber].Answerreaslt = true;
Quizz.numberofrigthanswer++;
cout << "right answer ";
}
cout << endl;
setscreenColor(Quizz.Questionlist[questionnuber].Answerreaslt);
}
void AskAndcorretQuestionAnswer(stQuizz& Quizz)
{
for (short questionnumber = 0; questionnumber < Quizz.numberofquestion;questionnumber++)
{
printtheQuestion(Quizz, questionnumber);
Quizz.Questionlist[questionnumber].playeranswer = ReadquestionAnswer();
CorrecttheQuestionAnswer(Quizz, questionnumber);
}
Quizz.ispasse = (Quizz.numberofrigthanswer >= Quizz.numberofwronganswer);
}
string getfinalreslts(bool pass)
{
if (pass)
return "pass :-)";
else
return "fial :-(";
}
void printQuizzResultst(stQuizz Quizz)
{
cout << "\n";
cout << "\n----------------------------\n";
cout << " Final Resutls is " << getfinalreslts(Quizz.ispasse);
cout << "\n--------------------------------------_\n\n";
cout << "Number of Questions: " << Quizz.numberofquestion << endl;
cout << "Questions Level : " << getQuestionlevel(Quizz.levelgame) << endl;
cout << "OpType : " << getoptypesymbol(Quizz.op) << endl;
cout << "Number of Right Answers: " << Quizz.numberofrigthanswer << endl;
cout << "Number of Wrong Answers: " << Quizz.numberofwronganswer << endl;
cout << "------------------------------------_\n";
}
void playgammath()
{
stQuizz Quizz;
Quizz.numberofquestion = readthemonyQuestion();
Quizz.levelgame = ReadQuestionlevels();
Quizz.op = Readoptype();
Genaratequestion(Quizz);
AskAndcorretQuestionAnswer(Quizz);
printQuizzResultst(Quizz);
}
void resetscrean()
{
system("cls");
system("color 0f");
}
void startGame()
{
char playagain = 'Y';
do
{
resetscrean();
playgammath();
cout << "\n" << "do you want to play again Y/N ? ";
cin >> playagain;
} while (playagain == 'Y' || playagain == 'y');
}
int main()
{
startGame();
return 0;
}