import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args){
ArrayList<Integer> inputs = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
String line = in.nextLine();
if(line!=null && !line.isEmpty()) {
int res = resolve(line.trim());
System.out.println(String.valueOf(res));
}
}
public static int resolve(String expr) {
String[] exprs = expr.split(" ");
Stack<Integer> stack = new Stack<>();
for(int i=0;i<exprs.length;i++){
String temp = exprs[i];
if(temp.equals("")){
continue;
}
if(!(temp.contains("*")||temp.contains("^")||temp.contains("+"))){
if(stack.size()==16){
return -2;
} else {
stack.push(Integer.valueOf(temp));
}
} else {
String posibleNumber="";
boolean isNum = false;
for(int j=0;j<temp.length();j++){
if(temp.codePointAt(j)>=48&&temp.codePointAt(j)<=57){
posibleNumber+=temp.charAt(j);
isNum=true;
} else {
if(isNum) {
if(stack.size()==16){
return -2;
} else {
stack.push(Integer.valueOf(posibleNumber));
posibleNumber="";
isNum=false;
j--;
}
} else {
if(temp.charAt(j) == '^'){
if(stack.size()==0){
return -1;
} else {
int tempNum = stack.pop()+1;
stack.push(tempNum);
}
} else if(temp.charAt(j) == '+') {
if(stack.size()<2){
return -1;
} else {
int tempA = stack.pop();
int tempB = stack.pop();
stack.push(tempA+tempB);
}
} else if(temp.charAt(j) == '*'){
if(stack.size()<2){
return -1;
} else {
int tempA = stack.pop();
int tempB = stack.pop();
stack.push(tempA*tempB);
}
}
}
}
}
if(isNum){
if(stack.size()==16){
return -2;
} else {
stack.push(Integer.valueOf(posibleNumber));
posibleNumber="";
isNum=false;
}
}
}
}
if(stack.size()>=1)
return stack.pop();
else
return -1;
}
}
通过100%