5 solutions
-
-1
#include<bits/stdc++.h> using namespace std; stack<int> stk1; stack<char> stk2; void cal() { int b=stk1.top();stk1.pop(); int a=stk1.top();stk1.pop(); char ch=stk2.top();stk2.pop(); if(ch=='+') { stk1.push(a+b); } if(ch=='-') { stk1.push(a-b); } if(ch=='*') { stk1.push(a*b); } if(ch=='/') { stk1.push(a/b); } if(ch=='^') { int t=1; for(int i=1;i<=b;i++) { t=t*a; } stk1.push(t); } } int main() { string str; cin>>str; map<char,int> h={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}}; int n=str.size(); for(int i=0;i<n;i++) { if(str[i]=='@') { break; } if(str[i]>='0'&&str[i]<='9') { int t=0; while(str[i]>='0'&&str[i]<='9') { t=t*10+str[i]-'0'; i++; } stk1.push(t); i--; continue; } if(str[i]=='(') { stk2.push('('); continue; } if(str[i]==')') { while(stk2.top()!='(') { cal(); } stk2.pop(); continue; } while(stk2.size()&&h[str[i]]<=h[stk2.top()]) { cal(); } stk2.push(str[i]); } while(stk2.size()) { cal(); } cout<<stk1.top(); return 0; }
Information
- ID
- 171
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 78
- Accepted
- 14
- Uploaded By