0分求助!!!

B2052 简单计算器

xibaby @ 2023-10-18 19:01:45

#include<bits/stdc++.h>
using namespace std;
int a,c; char b;
int main(){
    scanf("%d%c%d",&a,&b,&c);
    switch(b){
        case '+':printf("%d",a+c);break;
        case '-':printf("%d",a-c);break;
        case '*':printf("%d",a*c);break;
        case '/':{
            if(c==0) printf("Divided by zero!");
            else printf("%d",a/c);
            break;
        }
        default:printf("Invalid operator!");
    }
    return 0;
}

by hexuchen @ 2023-10-18 19:04:20

@xibaby 输入的顺序错了,是先输入a,c,再输入b


by zhouyk0501 @ 2023-10-18 19:05:40

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

by hexuchen @ 2023-10-18 19:06:53

@zhouyukun0501 不是别人求助你发自己的代码有p用


by xibaby @ 2023-10-18 19:07:23

@hexuchen 谢谢!


by zhouyk0501 @ 2023-10-18 19:08:41

@hexuchen 因为我不想用 switch


by zhouyk0501 @ 2023-10-18 19:10:38

@hexuchen 而且用 scanfchar 字符有时候会出问题


|