import java.util.*;

public class Calculator{

	private ArrayList operator;//操作符栈
	private ArrayList operand;//操作数栈
	private String opnd;//存放操作数
	private double res;
	char precede(char a,char bi){//比较操作符的优先级
		int i,j;
		char[][] compare={
			{' ','+','-','*','/','(',')','#'},
			{'+','>','>','<','<','<','>','>'},
			{'-','>','>','<','<','<','>','>'},
			{'*','>','>','>','>','<','>','>'},
			{'/','>','>','>','>','<','>','>'},
			{'(','<','<','<','<','<','=',' '},
			{')','>','>','>','>',' ','>','>'},
			{'#','<','<','<','<','<',' ','='}
		};
		for(i=0;i<8;i++)
			if(compare[0][i]==a)break;
		for(j=0;j<8;j++)
			if(compare[j][0]==b)break;
		return compare[j][i];
	}

	double operate(double a,char x,double c){//对a和c进行x运算
		double y;
		switch(x){
			case '+':y=a+c;break;
			case '-':y=a-c;break;
			case '*':y=a*c;break;
			case '/':y=a/c;break;  
			default:return 0;
		}
		return y;
	}
	public boolean evaluateExpression(){//计算表达式的值
		operator.add('#');//'#'入栈
		while(true){
			if("0123456789.".indexOf(comm)!=-1){
				opnd+=comm;
				operand.add(opnd);
			}

			if("+-*/".indexOf(comm)!=-1){
				//比较当前操作符和栈顶操作符的优先级
				switch(precede(comm.charAt(0),operator.get(operator.size()-1))){
					case '<':
						operator.add(comm.charAt(0));
						break;//当前操作符优先级低于栈顶操作符,当前操作符入栈
					case '=':
						operator.remove(operator.size()-1);
						break;//当前操作符优先级等于栈顶操作符，栈顶操作符出栈
					case '>':
						char theta=(char)operator.remove(operator.size()-1);//当前操作符优先级大于栈顶操作符,操作符出栈
						double a=(double)  operand.remove(operand.size()-1);//操作数出栈
						double b=(double)operand.remove(operand.size()-1);//操作数出栈
							 operand.add(operate(a,theta,b));break;//把a theta b的结果入操作数栈
					default:break;
				}
			}
			if(operator.get(operator.size()-1)=='#') {
				res=(double) operand.get(operand.size()-1);
				return true;
			}else{
				return false;
			}
		}
	}
}
