1 packagedemo3;2 importdemo2.MyArrayStack;3
4 /**
5 * 使用栈来计算算出表达式的值6 *@authorASUS7 *8 */
9 public classTestCalculateExpression {10
11 public static voidmain(String[] args) {12 String expression="4+3+(6-10+2*3)*4";13 double result=calculate(expression);14 System.out.println(result);15
16 }17 //定义方法计算指定次表达式的值
18 private static doublecalculate(String expression) {19 MyArrayStack operatorStack=newMyArrayStack();20 MyArrayStack operandStack=newMyArrayStack();21 //遍历表达式的操作数与操作符
22 for(int i=0;i
25 if(Character.isDigit(cc)){26 //取出操作数
27 StringBuilder sb=newStringBuilder();28 //只有是数字就是循环的一部分
29 while(Character.isDigit(cc)){30 sb.append(cc);31 i++;32 if(i>=expression.length()){33 break;34 }35 cc=expression.charAt(i);36 }37 //操作数入栈
38 operandStack.push(i);39 //修正i变量的值
40 i--;41 }else{42 //如果是操作符43 //栈为空,直接把操作符入栈
44 if(operatorStack.isEmpty()){45 operatorStack.push(cc);46 continue;47 }48 while(!operatorStack.isEmpty()){49 char op1=(char) operatorStack.peek();50
51 //操作符栈不为空的情况
52 if(compareOperator(op1,cc)<0){53 //当前运算符优先级高于栈顶运算符优先级
54 operatorStack.push(cc);55 break;56 }else if(compareOperator(op1,cc)==0){57 //当前运算符优先级等于栈顶运算符的优先级,只有一种情况,左边小括号遇到右边小括号
58 operatorStack.pop();59 break;60 }else{61 //栈顶运算优先级高62 //取出两个操作数
63 if(operandStack.isEmpty()){64 throw new RuntimeException( "表达式错误");65 }66 double num1=Double.parseDouble(operandStack.pop().toString());67 if(operandStack.isEmpty()){68 throw new RuntimeException( "表达式错误");69 }70 double num2=Double.parseDouble(operandStack.pop().toString());71 //取栈顶运算符
72 char operator=(char) operatorStack.pop();73 //计算num1和num2的值
74 double result=compute(operator,num2,num1);75 //把结果存储到操作栈中
76 operandStack.push(result);77 //如果栈为空,新的操作符入栈
78 if(operatorStack.isEmpty()){79 operatorStack.push(cc);80 break;81 }82 }83
84 }85 }86 }87 //当表达式遍历完后,如果操作字符串不为空,需继续计算
88 while(!operatorStack.isEmpty()){89 if(operandStack.isEmpty()){90 throw new RuntimeException( "表达式错误");91 }92 char operator=(char) operatorStack.pop();93 double num1=Double.parseDouble(operandStack.pop().toString());94 if(operandStack.isEmpty()){95 throw new RuntimeException( "表达式错误");96 }97 double num2=Double.parseDouble(operandStack.pop().toString());98 if(operandStack.isEmpty()){99 throw new RuntimeException( "表达式错误");100 }101 double result=compute(operator,num2,num1);102 operandStack.push(result);103 }104 //当操作栈为空,操作数多余一个数表达式错误
105 if(operandStack.getSize()>1){106 throw new RuntimeException( "表达式错误");107 }108 returnDouble.parseDouble(operandStack.pop().toString());109 }110 private static double compute(char operator, double num1, doublenum2) {111 switch(operator) {112 case '+':113
114 return num1+num2;115 case '-':116
117 return num1-num2;118 case '*':119
120 return num1*num2;121 case '/':122
123 return num1/num2;124
125 }126 return 0;127 }128 private static int compareOperator(char op1, charop2) {129 if(op1=='+'||op1=='-'){130 if(op2=='*'||op2=='/'||op2=='('){131 return -1;132 }133 }134 if(op1=='*'||op1=='/'){135 if(op2=='('){136 return -1;137 }138 }139 if(op1=='('){140 if(op2==')'){141 return 0;142 }else{143 return -1;144 }145 }146 return 1;147 }148
149 }