package net.megaroid.expr;

import java.util.Scanner;
import net.megaroid.collections.Stack;
import net.megaroid.math.Rational;

public class PostfixCalculator {
	
	private Stack<Rational> stack = new Stack<>();
	
	private static boolean isOperator(char op) {
		return op == '+' || op == '-' || op == '*' || op == '/';
	}
	
	public void putTerm(String term) {
		if (Character.isDigit(term.charAt(0))) {
			int n = Integer.parseInt(term);
			stack.push(new Rational(n));
		} else if (isOperator(term.charAt(0))) {
			Rational r2 = stack.pop();
			Rational r1 = stack.pop();
			
			switch (term.charAt(0)) {
			case '+':
				stack.push(r1.add(r2));
				break;
			case '-':
				stack.push(r1.subtract(r2));
				break;
			case '*':
				stack.push(r1.multiply(r2));
				break;
			case '/':
				stack.push(r1.divide(r2));
			}
		}
	}
	
	public boolean isComplete() {
		return stack.size() == 1;
	}
	
	public Rational getAnswer() {
		return stack.pop();
	}
	
	public String toString() {
		return "Stack >> " + stack;
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input a postfix expression. '.' or '=' for exit.\n> ");
		PostfixCalculator calc = new PostfixCalculator();
		String term;
		while (true) {
			term = scanner.next();
			if (term.charAt(0) == '.' || term.charAt(0) == '=') {
				break;
			}
			
			calc.putTerm(term);
			System.out.println(calc.toString());
		}
		scanner.close();
		
		if (!calc.isComplete()) {
			throw new RuntimeException("Invalid Postfix Expression");
		}
		
		System.out.println("The answer is " + calc.getAnswer());
	}
}

@@ -396,16 +396,69 @@
 (0))) %7B%0A
+%09%09%09int pos = term.indexOf(%22.%22);%0A%09%09%09if (pos == -1) %7B%0A%09
 %09%09%09int n
@@ -484,16 +484,17 @@
 (term);%0A
+%09
 %09%09%09stack
@@ -517,16 +517,302 @@
 al(n));%0A
+%09%09%09%7D else %7B%0A%09%09%09%09int n1 = Integer.parseInt(term.substring(0, pos));%0A%09%09%09%09int n2 = Integer.parseInt(term.substring(pos + 1));%0A%09%09%09%09int length = term.length();%0A%09%09%09%09int m = 1;%0A%09%09%09%09for (int i = pos + 1; i %3C length; i++) %7B%0A%09%09%09%09%09m *= 10;%0A%09%09%09%09%7D%0A%09%09%09%09stack.push(new Rational(n1 * m + n2, m));%0A%09%09%09%7D%0A
 %09%09%7D else

Convert xx.xx to n/m