Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- string str = "* - 5 8 7"; // (5-8)*7
- stack<int>st1;
- stack<char>st2;
- for(int i=str.size()-1;i>=0;--i){
- if(str[i] == '*')st2.push('*'); else
- if(str[i] == '+')st2.push('+'); else
- if(str[i] == '-')st2.push('-'); else
- { //если число
- if(str[i] != ' ')
- st1.push(str[i] - '0');
- }
- if (st2.size()>0 && st1.size() >= 2){
- int a = st1.top(); st1.pop();
- int b = st1.top(); st1.pop();
- char c = st2.top(); st2.pop();
- if(c == '+') st1.push(a+b);
- if(c == '-') st1.push(a-b);
- if(c == '*') st1.push(a*b);
- }
- }
- int ans = st1.top(); st1.pop();
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement