deyanmalinov

Simple Calculator 2

Mar 21st, 2020
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayDeque;
  2. import java.util.Collection;
  3. import java.util.Collections;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         //2 + 5 + 10 - 2 - 1
  9.         Scanner scan = new Scanner(System.in);
  10.         String[] line = scan.nextLine().split(" ");
  11.         ArrayDeque<String> stack = new ArrayDeque<>();
  12.         //Collections.addAll(stack, line);
  13.         for (int i = line.length-1; i > -1; i--) {
  14.             stack.push(line[i]);
  15.         }
  16.        while (stack.size()>1){
  17.            int a = Integer.parseInt(stack.pop());
  18.            String znak = stack.pop();
  19.            int b = Integer.parseInt(stack.pop());
  20.            if (znak.equals("+")){
  21.                stack.push(""+(a+b));
  22.            }else {
  23.                stack.push(""+(a-b));
  24.            }
  25.          }
  26.         for (String s : stack) {
  27.             System.out.println(s);
  28.         }
  29.     }
  30. }
RAW Paste Data