ch=exp.charAt(i);
switch(ch){
case'+':
case'-':
case'*':
case'/':
preChar=opStack.peek();
// 如果栈里面的操作符优先级比当前的大,则把栈中优先级大的都添加到后缀表达式列表中
while(priority(preChar)>=priority(ch)){
postExp.add(""+preChar);
opStack.pop();
preChar=opStack.peek();
}
opStack.push(ch);
i++;
break;
case'(':
// 左括号直接压栈
opStack.push(ch);
i++;
break;
case')':
// 右括号则直接把栈中左括号前面的弹出,并加入后缀表达式链表中
charc=opStack.pop();
while(c!
='('){
postExp.add(""+c);
c=opStack.pop();//'c'是'('时不要添加到输出字符串中了
}
i++;
break;
// #号,代表表达式结束,可以直接把操作符栈中剩余的操作符全部弹出,并加入后缀表达式链表中
case'#':
charc1;
while(!
opStack.isEmpty()){
c1=opStack.pop();
if(c1!
='#')
postExp.add(""+c1);
}
i++;
break;
//过滤空白符
case'':
case'\t':
i++;
break;
// 数字则凑成一个整数,加入后缀表达式链表中
default:
if(Character.isDigit(ch)){
while(Character.isDigit(ch)){
numBuffer.append(ch);
ch=exp.charAt(++i);
}
postExp.add(numBuffer.toString());
numBuffer=newStringBuffer();
}else{
thrownewIllegalExpressionException("illegaloperator");
}
}
}
}catch(RuntimeExceptione){
thrownewIllegalExpressionException(e.getMessage());
}
returnpostExp;
}
privateintpriority(charop){//定义优先级
switch(op){
case'+':
case'-':
return1;
case'*':
case'/':
return2;
case'(':
case'#':
return0;
}
thrownewIllegalExpressionException("Illegaloperator");
}
//main
publicstaticvoidmain(String[]args){
Lab6eval=newLab6();
System.out.println("输入公式");
Strings=SavitchIn.readLine();
s=s+"#";
doubleresult=eval.eval(s);
System.out.println(result);
}
}
//继承异常
classIllegalExpressionExceptionextendsRuntimeException{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
publicIllegalExpressionException(){
}
publicIllegalExpressionException(Stringinfo){
super(info);
}
∙package com.JRIAS;
∙
∙import java.math.BigDecimal;
∙import java.util.ArrayList;
∙import java.util.List;
∙import java.util.regex.Matcher;
∙import java.util.regex.Pattern;
∙
∙/**
∙ *
∙ * 应用:
逆波兰表达式
∙ *
∙ * @author Leon.Chen
∙ * gxqxmh@ www.JRIAS.com
∙ */
∙public class CalculateString {
∙
∙ public BigDecimal calculateString(String str) {
∙ char[] strs = str.toCharArray();
∙ Stack stack = new Stack();
∙
∙ for (int i = 0; i < strs.length; i++) {
∙ String stackStr = String.valueOf(strs[i]);
∙ stack.push(stackStr);
∙ if (")".equals(stack.top())) {
∙ String subStr = null;
∙ while (!
"(".equals(stack.top())) {
∙ stack.pop();
∙ if (!
"(".equals(stack.top())) {
∙ subStr = addEnd(subStr, stack.top());
∙ }
∙ }
∙ String pushStr = CalculateReversePolishExpression(subStr);
∙ stack.pop();
∙ stack.push(pushStr);
∙ }
∙
∙ }
∙ String resultStr = null;
∙ while (stack.count !
= 0) {
∙ resultStr = CalculateReversePolishExpression(stack.toString());
∙ }
∙ BigDecimal result = null;
∙ if (resultStr !
= null) {
∙ result = new BigDecimal(resultStr);
∙ } else {
∙ result = BigDecimal.ZERO;
∙ }
∙ return result.setScale(2, BigDecimal.ROUND_HALF_UP);
∙ }
∙
∙ public String[] matcher(String regex, String str) {
∙ Pattern pattern = Ppile(regex);
∙ Matcher matcher = pattern.matcher(str);
∙ List list = new ArrayList();
∙ while (matcher.find()) {
∙ list.add(matcher.group());
∙ }
∙ String[] result = new String[list.size()];
∙ return list.toArray(result);
∙ }
∙
∙ public List createReversePolishExpression(String subStr) {
∙ String regex = "\\d+\\.{0,1}\\d*";
∙ String[] numbers = matcher(regex, subStr);
∙ String changeStr = subStr.replaceAll(regex, "0").replaceAll("\\-\\-0",
∙ "-1").replaceAll("\\+\\-0", "+1").replaceAll("\\*\\-0", "*1")
∙ .replaceAll("\\/\\-0", "/1");
∙ char[] chars = changeStr.toCharArray();
∙ int index = 0;
∙ List list = new ArrayList();
∙ for (int i = 0; i < chars.length; i++) {
∙ String str = String.valueOf(chars[i]);
∙ if ("0".equals(str)) {
∙ list.add(numbers[index++]);
∙ } else if ("1".equals(str)) {
∙ list.add("-" + numbers[index++]);
∙ } else {
∙ list.add(str);
∙ }
∙ }
∙ List suffix = new ArrayList();
∙ Stack operator = new Stack();
∙ for (int i = 0; i < list.size(); i++) {
∙ if (!
isOperatorType(list.get(i))) {
∙ suffix.add(list.get(i));
∙ } else {
∙ if (operator.count == 0) {
∙ operator.push(list.get(i));
∙ } else {
∙ while (operator.count !
= 0&&compare(operator.top(), list.get(i)) >= 0) {
∙ String top = operator.top();
∙ operator.pop();
∙ suffix.add(top);
∙ }
∙ operator.push(list.get(i));
∙
∙ }
∙ }
∙ }
∙
∙ while (operator.count !
= 0) {
∙ suffix.add(operator.top());
∙ operator.pop();
∙ }
∙ return suffix;
∙ }
∙
∙ public String CalculateReversePolishExpression(String subStr) {
∙