#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; } }
标签:string,int,四则运算,find,&&,no1 From: https://www.cnblogs.com/hanxuyao/p/17826781.html