四则运算

发布时间 2023-11-12 09:46:15作者: hanxuyao
#include <bits/stdc++.h>
using namespace std;
int f(string a,string b);//计算

string no1(string x);//清除空格
int main(){
    string a;
    getline(cin,a);
    a=no1(a);
    if(a.find("+")>0 && a.find("+")<a.size()){
        cout<<f(a,"+");
    }else if(a.find("-")>0 && a.find("-")<a.size()){
        cout<<f(a,"-");
    }else if(a.find("*")>0 && a.find("*")<a.size()){
        cout<<f(a,"*");
    }else if(a.find("/")>0 && a.find("/")<a.size()){
        cout<<f(a,"/");
    }
    return 0;
}
string no1(string a){
    while(a.find(" ")>=0 && a.find(" ")<a.size()){
        a.replace(a.find(" "),1,"");
    }
    return a;
}
int f(string a,string b){
    int x = stoi(a.substr(0,a.find(b)));
    int y = stoi(a.substr(a.find(b)+1));
    if(b=="+"){
        return x+y;
    }if(b=="-"){
        return x-y;
    }if(b=="*"){
        return x*y;
    }if(b=="/"){
        return x/y;
    }
}