#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int sum = 0;
int rr = 0;
int mulu()
{
puts("请选择难度:\n");
puts("1.10以内\n") ;
puts("2.100以内\n");
puts("3.1000以内\n");
int h;
scanf("%d",&h);int flag = 1;
while(flag)
{
switch(h)
{
case 1:flag = 0;return 10;
case 2:flag = 0;return 100;
case 3:flag = 0;return 1000;
default:printf("请重新输入\n");flag = 1;break;
}
}
}
void ti()
{
puts("请输入题量:");
scanf("%d",&sum);
puts("ok\n");
}
void fushu()
{
ti();
int flag = mulu();
for(int i = rr;i < rr + sum;++i)
{
int g1 = -(rand()%flag);
int g2 = -(rand()%flag);
int h = rand()%4;int ans;double hh;
switch(h)
{
case 0:printf("%d+%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1+g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1+g2);
}
break;
case 1:printf("%d-%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1-g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1-g2);
}
break;
case 2:printf("%d*%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1*g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1*g2);
}
break;
case 3:printf("%d/%d= ",g1,g2);puts("请输出答案:");scanf("%lf",&hh);
if(hh==g1/g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1/g2);
}
break;
default:break;
}
}
}
void xiaoshu()
{
ti();
int flag = mulu();
for(int i = rr;i < rr + sum;++i)
{
int h5 = rand()%flag+1;
int h6 = rand()%flag;
float h3 = rand()%100;
float h4 = rand()%100;
float g1 = h5+h3/100.0;
float g2 = h6+h4/100.0;
int h = rand()%4;float ans;float hh;
switch(h)
{
case 0:printf("%f+%f= ",g1,g2);puts("请输出答案:");scanf("%f",&ans);
if(ans==g1+g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%f\n",g1+g2);
}
break;
case 1:printf("%f-%f= ",g1,g2);puts("请输出答案:");scanf("%f",&ans);
if(ans==g1-g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%f\n",g1-g2);
}
break;
case 2:printf("%f*%f= ",g1,g2);puts("请输出答案:");scanf("%f",&ans);
if(ans==g1*g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%f\n",g1*g2);
}
break;
case 3:printf("%f/%f= ",g1,g2);puts("请输出答案:");scanf("%f",&hh);
if(hh==g1/g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%f\n",g1/g2);
}
break;
default:break;
}
}
}
string s ;
int cnt = 0 ;
int term() ;
int factor() ;
int expression(){
int t ;
t = term() ;
while(1){
if(s[cnt] == '+' ){ // 判断为+ 继续
cnt++ ; // 下一位
t = t + term() ; // 进去term计算数值
}else if(s[cnt] == '-'){
cnt++ ;
t = t - term() ;
}else break ;
}
return t ;
}
int term() {
int f;
f = factor(); // 进入 factor 函数
while(1){
if(s[cnt]=='*') { // 判断为乘除则进入factor计算数值
cnt++;
f = f * factor();
} else if(s[cnt]=='/') {
cnt++;
f = f / factor();
} else break;
}
return f;
}
int factor() {
int f;
if(s[cnt]=='(') { // 如果判断为括号则回到 expression 中相当从新开始
cnt++ ;
f = expression();
if(s[cnt]==')') cnt++;
return f;
}
f=0;
while('0'<=s[cnt] && s[cnt]<='9') { //如果是数字就计算这个数字是多少返回到上一个函数
f=f*10+(s[cnt]-'0');
cnt++;
}
return f;
}
void daikuohao()
{
ti();
int flag = mulu();
int g1 = rand()%flag;
int g2 = rand()%flag;
int g3 = rand()%flag;
int g4 = rand()%flag;
s+="(";
stringstream aa;
aa << g1;
string h1;
aa >> h1;
s += h1;
int hh = rand()%7;
switch(hh)
{
case 0:s+="+";break;
case 1:s+="-";break;
case 2:s+="*";break;
case 3:break;
case 4:s+=")+(";break;
case 5:s+=")*(";break;
case 6:s+=")-(";break;
}
stringstream aa1;
aa1 << g2;
aa1 >> h1;
s += h1;
hh = rand()%4;
switch(hh)
{
case 0:s+="+";break;
case 1:s+="-";break;
case 2:s+="*";break;
case 3:break;
}
stringstream aa2;
aa2 << g3;
aa2 >> h1;
s += h1;
hh = rand()%4;
switch(hh)
{
case 0:s+="+";break;
case 1:s+="-";break;
case 2:s+="*";break;
case 3:break;
}
stringstream aa3;
aa3 << g4;
aa3 >> h1;
s += h1;
s += ")";
cout<<s<<" ";
cout<<"请输出答案:";
int ans;
cin>>ans;
int aaa = expression();
if(ans == aaa)
{
printf("答案正确\n");
}
else{
printf("错误,正确答案是:");
cout<<aaa<<endl;
}
Sleep(1000);
}
void putong()
{
ti();
int flag = mulu();
for(int i = rr;i < rr + sum;++i)
{
int g1 = rand()%flag;
int g2 = rand()%flag;
int h = rand()%4;int ans;double hh;
switch(h)
{
case 0:printf("%d+%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1+g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1+g2);
}
break;
case 1:printf("%d-%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1-g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1-g2);
}
break;
case 2:printf("%d*%d= ",g1,g2);puts("请输出答案:");scanf("%d",&ans);
if(ans==g1*g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1*g2);
}
break;
case 3:printf("%d/%d= ",g1,g2);puts("请输出答案:");scanf("%lf",&hh);
if(hh==g1/g2)
{
printf("答案正确\n");
}
else{
printf("错误\n");
printf("正确答案是%d\n",g1/g2);
}
break;
default:break;
}
}
}
int main()
{
//freopen("1.txt","r",stdin);
//freopen("2.txt","w",stdout);
system("color A");
while(1)
{
system("cls");
int n;
puts(" 四则运算自测软件\n");
puts("请选择选项:\n");
puts("1.负数\n");
puts("2.小数\n");
puts("3.带括号\n");
puts("4.普通\n");
puts("5.退出\n");
scanf("%d",&n);
switch(n)
{
case 1:fushu();break;
case 2:xiaoshu();break;
case 3:daikuohao();break;
case 4:putong();break;
default:return 0;
}
}
return 0;
}