c语言求助

B2052 简单计算器

Ma1234567890 @ 2024-10-19 22:28:03

int main()
{
    int a, b;
    char c;
    scanf("%d%d%c", &a, &b, &c);
    if (c == ' / ' && b == 0)
    {
        printf("Divided by zero!");
        return 0;
    }
    if (c == '+')
        printf("%d", a + b);
    else if (c == '-')
        printf("%d", a - b);
    else if (c == '*')
        printf("%d", a * b);
    else if (c == '/')
        printf("%d", a / b);
    else
        printf("Invalid operator!");
    return 0;
}

by ChenHaoQi @ 2024-10-19 22:41:39

@Ma1234567890 RE吗


by Ma1234567890 @ 2024-10-19 22:45:10

@ChenHaoQi WA


by ChenHaoQi @ 2024-10-20 08:19:52

@Ma1234567890 哪几个WA?


by Ma1234567890 @ 2024-10-20 11:00:01

@ChenHaoQi![]("C:\Users\Lenovo\Pictures\Screenshots\屏幕截图 2024-10-20 105816.png")


by Ma1234567890 @ 2024-10-20 11:00:57

@ChenHaoQi 全题就一个WA


by 陈小希2013 @ 2024-10-20 12:36:28

给你参考一下


by 陈小希2013 @ 2024-10-20 12:40:46

为啥发不了代码?


by xxwgdy @ 2024-11-02 18:05:18



    #include<stdio.h>
      int main()
     { 
              char c;
      int a,b;
   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;
     case'%':if(b!=0) printf("%d\n",a%b);
        else printf("Divided by zero!");
        break;
     default:printf("Invalid operator!\n");
   }
   return 0;

by xxwgdy @ 2024-11-02 18:05:54

@xxwgdy 可以用swicth选择语句去做


by xxwgdy @ 2024-11-02 18:06:58

@xxwgdy 最后面漏了一个},抱歉


|