有大佬看看为什么不对吗

B2052 简单计算器

Retribution321 @ 2024-10-22 16:47:50

(我有分行以及缺位对齐处理,但不知道为啥0分)


#include <bits/stdc++.h>  
using namespace std;

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

by bsdsdb @ 2024-10-22 16:52:58

@Retribution321 最后一个if判断反了


by luohoujunyang @ 2024-10-22 16:53:02

@Retribution321

if ((c == '+' && c == '-') && (c == '*' && c == '/'))改为if ((c != '+' && c != '-') && (c != '*' && c != '/'))


by Retribution321 @ 2024-10-22 16:57:11

好的,谢谢!


by Li18519195979 @ 2024-12-08 15:53:29

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<<ab<<endl; else if(c=='/') cout<<a/b<<endl; else cout<<"Invalid operator!"<<endl; return 0; }

#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;
}

|