-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculate Update.cpp
381 lines (359 loc) · 6.67 KB
/
Calculate Update.cpp
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;
/*****主要函数*****/
void generateExpression();//产生算式
void calculateResult (int t); //运算答案
void checkanswer(int result); //检验答案
void print(); //统计结果
int scan();//用户输入
void randomNumber(); //随机数字
void randomOperation(); //随机符号
/*递归运算答案部分*/
void match( char nowsign );
double low(int t);
double mid(int t);
double high(int t);
char nowsign,sign,signs[10];
int k=0,correct=0,wrong=0,flag=0,temp=0,bracketNum=0,braO,braT,lan,nums[10],digit,judge;
int main()
{
srand((unsigned(time(NULL))));
int n=scan();
for (int j=0;j<n;j++)
{
judge=rand()%4; //设置产生分式的概念
randomNumber(); //产生随机数字
randomOperation(); //产生随机符号
generateExpression();//产生算式
cout<<endl;
}
print(); //统计结果
return 0;
}
/*用户输入*/
int scan()
{
int n;
cout<<"请选择语言(Please choose language)"<<endl;cout<<"1.中文 2.English : ";
cin>>lan;
switch(lan)
{
case 1:cout<<"请输入题数:";cin>>n;break;
case 2:cout<<"Please enter a number of questions:";cin>>n;break;
defaul:cout<<"Error!";break;
}
return n;
}
/*随机产生数字*/
void randomNumber()
{
if (judge!=0)
{
digit=rand()%3+4; //数字的个数
for(int i=0;i<digit;i++)
{
nums[i]=rand()%9+1; // 数字
}
}
else if (judge==0)
{
for(int i=0;i<4;)
{
int x=rand()%9+1; //有可能是分子分母的两个数
int y=rand()%9+1;
if(x!=y)
{
if(x>y)
{
nums[i]=y;nums[i+1]=x;
}
else
{
nums[i]=x;nums[i+1]=y;
}
i+=2;
}
}
}
}
/*随机产生符号*/
void randomOperation()
{
char c[]="+-*/";
if (judge!=0)
{
for(int i=0;i<digit;i++)
{
signs[i]=c[rand()%4]; //符号
}
signs[digit-1]='=';
}
else if (judge==0)
{
signs[0]=c[rand()%4];
signs[1]='=';
}
}
/*产生算式*/
void generateExpression()
{
bracketNum=rand()%2; //括号个数
if(bracketNum==1)
{
flag=0;
temp=0;
braO=rand()%((digit-1)/2)+1;
braT=braO+rand()%((digit-1)/2)+2;
//分别在第几个数加括号
//braO+1是第一个括号的位置,braT是第二个括号的位置
}
calculateResult(digit);
}
/*运算答案*/
void calculateResult (int t)
{
double result=0;
if (judge!=0)
{
k=0;
sign = nums[0]; //载入第一个符号
result = low(t); //进行计算
if (result==(int)result)
{
if (bracketNum==0)
{
for (int i=0;i<t;i++)
{
if (signs[i]=='/')
cout<<nums[i]<<"÷";
else
cout<<nums[i]<<signs[i];
}
// cout<<result;
}
else if (bracketNum==1)
{
for(int i=0;i<t;i++) //测试算式
{
if(i==braO) cout<<"(";
cout<<nums[i];
if(i==braT-1) cout<<")";
if (signs[i]=='/')
cout<<"÷";
else
cout<<signs[i];
}
// cout<<result;
}
checkanswer(result);
}
else if (result!=(int)result)
{
randomNumber();
randomOperation();
generateExpression();
}
}
else if (judge==0)
{
int demo; //分子
double nume; //分母
demo=nums[1]*nums[3];
nums[0]*=nums[3];
nums[2]*=nums[1];
switch (signs[0])
{
case '+':nume=nums[0]+nums[2];break;
case '-':nume=nums[0]-nums[2];break;
case '*':nume=nums[0]*nums[2]/nums[3]/nums[1];break;
case '/':nume=(double)nums[0]/(double)nums[2];break;
}
result=nume/(double)demo;
if (result!=(int)result)
{
randomNumber();
randomOperation();
generateExpression();
}
else
{
nums[0]/=nums[3];
nums[2]/=nums[1];
for(int j=0,i=0;i<4;i++) //测试算式
{
if(i/2.0==i/2)
{
cout<<"(";
}
cout<<nums[i];
if(i/2.0!=i/2)
{
cout<<")";
}
if(i/2.0==i/2)
{
cout<<"/";
}
else
{
if (signs[j]=='/')
cout<<"÷";
else
cout<<signs[j];
j++;
}
}
checkanswer(result);
}
}
}
void checkanswer(int result) //检验答案
{
int answer;
char ch;
cin>>answer;
ch=getchar();
if((answer==result)&&(ch!='e'))
{
if(lan==1)
cout<<" 答案正确";
else
cout<<" correct";
correct+=1;
}
else if ((answer!=result)&&(ch!='e'))
{
if(lan==1)
cout<<" 答案错误,正确答案为:"<<result;
else
cout<<" wrong answer. The correct answer is:"<<result;
wrong+=1;
}
if (ch=='e')
{
print();
}
}
void match( char nowsign ) //匹配符号
{
if (bracketNum==0)
{
if( sign == nowsign ) //匹配成功,获取下一个标志
{
k++;
if ((k%2) == 0) //下一位是数字
sign = nums[k/2];
else //下一位是符号
sign = signs[(k-1)/2];
}
}
else if (bracketNum==1)
{
if( sign == nowsign ) //匹配成功,获取下一个标志
{
k++;
if ((k==2*braO)&&(flag==0))
{
sign = '(';
flag=1;
k--;
}
else if ((k==braT*2-1)&&(temp==0))
{
sign = ')';
temp=1;
k--;
}
else if ((k==2*braO)&&(flag==1))
sign = nums[k/2];
else if ((k==braT*2-1)&&(temp==1))
sign = signs[(k-1)/2];
else if (((k%2) == 0)&&(k!=2*braO)) //下一位是数字
sign = nums[k/2];
else if (((k%2) != 0)&&(k!=braT*2-1)) //下一位是符号
sign = signs[(k-1)/2];
}
}
}
double low(int t) //加减运算
{
double result1 = mid(t); //计算比加减运算优先级别高的部分
while(( sign == '+' ) || ( sign == '-' ))
if ( sign == '+')
{
match('+'); //进行加法运算
result1 += mid(t);
if (k==2*t-1)
break;
}
else if ( sign == '-')
{
match('-'); //进行减法运算
result1 -= mid(t);
if (k==2*t-1)
break;
}
return result1;
}
double mid(int t) //乘除运算
{
double result2 = high(t);
while(( sign == '*' ) || ( sign == '/' ))
if ( sign == '*')
{
match('*'); //进行乘法运算
result2 *= high(t);
if (k==2*t-1)
break;
}
else if ( sign == '/')
{
match('/'); //进行除法运算
result2 /= high(t);
if (k==2*t-1)
break;
}
return result2;
}
double high(int t) //括号运算及数字
{
double result3;
if( sign == '(' ) //带有括号的运算
{
match( '(' );
result3 = low(t); //递归计算表达式
match(')');
}
else if (sign >= 0 && sign<= 9) //实际的数字
{
result3=sign;
k++;
if (bracketNum==0)
{
sign = signs[(k-1)/2];//下一位是符号
}
else if (bracketNum==1)
{
if ((k==braT*2-1)&&(temp==0))
{
sign = ')';
temp=1;
k--;
}
else if (((k%2) != 0)&&(k!=braT*2-1)) //下一位是符号
sign = signs[(k-1)/2];
}
}
return result3;
}
void print() //统计结果
{
if(lan==1)
cout<<endl<<"做对"<<correct<<"道题;"<<"做错"<<wrong<<"道题";
else
cout<<endl<<"Correct questions:"<<correct<<";Wrong questions:"<<wrong;
exit(0);
}