Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import exercise.*;
- import java.util.Scanner;
- import java.util.Stack;
- public class RPN {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- Stack<Rational> stack = new Stack<Rational>();
- while (scanner.hasNext()) {
- String s = scanner.next();
- if (s == "+") stack.push(Rational.addition(stack.pop(),stack.pop()));
- else if (s == "*") stack.push(Rational.multiplikation(stack.pop(),stack.pop()));
- else if (s.== "/") stack.push(Rational.division(stack.pop(),stack.pop()));
- else if (s == "-") stack.push(Rational.subtraktion(stack.pop(),stack.pop()));
- else stack.push(new Rational(Long.parseLong(s)));
- }
- System.out.println(stack.pop());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement