Advertisement
vov44k

Untitled

Dec 4th, 2022
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class Main {
  6.  
  7.     static PrintWriter pw;
  8.  
  9.     public static void main(String[] args) throws FileNotFoundException {
  10.         Scanner in = new Scanner(new File("input.txt"));
  11.         pw = new PrintWriter(new File("output.txt"));
  12.  
  13.  
  14.         Stack<Integer> stack = new Stack<>();
  15.  
  16.         while (in.hasNext()) {
  17.             String a = in.next();
  18.             switch (a){
  19.                 case "*":
  20.                     stack.push(stack.pop() * stack.pop());
  21.                     break;
  22.                 case "+":
  23.                     stack.push(stack.pop() + stack.pop());
  24.                     break;
  25.                 case "-":
  26.                     stack.push(-stack.pop() + stack.pop());
  27.                     break;
  28.                 default:
  29.                     stack.push(Integer.parseInt(a));
  30.             }
  31.         }
  32.         pw.println(stack.pop());
  33.         pw.close();
  34.  
  35.     }
  36.  
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement