package oving3;

import java.util.Stack;

public class RPNCalc {

	private Stack<Double> stack = new Stack<>();

	RPNCalc() {
	}

	void push(double n) {
		stack.push(n);
	}

	double pop() {
		if (stack.isEmpty()) {
			return Double.NaN;
		}
		return stack.pop();
	}

	double peek(int i) {
		int id = stack.size() - i;
		if (id < 0 || id >= stack.size()) {
			return Double.NaN;
		}
		return stack.get(id);
	}

	int getSize() {
		return stack.size();
	}

	void performOperation(char c) {
		if (stack.size() <= 1) {
			return;
		}
		double a = stack.pop();
		double b = stack.pop();
		switch (c) {
			case '+':
				stack.push(a + b);
				break;
			case '-':
				stack.push(a - b);
				break;
			case '*':
				stack.push(a * b);
				break;
			case '/':
				stack.push(a / b);
				break;
			default:
				throw new IllegalArgumentException("Invalid operation");
		}
	}

	@Override
	public String toString() {
		return stack.toString();
	}
}
