c++RE

B2052 简单计算器

zhuangshanjbin @ 2023-05-06 16:13:49


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

}

by wangyinghao @ 2023-05-06 16:16:57

@qwer122 除法判断里的b==0要先判断


by zhuangshanjbin @ 2023-05-06 16:20:22

@wangyinghao 懂了


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

    }

}

|