救命!!!!!!

B2052 简单计算器

duankh @ 2023-11-26 15:19:09

#include<iostream>
using namespace std;
int main(){
    int a,b;
    char c;
    cin>>a,b,c;
    switch(c)
    {
    case '+' :cout<<a<<c<<b<<"="<<a+b; break;
    case '-' :cout<<a<<c<<b<<"="<<a-b; break;
    case '*' :cout<<a<<c<<b<<"="<<a*b; break;
    case '/' :if(b!=0){
                cout<<a<<c<<b<<"="<<a+b; break;
               }
               else{
                cout<<"Divided by zero!";break;
               }
    default : cout<<"Invalid operator!"; break; 
    }
    return 0;
} 

by duankh @ 2023-11-26 15:22:22

我知道错误了!!


by Misty7 @ 2023-11-26 15:22:53

@duankh 比如输入 1 + 2 ,不需要输出1 + 2 = 3,直接输出3


by weirdo_shao @ 2023-11-26 15:24:14

Ac代码:

#include<bits/stdc++.h> 
using namespace std;
int main()
{

    int a,b;
    char c;
    cin>>a>>b>>c;
    switch(c)
    {
    case '+':cout<<a+b;break;
    case '-':cout<<a-b;break;
    case '*':cout<<a*b;break;
    case '/':if (b!=0)
                 cout<<a/b;
             else
                 cout<<"Divided by zero!";
            break;
    default:cout<<"Invalid operator!";
    }   
}

by Rb3f @ 2024-06-22 15:38:44

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    char c;
    cin>>a>>b;
    cin>>c;
    if(b==0&&c=='/'){
        cout<<"Divided by zero!"<<endl;
        return 0;
    }
    if(c=='+')
        cout<<a+b<<endl;
    else if(c=='-')
        cout<<a-b<<endl;
    else if(c=='*')
        cout<<a*b<<endl;
    else if(c=='/')
        cout<<a/b<<endl;
    else
        cout<<"Invalid operator!"<<endl;
    return 0;
}

by joe_001 @ 2024-08-08 16:06:31

#include<bits/stdc++.h>
using namespace std;
long long x,y,k;
char z;
int main()
{   
    cin>>x>>y>>z;
    if(y==0 && z=='/')
    {
        cout<<"Divided by zero!"<<endl;
        return 0;
    }
    if(z!='+' && z!='-' && z!='*' && z!='/')
    {
        cout<<"Invalid operator!"<<endl;
        return 0;
    }
    if(z=='+')
        cout<<x+y;
    if(z=='-')
        cout<<x-y;
    if(z=='*')
        cout<<x*y;
    if(z=='/')
        cout<<x/y;
    return 0;
}

by luogu_00 @ 2024-08-29 12:18:16

代码

#include<iostream>
using namespace std;
int main(){
  int a,b;
  char c;
  cin>>a,b,c;
  switch(c)
  {
  case '+' :cout<<a<<c<<b<<"="<<a+b; break;
  case '-' :cout<<a<<c<<b<<"="<<a-b; break;
  case '*' :cout<<a<<c<<b<<"="<<a*b; break;
  case '/' :if(b!=0){
              cout<<a<<c<<b<<"="<<a+b; break;
         }
             else{
              cout<<"Divided by zero!";break;
             }
  default : cout<<"Invalid operator!"; break;
  }
  return 0;
}

代码分析

这段代码里面有5个缺陷:

  1. cin>>a,b,c应该是cin>>a>>b>>c
  2. case应该再加一个缩进,所有的case都是。
  3. 题目要求的是输出计算结果而不是输出一个完整的表达式以及值。
  4. if(b!=0)下面的a+b应该是a/b,因为一个doubleint是自动趋于0的。
  5. if...else下面的cout应该再加3个空格。

    正确代码

    #include<iostream>
    using namespace std;
    int main(){
    int a,b;
    char c;
    cin>>a>>b>>c;
    switch(c)
    {
    case '+' :cout<<a+b; break;
    case '-' :cout<<a-b; break;
    case '*' :cout<<a*b; break;
    case '/' :if(b!=0){
                cout<<a/b; break;
              }
              else{
                cout<<"Divided by zero!";break;
              }
    default : cout<<"Invalid operator!"; break;
    }
    return 0;
    }

    警告

    代码中一个缩进为2个空格,打代码时请用一个Tab代替2个空格,否则可能无法AC


|