Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <fstream>
- using namespace std;
- struct Stack
- {
- vector<int> st;
- int size;
- Stack(int n)
- {
- size = 0;
- st.resize(n);
- }
- void push(int value)
- {
- st[++size] = value;
- }
- int pop()
- {
- return st[size--];
- }
- };
- int res(int a, char op, int b)
- {
- if (op == '+') return a + b;
- if (op == '-') return a - b;
- if (op == '*') return a * b;
- }
- int main()
- {
- ifstream fin("postfix.in");
- ofstream fout("postfix.out");
- string str;
- getline(fin, str);
- Stack s(str.size());
- for(int i = 0;i<str.length();i++)
- {
- if (str[i] == ' ') continue;
- if (isdigit(str[i]))
- {
- s.push(str[i] - '0');
- }
- else
- {
- s.push(res(s.pop(), str[i], s.pop()));
- }
- }
- fout << s.pop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement