C语言RE

B2052 简单计算器

ABtianCD @ 2023-04-01 10:47:31

#include<stdio.h>
int main()
{
    int x,y;
    char c;
    scanf("%d%d %c",&x,&y,&c);
    switch (c){
    case '+':printf("%d",x+y);break;
    case '-':printf("%d",x-y);break;
    case '*':printf("%d",x*y);break;
    case '/':
        if(y==0) printf("Divided by zero!");
    printf("%d",x/y);break;
    default:printf("Invalid operator!");
    }
    return 0;
}

在自己的编译器可以运行,提交会RE,应该怎么处理,谢谢


by Terrible @ 2023-04-01 10:59:56

@ABtianCD if(y==0)... else ...

This case, the division will still be called, which can cause a Floating-point exception of dividing by zero.

你这样写还是会执行除法,会产生除零的浮点错误


by ABtianCD @ 2023-04-02 10:30:02

@Terrible 哦对,应该加上else,懂了,谢谢


|