Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int preCalc(string line) {
- Stack stack(line.length());
- for (int i = line.length() - 1; i >= 0; i--) {
- if (line[i] >= '0' and line[i] <= '9') {
- stack.push(line[i] - '0');
- }
- else {
- int x, y;
- switch (line[i])
- {
- case '+':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x + y);
- break;
- case '-':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x - y);
- break;
- case '*':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x * y);
- break;
- case '/':
- x = stack.peek();
- stack.pop();
- y = stack.peek();
- stack.pop();
- stack.push(x / y);
- break;
- default:
- break;
- }
- }
- }
- return stack.peek();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement