Advertisement
deyanmalinov

02. Simple Calculator

May 8th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.     public static void main(String[] args){
  7.         Scanner scan = new Scanner(System.in);
  8.         String[] line = scan.nextLine().split(" ");
  9.         Deque<String> stack = new ArrayDeque<>();
  10.         Collections.addAll(stack, line);
  11.  
  12.         while (stack.size() > 1){
  13.             int first = Integer.parseInt(stack.pop());
  14.             String sign = stack.pop();
  15.             int second = Integer.parseInt(stack.pop());
  16.  
  17.             switch (sign){
  18.                 case "+":
  19.                     stack.push(String.valueOf(first + second));
  20.                     break;
  21.                 case "-":
  22.                     stack.push(String.valueOf(first-second));
  23.                     break;
  24.             }
  25.         }
  26.         System.out.println(stack.pop());
  27.         }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement