匿名用户
1级
2016-11-27 回答
#include
#include
#ifndef RedContritio
#include
#else
#message("This is RedContritio's energy")
#endif
#include //用到了time函数
int jia(int a,int b) {
return a+b;
}
char solutation[999] = "\0";
int jian(int a,int b) {
return a-b;
}
int cheng(int a,int b) {
return a*b;
}
float chu(int a,int b) {
return 1.0*a/b;
}
int solve(int a,int b,int c) {
if(c==1)return jia(a,b);
if(c==2)return jian(a,b);
if(c==3)return cheng(a,b);
if(c==4)return (int)chu(a,b);
}
int main() {
int a,b,c,answer;
char cmd;
srand(time(NULL)); //用时间做种,每次产生随机数不一样
printf("enter to start,-1 to quit:");
while((cmd=getchar())!='-') {
a = rand() % 10+1; //产生1-10的随机数
b= rand() % 10+1;
c= rand() %4+1;
answer=solve(a,b,c);
char ys;
switch(c) {
case 1:
ys='+';
break;
case 2:
ys='-';
break;
case 3:
ys='*';
break;
case 4:
ys='/';
break;
}
printf("%d %c %d = ?\n",a,ys,b);
int r=0,solution;
while(!r) {
printf("please input your answer.\n");
memset(solutation,0,sizeof(solutation));
scanf("%s",solutation);
solution = atoi(solutation);
if(!(solution-answer)) { //除法出现小数这里是向下取整,不是四舍五入
puts("Right!");
r=1;
} else puts("Wrong!Please try again.");
}
printf("continue?(-1 to quit):");
getchar();//处理缓冲区的多余回车
}
return 0;
}望采纳,多余的printf是为了增强交互性,不需要可删掉。另除法不是很完善,自己可规定小数精度,对代码进行修改。