问题标题: 整数计算器

1
0
已解决
朱一鸣
朱一鸣
新手光能
新手光能

#include<iostream>
#include<cmath>
using namespace std;
long long a,b;
char c;
bool flag;
string s;
int main(){
    while(s!=" "){
        a=0;
        b=0;
        flag=false;
        cout<<"输入算式,不用输入等号:";
        getline(cin,s);
        if(s==" "){
            break;
        }
        for(int i=0;i<(int)s.size();i++){
            if(s[i]>='0'&&s[i]<='9'&&flag==false){
                a=a*10+(s[i]-'0');
            }
            if(s[i]>='0'&&s[i]<='9'&&flag==true){
                b=b*10+(s[i]-'0');
            }
            if(!(s[i]>='0'&&s[i]<='9')){
                flag=true;
                c=s[i];
            }
        }
        cout<<a<<c<<b<<"=";
        if(c=='+'){
            printf("%lld\n",a+b);
        }else if(c=='-'){
            printf("%lld\n",a-b);
        }else if(c=='*'){
            printf("%lld\n",a*b);
        }else{
            printf("%.2f\n",a*1.0/b);
        }
    }
    return 0;
}


1
1
我要回答