forked from NSDie/Operations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
296 lines (287 loc) · 6.71 KB
/
Copy pathrandom.cpp
File metadata and controls
296 lines (287 loc) · 6.71 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int flag =0 ;
int count =0;
class Question {
public :
int randNum();
int randSym();
char symbol(int i);
};
int Question::randNum()
{
return 1+rand()%9;
}
int Question::randSym()
{
return 1+rand()%4;
}
char Question::symbol(int i){
switch(i)
{
case 1: return '+';
break;
case 2: return '-';
break;
case 3: return '*';
break;
case 4: if(flag=0)
{
return '/';
flag=1;
break;
}
else return symbol(rand()%4+1);
}
}
int check(char str[])
{
int sum,i,j,t,top=0;
double d;
char exp[10];
char stack[10];
char ch;
sum=8;
t=1;i=1;
ch=str[i];i++;
while (ch!='#')
{
switch(ch)
{
case '(': /*判定为左括号*/
top++;stack[top]=ch;
break;
case ')': /*判定为右括号*/
while (stack[top]!='(')
{
exp[t]=stack[top];top--;t++;
}
top--;
break;
case '+': /*判定为加减号*/
case '-':
while (top!=0 && stack[top]!='(')
{
exp[t]=stack[top];top--;t++;
}
top++;stack[top]=ch;
break;
case '*': /*判定为'*'或'/'号*/
case '/':
while (stack[top]=='*' || stack[top]=='/')
{
exp[t]=stack[top];top--;t++;
}
top++;stack[top]=ch;
break;
case ' ':break;
default:
while (ch>='0' && ch<='9') /*判定为数字*/
{
exp[t]=ch;t++;
ch=str[i];i++;
}
i--;
exp[t]='#';t++;
}
ch=str[i];i++;
}
while (top!=0)
{
exp[t]=stack[top];t++;top--;
}
exp[t]='#';
t=1;top=0;
ch=exp[t];t++;
while (ch!='#')
{
switch (ch)
{
case '+':stack[top-1]=stack[top-1]+stack[top];
top--;break;
case '-':stack[top-1]=stack[top-1]-stack[top];
top--;break;
case '*':stack[top-1]=stack[top-1]*stack[top];
top--;break;
case '/':if (stack[top]!=0)
stack[top-1]=stack[top-1]/stack[top];
else
{
printf("\n\t除零错误!\n");
exit(0);/*异常退出*/
}
top--;break;
default:d=0;
while (ch>='0' && ch<='9') /*判定为数字字符*/
{
d=10*d+ch-'0'; /*将数字字符转换成对应的数值*/
ch=exp[t];t++;
}
top++;
stack[top]=d;
}
ch=exp[t];t++;
}
return stack[top];
}
bool judge(double a){
int a1;
a1=(int)a;
if(a1==a) return true;
else return false;
}
int gcd(int m,int n)
{
if(m<0)
m=0-m;
if(n<0)
n=0-n;
for(int c=m%n;c!=0;)
{
m=n;
n=c;
c=m%n;
}
return n;
}
void Fraction(){
int a,b,c,d,g1,g2,g3;
int son,mom,num[5];
char s[5],ch;
srand(time(NULL));
a=1+rand()%5;
b=1+rand()%5;
c=6+rand()%5;
d=6+rand()%5;
g1=gcd(a,c);
g2=gcd(b,d);
a/=g1;
c/=g1;
b/=g2;
d/=g2;
cout <<a<<'/'<<c<<'+'<<b<<'/'<<d<<'=';
son=a*d+b*c;
mom=c*d;
g3=gcd(son,mom);
son/=g3;
mom/=g3;
cin>>s;
int t=0,i=0,e,length=strlen(s);
ch=s[t];
for(t=0;t<length;t++)
{
ch=s[t];
switch(ch)
{
case '/': break;
default : e=0;
while(ch>='0' && ch<='9') /*判定为数字字符*/
{
e=10*e+ch-'0'; /*将数字字符转换成对应的数值*/
t++;ch=s[t];
}
num[i]=e;
i++;
break;
}
}
if(num[0]==son&&num[1]==mom) cout <<"回答正确!"<<endl;
else cout <<"答错了!正确答案是:"<<son<<'/'<<mom<<endl;
}
void Rand(){
int i,temp;
int num[5];
int sign[5];
char sym[5];
char str[11],t;
int s1;
int count;
double s;
srand((unsigned)time(NULL));
Question q;
for(i=1;i<=4;i++)
num[i]=q.randNum();
for(i=1;i<=3;i++)
sign[i]=q.randSym();
for(i=1;i<=3;i++)
sym[i]=q.symbol(sign[i]);
for(i=1;i<=4;i++)
str[i*2-1]='0'+num[i];
for(i=1;i<=3;i++)
str[i*2]=sym[i];
temp=rand()%3+1;
for(i=8;i>=temp*2-1;i--)
str[i]=str[i-1];
str[temp*2-1]='(';
str[9]=')';
for(i=1;i<=9;i++)
cout <<str[i];
cout <<"=";
cin>>s1;
str[10]='#';
s=check(str);
if(judge(s))
{
if(s1==1975754292)
{
cout<<"目前答对了"<<count<<"道题"<<endl;
exit(0);
}
else if(s1!=s)
cout<<"答错了!正确答案是"<<s<<endl;
else
cout<<"回答正确!"<<endl;
}
else
Rand();
}
int main()
{
srand((unsigned)time(NULL));
char L;
int n,i,m;
char str[100];
cout <<"请问要使用哪种语言/Which language do you want to use?(C or E)"<< endl;
cin >>L;
if(L=='C')
{
cout <<"*********************************************************************"<<endl;
cout <<"***欢迎使用小学生四则运算练习题系统1.0 *"<<endl;
cout <<"*** (统计题数或是结束程序请输入g)*"<<endl;
cout <<"***--------附带计算器功能 输入1->练习题系统 输入2-> 计算器 *"<<endl;
cout <<"*********************************************************************"<<endl;
cin >>m;
switch(m)
{
case 1: cout <<"☆请输入练习题数n:";
cin >>n;
for(i=1;i<=n;i++)
{
if(i%5==0)
Fraction();
else
Rand();
}
case 2: cout <<"☆请输入算式: (注:以“#”表示结尾,输入g退出程序)"<<endl;
while(1)
{
cin >>str;
if(str[0]=='g')
exit(0);
else
cout <<"结果是:"<<check(str)<<endl;
}
}
}
else
{
cout <<"***************************************************************************************"<<endl;
cout <<"***Welcome to the use of the exercises system for pupils 1.0 *"<<endl;
cout <<"***(statistics or the end of the program, please enter g) *"<<endl;
cout <<"*** Along with the calculator function input 1-2 - >exercises system input calculator *"<<endl;
cout <<"***************************************************************************************"<<endl;
}
return 0;
}