中易网

C语言如何实现判断用户输入的算式结果正确?

答案:4  悬赏:20  
解决时间 2021-01-12 14:14
  • 提问者网友:末路
  • 2021-01-11 15:00
C语言如何实现判断用户输入的算式结果正确?
最佳答案
  • 二级知识专家网友:持酒劝斜阳
  • 2021-01-11 16:12
1234567891011121314151617181920212223242526#include#include#includevoidfun(){inta,b,c,s,s1;srand(time(NULL));a=rand()%9+1;b=rand()%9+1;c=rand()%4+1;switch(c){case1:s=a+b;printf("%d+%d=",a,b);break;case2:s=a-b;printf("%d-%d=",a,b);break;case3:s=a*b;printf("%d*%d=",a,b);break;case4:s=a;printf("%d/%d=",a*b,b);break;}scanf("%d",&s1);if(s==s1)printf("回答正确!\n");elseprintf("回答错误!答案是:%d\n",s);}intmain(){while(1)fun();return0;}
全部回答
  • 1楼网友:白昼之月
  • 2021-01-11 19:05
正则表达式吧
  • 2楼网友:荒野風
  • 2021-01-11 18:28
将用户输入的算式存在字符串中,然后将字符串中的字符转换成对应的数字和符号,自己算一遍。如果对,则告诉用户对,如果错,则告诉用户错。
  • 3楼网友:走死在岁月里
  • 2021-01-11 17:34
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
#include
#include
#include
void fun()
{
int a,b,c,s,s1;
srand(time(NULL));
a=rand()%9+1;
b=rand()%9+1;
c=rand()%4+1;
switch(c)
{
case 1:s=a+b;printf("%d+%d=",a,b);break;
case 2:s=a-b;printf("%d-%d=",a,b);break;
case 3:s=a*b;printf("%d*%d=",a,b);break;
case 4:s=a;printf("%d/%d=",a*b,b);break;
}
scanf("%d",&s1);
if(s==s1)printf("回答正确!\n");
else printf("回答错误!答案是: %d\n",s);
}
int main()
{
while(1)fun();
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息