import java.util.ArrayDeque;
import java.util.ArrayList;
public class Calc {
public static void main(String[] args) {
ArrayList<Object> equ = parseExpr("3 4 + 2 7 * + x -");
System.out.println(eval(equ, 3.0));
}
static ArrayList<Object> parseExpr(String expr) {
String[] parts = expr.split(" ");
ArrayList<Object> result = new ArrayList<Object>();
for(String elem: parts) {
try {
Double num = Double.valueOf(elem);
result.add(num);
} catch(NumberFormatException e) {
result.add(elem);
}
}
return result;
}
static Double eval(ArrayList<Object> equation, Double x) {
ArrayDeque<Double> stack = new ArrayDeque<Double>();
for(Object elem: equation) {
if(elem instanceof Double) {
stack.push((Double)elem);
} else {
String str = (String)elem;
if(str.equals("x")) stack.push(x);
else if(str.equals("+")) stack.push(stack.pop() + stack.pop());
else if(str.equals("-")) stack.push(-stack.pop() + stack.pop());
else if(str.equals("*")) stack.push(stack.pop() * stack.pop());
else if(str.equals("/")) stack.push(1/stack.pop() * stack.pop());
else if(str.equals("sin")) stack.push(Math.sin(stack.pop()));
else if(str.equals("cos")) stack.push(Math.cos(stack.pop()));
}
}
return stack.pop();
}
}