/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package LinkerList;
/**
*
* @author 一天更進步一天
*/
import java.util.Scanner;
import java.util.Stack;
public class InfixToPostfix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String s = input.nextLine();
int ans = PostfixCounter(toPostfix(s));
System.out.println(ans);
}
}
public static String[] toPostfix(String s) { //中序轉後序
/*
String data[] = s.split(" ");
Stack<Character> opst = new Stack<Character>();
int qReads = 0;
String[] q = new String[100];
for (int i = 0; i < data.length; i++) {
String tmp = data[i];
if (Character.isDigit(tmp.charAt(0))) {
q[qReads++] = tmp;
} else {
char t = tmp.charAt(0);
System.out.println(t);
switch (t) {
case ')':
while (opst.peek() != '(') {
q[qReads++] = opst.pop() + "";
}
opst.pop();// pop (
break;
case '(':
opst.push(t);
break;
case '+': case '-': case '*': case '/': case '%':
if (opst.isEmpty()) {
opst.push(t);
} else {
if (isFormer(t, opst.peek())) { //如果tmp的優先序大
opst.push(t);
} else {
q[qReads++] = opst.pop() + ""; //放進queue中
opst.push(t);
}
}
break;
default:
System.out.println("errors");
}
}
*/
String data[] = s.split(" ");
Stack<String> opst = new Stack<String>();
int qReads = 0;
String[] q = new String[1000];
for (int i = 0; i < data.length; i++) {
if(!data[i].equals("")){
String tmp = data[i];
if (isOp(tmp)) {
if (opst.isEmpty()) {
opst.push(tmp);
} else {
if (isFormer(tmp.charAt(0), opst.peek().charAt(0))) { //如果tmp的優先序大
opst.push(tmp);
} else {
q[qReads++] = opst.pop(); //放進queue中
opst.push(tmp);
}
}
} else if (tmp.equals(")")) {
while (!opst.peek().equals("(")) {
q[qReads++] = opst.pop();
}
opst.pop();// pop (
} else if (tmp.equals("(")) {
opst.push(tmp);
} else {
q[qReads++] = tmp;
}
}
}
while (!opst.isEmpty()) {
q[qReads++] = opst.pop() + "";
}
/*
for (String ss : q) {
System.out.print(ss + ",");
}*/
return q;
}
public static boolean isOp(String s) {
/*
char c = s.charAt(0);
switch (c) {
case '+':
case '-':
case '*':
case '/':
case '%':
return true;
default:
return false;
}*/
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")||s.equals("%")){
return true;
}else{
return false;
}
}
public static boolean isFormer(char a, char b) { //問a是否比b的次序先?
boolean isformer = true;
if (getTheOrder(a) - getTheOrder(b) >= 0) { //判斷是否不優先(相等當成不優先 )
//因為由左往後算
isformer = false;
}
return isformer;
}
public static int getTheOrder(char c) {//傳回判斷次序先後
if (c == '+' || c == '-') {//
return 2;
} else if (c == '*' || c == '/' || c == '%') {//*/%
return 1;
} else {
return 3;
}
}
public static int PostfixCounter(String data[]) { //or String[]
Stack<Integer> st = new Stack<Integer>();
for (int i = 0; i < data.length; i++) {
String tmp = data[i];
if (tmp == null) {
break;
} else {
if (Character.isDigit(tmp.charAt(0))) {
st.push(Integer.parseInt(tmp));
} else {
int second = st.pop();
int first = st.pop();
int sum = countTwoNum(first, tmp.charAt(0), second);
st.push(sum);
}
}
}
return st.peek();
}
public static int countTwoNum(int n1, char ind, int n2) {
//單純的計算兩個數字監的伍則運算
int ans = 0;
switch (ind) {
case '+':
ans = n1 + n2;
break;
case '-':
ans = n1 - n2;
break;
case '*':
ans = n1 * n2;
break;
case '/':
ans = n1 / n2;
break;
case '%':
ans = n1 % n2;
break;
default:
System.out.println("formate error!!");
break;
}
return ans;
}
}