IaLWH @ 2021-11-29 21:17:37
#include<stdio.h>
int main(){
int a,b,s;
scanf("%d%d",&a,&b);
switch(getchar()){
case '+':
s=a+b;
break;
case '-':
s=a-b;
break;
case '*':
s=a*b;
break;
case '/':
if(b)s=a/b;
else{
printf("Divided by zero!");
return 0;
}
break;
default:
printf("Invalid operator!");
return 0;
}
printf("%d",s);
return 0;
}
好家伙,想AC这个大淼题,结果出现玄学错误:
输入2 4 *
结果给我输出Invalid operator!
我是怎么了?连这种题也切不动了吗?!
by AFwhcing @ 2021-11-29 21:20:15
空格qi的苦……(不确信)
scanf("%d%d",&a,&b);
改成
scanf("%d %d ",&a,&b);
阔以试试
by IaLWH @ 2021-11-29 21:21:25
@蒟蒻de小王 已试当然不可以
by Heliox @ 2021-11-29 21:21:58
@PluckDuck switch里的getchar在实际输入的时候其实是空格,所以前面要再加一个getchar,先把空格读进去
by Heliox @ 2021-11-29 21:23:04
不是scanf的问题
by IaLWH @ 2021-11-29 21:23:11
@Cilivan 哦shit谢谢
by Heliox @ 2021-11-29 21:23:42
@PluckDuck 莫事莫事
by IaLWH @ 2021-11-29 21:23:51
竟然AC了
by AFwhcing @ 2021-11-29 21:25:28
@Cilivan +1
by AFwhcing @ 2021-11-29 21:26:25
@Cilivan 我用scanf把它吞了
by ScottSuperb @ 2021-11-29 21:44:39
直接char c; scanf("%d%d%c", &a, &b, &c);
他不香吗