Advertisement
Guest User

Untitled

a guest
May 25th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import exercise.*;
  2. import java.util.Scanner;
  3. import java.util.Stack;
  4.  
  5. public class RPN {
  6.     public static void main(String[] args){
  7.     Scanner scanner = new Scanner(System.in);
  8.    
  9.     Stack<Rational> stack = new Stack<Rational>();
  10.  
  11.         while (scanner.hasNext()) {
  12.             String s = scanner.next();
  13.          if      (s == "+") stack.push(Rational.addition(stack.pop(),stack.pop()));
  14.         else if (s == "*") stack.push(Rational.multiplikation(stack.pop(),stack.pop()));
  15.     else if (s.== "/") stack.push(Rational.division(stack.pop(),stack.pop()));
  16.     else if (s == "-") stack.push(Rational.subtraktion(stack.pop(),stack.pop()));
  17.         else stack.push(new Rational(Long.parseLong(s)));
  18.         }
  19.         System.out.println(stack.pop());
  20.        
  21.     }  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement