#include <bits/stdc++.h> using namespace std; int main(){ string s; getline(cin,s); char x; int a,a1,s1 = s.size(); bool have = true; for(int i = 0;i<s1;i++){ if((s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/') && have){ x = s[i]; a = stoi(s.substr(0,i)); s = s.substr(i+1); have = false; } if(have){ continue; } int j = 0; while(1){ if(s[j]=='+' || s[j]=='-' || s[j]=='*' || s[j]=='/' || j>=s.size()-1){ break; }else{ j++; } } a1 = stoi(s.substr(0,j+1)); if(x=='+'){ a += a1; }else if(x=='-'){ a -= a1; }else if(x=='*'){ a *= a1; }else if(x=='/'){ a /= a1; } x = s[j]; if(s.size()<=j+1){ break; } s = s.substr(j+1); } cout<<a; return 0; }
标签:int,else,a1,依次,算数,从左往右,size From: https://www.cnblogs.com/wangyihang-xh/p/18024136