整数的四则运算(一步)
#include <bits/stdc++.h> using namespace std; string no1(string);//清除多余空格 int no2(string,string);//计算 int no3(string,string);//寻找乘除的上一个运算符号在第几位 int no4(string,string);//寻找加减乘除的下一个运算符号在第几位 int main(){ string str; getline(cin,str); str = no1(str); int s = str.length(); int s1 = str.find("+"); int s2 = str.find("-"); int s3 = str.find("*"); int s4 = str.find("/"); while((s1>0 && s1<s) || (s2>0 && s2<s) || (s3>0 && s3<s) || (s4>0 && s4<s)){ if(s1>0 && s1<s && (s1<s2 || s1>s2 && s2<0) && (s3>s || s3<0) && (s4>s || s4<0)){ str = str.replace(0,str.find("+",s1+1),to_string(no2(str,"+"))); //cout<<s1<<endl; cout<<str<<endl; }else if(s2>0 && s2<s && (s3>s || s3<0) && (s4>s || s4<0)){ str = str.replace(0,str.find("-",s2+1) || str.find("+",s2+1),to_string(no2(str,"-"))); //cout<<s2<<endl; cout<<str<<endl; }else if(s3>0 && s3<s && (s3<s4 || s3>s4 && s4<0)){ str = str.replace(no3(str,"*")+1,no4(str,"*")-2,to_string(no2(str,"*"))); //cout<<s3<<endl; cout<<str<<endl; }else if(s4>0 && s4<s){ str = str.replace(no3(str,"/")+1,no4(str,"/")-2,to_string(no2(str,"/"))); //cout<<s4<<endl; cout<<str<<endl; } s = str.length(); s1 = str.find("+"); s2 = str.find("-"); s3 = str.find("*"); s4 = str.find("/"); } return 0; } string no1(string str){ while(str.find(" ")>=0 && str.find(" ")<=str.length()){ str.replace(str.find(" "),1,""); } return str; } int no2(string c,string x){ int a = stoi(c.substr(0,c.find(x))); int b = stoi(c.substr(c.find(x)+1)); if(x=="+"){ return a+b; }else if(x=="-"){ return a-b; }else if(x=="*"){ a = stoi(c.substr(no3(c,x)+1,c.find(x))); return a*b; }else if(x=="/"){ a = stoi(c.substr(no3(c,x)+1,c.find(x))); return a/b; } } int no3(string c,string x){ int i = c.find(x); while(c[i]!='+' && c[i]!='-'){ i--; } return i; } int no4(string c,string x){ int s_ = c.size(); int s_1 = c.find("+",c.find(x)+1); int s_2 = c.find("-",c.find(x)+1); int s_3 = c.find("*",c.find(x)+1); int s_4 = c.find("/",c.find(x)+1); if(s_1>0 && s_1<s_ && (s_1<s_2 || s_1>s_2 && s_2<0) && (s_3>s_ || s_3<0) && (s_4>s_ || s_4<0)){ return s_1; }else if(s_2>0 && s_2<s_ && (s_3>s_ || s_3<0) && (s_4>s_ || s_4<0)){ return s_2; }else if(s_3>0 && s_3<s_ && (s_3<s_4 || s_3>s_4 && s_4<0)){ return s_3; }else if(s_4>0 && s_4<s_){ return s_4; }else{ return s_; } }
#include <bits/stdc++.h> using namespace std; string no1(string);//清除多余空格 int no2(string,string);//计算 int no3(string,string);//寻找乘除的上一个运算符号在第几位 int no4(string,string);//寻找加减乘除的下一个运算符号在第几位 int main(){ string str; getline(cin,str); str = no1(str); int s = str.length(); int s1 = str.find("+"); int s2 = str.find("-"); int s3 = str.find("*"); int s4 = str.find("/"); while((s1>0 && s1<s) || (s2>0 && s2<s) || (s3>0 && s3<s) || (s4>0 && s4<s)){ if(s1>0 && s1<s && (s1<s2 || s1>s2 && s2<0) && (s3>s || s3<0) && (s4>s || s4<0)){ str = str.replace(0,no4(str,"+")-no3(str,"+")-1,to_string(no2(str,"+"))); //cout<<s1<<endl; cout<<str<<endl; }else if(s2>0 && s2<s && (s3>s || s3<0) && (s4>s || s4<0)){ str = str.replace(0,no4(str,"-")-no3(str,"-")-1,to_string(no2(str,"-"))); //cout<<s2<<endl; cout<<str<<endl; }else if(s3>0 && s3<s && (s3<s4 || s3>s4 && s4<0)){ str = str.replace(no3(str,"*")+1,no4(str,"*")-no3(str,"*")-1,to_string(no2(str,"*"))); //cout<<s3<<endl; cout<<str<<endl; }else if(s4>0 && s4<s){ str = str.replace(no3(str,"/")+1,no4(str,"/")-no3(str,"/")-1,to_string(no2(str,"/"))); //cout<<s4<<endl; cout<<str<<endl; } s = str.length(); s1 = str.find("+"); s2 = str.find("-"); s3 = str.find("*"); s4 = str.find("/"); } return 0; } string no1(string str){ while(str.find(" ")>=0 && str.find(" ")<=str.length()){ str.replace(str.find(" "),1,""); } return str; } int no2(string c,string x){ int a = stoi(c.substr(0,c.find(x))); int b = stoi(c.substr(c.find(x)+1)); if(x=="+"){ return a+b; }else if(x=="-"){ return a-b; }else if(x=="*"){ a = stoi(c.substr(no3(c,x)+1,c.find(x))); return a*b; }else if(x=="/"){ a = stoi(c.substr(no3(c,x)+1,c.find(x))); return a/b; } } int no3(string c,string x){ int i = c.find(x)-1; while(c[i]!='+' && c[i]!='-'){ i--; if(i<0){ return 0; } } return i; } int no4(string c,string x){ int s_ = c.size(); int s_1 = c.find("+",c.find(x)+1); int s_2 = c.find("-",c.find(x)+1); int s_3 = c.find("*",c.find(x)+1); int s_4 = c.find("/",c.find(x)+1); if(s_1>0 && s_1<s_ && (s_1<s_2 || s_1>s_2 && s_2<0) && (s_3>s_ || s_3<0) && (s_4>s_ || s_4<0)){ //cout<<1<<endl; return s_1; }else if(s_2>0 && s_2<s_ && (s_3>s_ || s_3<0) && (s_4>s_ || s_4<0)){ //cout<<2<<endl; return s_2; }else if(s_3>0 && s_3<s_ && (s_3<s_4 || s_3>s_4 && s_4<0)){ //cout<<3<<endl; return s_3; }else if(s_4>0 && s_4<s_){ //cout<<4<<endl; return s_4; }else{ //cout<<5<<endl; return s_; } }