import java.io.*;

class Calculator {

    private static final int STACK_SIZE = 20;

    private double[] stack = new double[STACK_SIZE];
    private int stackp = 0;

    int depth() {
	return stackp;
    }

    void push(double val) throws CalculatorException {
	if (stackp >= stack.length)
	    throw new CalculatorException("stack full");
	stack[stackp++] = val;
    }

    double pop() throws CalculatorException {
	if (stackp <= 0)
	    throw new CalculatorException("pop from empty stack");

	return stack[--stackp];
    }

    double peek(int levels) throws CalculatorException {
	if (levels <= 0)
	    throw new IllegalArgumentException(String.valueOf(levels));

	if (levels > stackp)
	    throw new CalculatorException("peek " + levels +
		" beyond stack depth " + stackp);

	return stack[stackp - levels];
    }

    // verify at least given number of arguments on stack
    void verify(int args) throws CalculatorException {
	if (stackp < args)
	    throw new CalculatorException("need " + args + " argument" +
		(args == 1 ? "" : "s") + " on stack");
    }

    public void execute(String cmd, PrintStream out) {

	try {
	    // see if it's a number; push on stack
	    try {
		double val = Double.valueOf(cmd).doubleValue();
		push(val);
		return;
	    } catch (NumberFormatException e) {
		// not a number: process as command
	    }

	    if (cmd.length() == 0) {

	    } else if (cmd.equals("push")) {
		verify(1);
		push(peek(1));

	    } else if (cmd.equals("pop")) {
		verify(1);
		pop();

	    } else if (cmd.equals("dump")) {
		for (int i = 1; i < depth(); i++) {
		    out.println("\t" + peek(depth() - i + 1) +
			"\t[" + i + "]");
		}

	    } else if (cmd.equals("+")) {
		verify(2);
		push(pop() + pop());

	    } else if (cmd.equals("-")) {
		verify(2);
		double y = pop();
		push(pop() - y);

	    } else if (cmd.equals("*")) {
		verify(2);
		push(pop() * pop());

	    } else if (cmd.equals("/")) {
		verify(2);
		double y = pop();
		push(pop() / y);

	    } else if (cmd.equals("%")) {
		verify(2);
		double y = pop();
		push(pop() % y);

	    } else if (cmd.equals("IEEEremainder")) {
		verify(2);
		double y = pop();
		push(Math.IEEEremainder(pop(), y));

	    } else if (cmd.equals("sin")) {
		verify(1);
		push(Math.sin(pop()));

	    } else if (cmd.equals("cos")) {
		verify(1);
		push(Math.cos(pop()));

	    } else if (cmd.equals("tan")) {
		verify(1);
		push(Math.tan(pop()));

	    } else if (cmd.equals("asin")) {
		verify(1);
		push(Math.asin(pop()));

	    } else if (cmd.equals("atan")) {
		verify(1);
		push(Math.atan(pop()));

	    } else if (cmd.equals("atan2")) {
		verify(2);
		double y = pop();
		push(Math.atan2(pop(), y));

	    } else if (cmd.equals("exp")) {
		verify(1);
		push(Math.exp(pop()));

	    } else if (cmd.equals("pow")) {
		verify(2);
		double x = pop();
		push(Math.pow(pop(), x));

	    } else if (cmd.equals("log")) {
		verify(1);
		push(Math.log(pop()));

	    } else if (cmd.equals("sqrt")) {
		verify(1);
		push(Math.sqrt(pop()));

	    } else if (cmd.equals("ceil")) {
		verify(1);
		push(Math.ceil(pop()));

	    } else if (cmd.equals("floor")) {
		verify(1);
		push(Math.floor(pop()));

	    } else if (cmd.equals("rint")) {
		verify(1);
		push(Math.rint(pop()));

	    } else if (cmd.equals("round")) {
		verify(1);
		push(Math.round(pop()));

	    } else if (cmd.equals("abs")) {
		verify(1);
		push(Math.abs(pop()));

	    } else if (cmd.equals("min")) {
		verify(2);
		push(Math.min(pop(), pop()));

	    } else if (cmd.equals("max")) {
		verify(2);
		push(Math.max(pop(), pop()));

	    } else {
		throw new CalculatorException("invalid command: " + cmd);
	    }

	    // print result of command (top of stack) and stack depth
	    if (depth() > 0) {
		out.println("\t" + peek(1) + "\t[" + depth() + "]");
	    } else {
		out.println("\t\t[empty stack]");
	    }

	} catch (CalculatorException e) {
	    out.println("ERROR: " + e.getMessage());
	}
    }


    public static void main(String[] args) {
	Calculator calc = new Calculator();
	DataInputStream in = new DataInputStream(System.in);

	System.out.println("Enter \"help\" for instructions");
	while (true) {
	    System.out.print("> ");
	    System.out.flush();

	    String line;
	    try {
		line = in.readLine();
	    } catch (IOException e) {
		break;
	    }
	    line = line.trim();			// ignore extra spaces

	    if (line.equals("quit"))
		break;
	    else if (line.equals("help"))
		help(System.out);
	    else
		calc.execute(line, System.out);
	}
    }

    public static void help(PrintStream out) {
	out.println("Calculator instructions:");
	out.println("\tEnter number to push on stack");
	out.println("\tEnter operator to perform operation");
	out.println("\tEnter \"dump\" to view stack");
	out.println("\tEnter \"push\" to copy top of stack");
	out.println("\tEnter \"pop\" to remove top of stack");
	out.println("\tEnter \"quit\" when done");
    }
}

class CalculatorException extends Exception {
    public CalculatorException(String message) {
	super(message);
    }
}
