求助

B2052 简单计算器

YYBS @ 2025-01-04 09:09:08


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

by syt_cout @ 2025-01-04 09:11:07

你需要先判断除数是否为 0


by guxiaoqing @ 2025-01-04 10:03:46

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

    }

    else if (e!='+' && e!='-' && e!='*' && e!='/'){
        cout<<"Invalid operator!"; 
    } 
    return 0;
}

@YYBS


by guxiaoqing @ 2025-01-04 10:04:22

~注意文明用语~ 求关


by YHX_50537 @ 2025-01-09 13:19:16

先判断除数是否为0,如果不是再输出结果。


|