Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Stack;
- public class Test {
- public static void main(String[] args) {
- Stack<String> stack = new Stack<>();
- int result = 0;
- String inputString = "5 7 + 3 - 5 +";
- List<String> operators = Arrays.asList("+", "-");
- String[] params = inputString.split(" ");
- for (String op: params) {
- if (operators.contains(op)) {
- if (stack.size() == 2) {
- result += Integer.parseInt(stack.pop());
- }
- int second = Integer.parseInt(stack.pop());
- if (op.equals("+")) {
- result += second;
- }
- else if(op.equals("-")) {
- result -= second;
- }
- }
- else {
- stack.push(op);
- }
- }
- System.out.println("Result: " + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement