Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void solution(){
- stack<char> stk;
- string s;
- cin>>s;
- string up="*/",ni="-+";
- vector<string>res;
- forx(i,0,isz(s)){
- string u="";
- if (isdigit(s[i])){
- while (isdigit(s[i])){
- u+=s[i];
- ++i;
- }
- --i;
- res.push_back(u);
- }
- else if(s[i]=='(')
- stk.push(s[i]);
- else if(s[i]==')'){
- while(stk.top()!='(' && isz(stk)>0){
- u="";
- u+=stk.top();
- res.push_back(u);
- stk.pop();
- }
- if(isz(stk)>0)
- stk.pop();
- }
- else{
- if(isz(stk)==0 || up.find(s[i])!=string::npos && ni.find(stk.top())!=string::npos)
- stk.push(s[i]);
- else{
- while(isz(stk)>0 && (up.find(stk.top())!=string::npos && ni.find(s[i])!=string::npos || up.find(stk.top())!=string::npos && up.find(s[i])!=string::npos || \
- ni.find(stk.top())!=string::npos && ni.find(s[i])!=string::npos)){
- u="";
- u+=stk.top();
- res.push_back(u);
- stk.pop();
- }
- stk.push(s[i]);
- }
- }
- }
- string u;
- while(isz(stk)>0){
- u="";
- u+=stk.top();
- res.push_back(u);
- stk.pop();
- }
- stack<int>stk2;
- for(auto& a:res){
- if(up.find(a)==string::npos && ni.find(a)==string::npos)
- stk2.push(stoi(a));
- else{
- int x=stk2.top();
- stk2.pop();
- int y=stk2.top();
- stk2.pop();
- if(a=="+")
- stk2.push(x+y);
- if(a=="-")
- stk2.push(x-y);
- if(a=="*")
- stk2.push(x*y);
- if(a=="/")
- stk2.push(x/y);
- }
- }
- cout<<stk2.top();
- }
Add Comment
Please, Sign In to add comment