Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ArrayDeque<String> calcStack = new ArrayDeque<>();
- String[] numbers = sc.nextLine().split("\\s+");
- Collections.addAll(calcStack, numbers);
- while(calcStack.size() > 1) {
- int firstNum = Integer.parseInt(calcStack.pop());
- String operator = calcStack.pop();
- int secondNum = Integer.parseInt(calcStack.pop());
- if (operator.equals("+")) {
- calcStack.push(String.valueOf(firstNum + secondNum));
- } else {
- calcStack.push(String.valueOf(firstNum - secondNum));
- }
- }
- System.out.print(calcStack.peek());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement