Advertisement
Krassi_Daskalova

Simple Calculator

Jan 26th, 2022
882
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayDeque;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Scanner;
  5.  
  6. public class SimpleCalculator {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         ArrayDeque<String> stack = new ArrayDeque<>();
  11.         String[] symbols = scanner.nextLine().split("\\s+");
  12.         //Arrays.stream(scanner.nextLine().split(" ")).forEach(stack::push);
  13.         Collections.addAll(stack,symbols);
  14.  
  15.         while (stack.size() > 1){
  16.             int firstNum = Integer.parseInt(stack.pop());
  17.             String operator = stack.pop();
  18.             int secondNum = Integer.parseInt(stack.pop());
  19.  
  20.             switch (operator){
  21.                 case "+":
  22.                     stack.push(String.valueOf(firstNum + secondNum));
  23.                     break;
  24.                 case "-":
  25.                     stack.push(String.valueOf(firstNum - secondNum));
  26.                     break;
  27.             }
  28.             System.out.println(stack.pop());
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement