不知哪里写错了

B2052 简单计算器

Zyuxuan @ 2024-11-28 19:31:21

谁能给我一点提示?

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    double a,b;
    char ch;
    cin>>a>>b>>ch;
    if(ch=='+'){
        cout<<a+b;
    }
    else if(ch=='-'){
        cout<<a-b;
    }
    else if(ch=='*'){
        cout<<a*b;
    }
    else if(ch=='/'&&b!=0){
        cout<<a/b;
    }
    else if(ch=='/'&&b==0){
        cout<<"Divided by zero!";
    }
    else{
        cout<<"Invalid operator!";
    }
    return 0;
}

by xywuyu @ 2024-11-28 19:35:23

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

by xywuyu @ 2024-11-28 19:35:54

@Zyuxuan 要用int


by Zyuxuan @ 2024-11-28 19:40:00

@xywuyu 谢谢,已过!


by luozixin666 @ 2024-12-28 20:05:16

include<bits/stdc++.h>

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

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

_


|