-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionFun.cpp
More file actions
93 lines (89 loc) · 1.47 KB
/
Copy pathQuestionFun.cpp
File metadata and controls
93 lines (89 loc) · 1.47 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
// 20190324_ex5.cpp: ¥Dn±M®×ÀÉ¡C
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <iomanip>
#define N 5
using namespace std;
enum myOperator{add, sub, mul, divi};
class QUESTION{
private:
int iLeft;
int iRight;
int iAnswer;
myOperator opt;
public:
QUESTION();
void show();
bool veri(int);
void showAnswer();
};
int main()
{
srand(unsigned(time(NULL)));
int iUserInput, iCount = 0;
QUESTION myExam[N];
for(int i = 0; i<N; ++i){
myExam[i].show();
cin >> iUserInput;
if (myExam[i].veri(iUserInput)) iCount++;
}
cout << "µª¹ï " << iCount << " ÃD" << endl;
for(int i = 0; i<N; ++i){
myExam[i].showAnswer();
}
system("pause");
return 0;
}
QUESTION::QUESTION(){
iLeft = rand()%100 + 1;
iRight = rand()%100 + 1;
int tmp = rand()%3;
switch(tmp){
case 0:
opt = add;
iAnswer = iLeft + iRight;
break;
case 1:
opt = sub;
iAnswer = iLeft - iRight;
break;
case 2:
opt = mul;
iAnswer = iLeft * iRight;
break;
}
}
void QUESTION::show(){
cout << setw(3) <<iLeft;
switch(opt){
case add:
cout << " + ";
break;
case sub:
cout << " - ";
break;
case mul:
cout << " * ";
break;
}
cout << setw(3) << iRight << " = ";
}
bool QUESTION::veri(int answer){
return this->iAnswer == answer;
}
void QUESTION::showAnswer(){
cout << setw(3) <<iLeft;
switch(opt){
case add:
cout << " + ";
break;
case sub:
cout << " - ";
break;
case mul:
cout << " * ";
break;
}
cout << setw(3) << iRight << " = " << iAnswer << endl;
}