求助大佬 这样子为什么过不了啊 是不是逻辑有问题

B2052 简单计算器

Czh116489 @ 2022-08-27 20:49:59

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    char c;
    scanf("%d%d%c",&a,&b,&c);
    if(b==0&&c=='/')
    printf("Divided by zero!"); 
    switch(c)
    {
        case '+':
        printf("%d\n",a+b);
        break;
        case '_':
        printf("%d\n",a-b);
        break;
        case '*':
        printf("%d\n",a*b);
        break;
        case '/':
        printf("%d\n",a/b);
        break;
        default :
        printf("Invalid operator!");//输入错误项
        break;
    }

    return 0;
}

by Czh116489 @ 2022-08-27 21:00:02

引号那个减号那里打错了 但是还是改不了


by Burnling @ 2022-08-27 21:02:16

我猜你 c 很大概率只读到了空格符

建议 scanf("%d%d %c",&a,&b,&c);


by bamboo12345 @ 2022-08-27 21:02:23

@Czh116489 %c前面加空格,要不然会读入不可见字符


by bamboo12345 @ 2022-08-27 21:02:46

@Burnling 但好像我交了一发还是不对


by bamboo12345 @ 2022-08-27 21:04:20

好像改完后RE了


by Burnling @ 2022-08-27 21:05:53

@bamboo123 输出 Divided by zero! 后应该直接 return 0


by bamboo12345 @ 2022-08-27 21:06:38

@Czh116489 你判断除数为零后直接return 0就好了,要不然他会继续运行下面的程序,还是会RE


by Czh116489 @ 2022-08-27 21:07:59

感谢换了一个代码 加了空格可以了


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    char c;
    scanf("%d%d %c",&a,&b,&c);
    switch(c)
    {
        case '+':
        printf("%d\n",a+b);
        break;
        case '-':
        printf("%d\n",a-b);
        break;
        case '*':
        printf("%d\n",a*b);
        break;
        case '/':
        if(b!=0)
        printf("%d\n",a/b);
        else
        printf("Divided by zero!");
        break;
        default :
        printf("Invalid operator!");//输入错误项
        break;
    }

    return 0;
}

by Czh116489 @ 2022-08-27 21:08:53

@bamboo123 我也发现逻辑出问题了干谢


|