Compile Error 编译失败 !!!C++

B2052 简单计算器

MWSFirestar @ 2024-12-14 21:02:02

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

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

by Lipearc @ 2024-12-14 21:04:45

@MWSFirestar 这还不简单:

#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<<"Divided by zero!";
            }

            else{
                cout<<a/b;
            }
            break;
        default:
            cout<<"Invalid operator!";
    }

return 0;
}

by xxboyxx @ 2024-12-14 21:12:56

@MWSFirestar 显然你没有打分号


by MWSFirestar @ 2024-12-15 12:47:47

编译失败 !!!C++

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

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

by LiJinwen20130617 @ 2024-12-24 13:21:31

@MWSFirestar 这样就可以了,s一开始不用赋值,还有输入的顺序是abs不是asb

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

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

by MWSFirestar @ 2025-01-05 13:03:05

@LiJinwen20130617 谢谢了


by YHX_50537 @ 2025-01-09 13:13:25

第五行有一个单引号是中文的


|