Stack练习:: 中缀-后缀表达式

package Stack.Calculate;

import java.util.List;

public class calculation {
private String expression = null;

private InfixToPostfix inf = null;

private PostfixEval pos = null;

public calculation(String s) {
expression = s;
expression += "@";
}

public int calculat() {
int result = 0;
// 转化成后缀表达式
inf = new InfixToPostfix(expression);
List<String> list = inf.toPostfix();

// 后缀表达式求值
pos = new PostfixEval(list);
result = pos.evaluate();
return result;
}

public static void main(String[] args) {
calculation c = new calculation("(300 + 200) / 100");
System.out.println(c.calculat()); // 5
}

}


中缀转化成后缀表达式
package Stack.Calculate;

import java.util.*;

public class InfixToPostfix {

private String infixExp = null;

private List<String> postfixExp = new ArrayList<String>();

private Stack<Character> operStack = new Stack<Character>();

private StringBuffer numBuf = new StringBuffer();

private int priority(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case '@':
return 0;
}
return -1;
}

public InfixToPostfix(String s) {
infixExp = s;
}

public List<String> toPostfix() {
char ch, preChar;
operStack.push('@');

for (int i = 0; i < infixExp.length();) {
ch = infixExp.charAt(i);
switch (ch) {
case '+':
case '-':
case '*':
case '/':
preChar = operStack.peek();
while (priority(preChar) >= priority(ch)) {
postfixExp.add("" + preChar);
operStack.pop();
preChar = operStack.peek();
}
operStack.push(ch);
i++;
break;
case '(':
operStack.push(ch);
i++;
break;
case ')':
char c = operStack.pop();
while (c != '(') {
postfixExp.add("" + c);
c = operStack.pop();
}
i++;
break;
case '@':
char c1;
while (!operStack.isEmpty()) {
c1 = operStack.pop();
if (c1 != '@')
postfixExp.add("" + c1);
}
i++;
break;
case ' ':
case '\t':
i++;
break;
default:
if (Character.isDigit(ch)) {
while (Character.isDigit(ch)) {
numBuf.append(ch);
ch = infixExp.charAt(++i);
}
postfixExp.add("" + numBuf);
numBuf = new StringBuffer();
} else {
System.err.println("Illegal operator");
}

} // switch end~
}

return postfixExp;
} // toPostfix end~

/*
* public static void main(String[] args) { InfixToPostfix in = new
* InfixToPostfix("50 + 2 * 3@"); List<String> list = in.toPostfix();
* System.out.println(list); }
*/

}


后缀表达式求值
package Stack.Calculate;

import java.util.*;

public class PostfixEval {
private Stack<String> operandStack;

private List<String> list;

private boolean isOperator(String s) {
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
}

private int getOperand() {
/*
* if (operandStack.isEmpty()) throw new
* ArithmeticException("PostfixEval: Too many operators");
*/

return Integer.parseInt(operandStack.pop());
}

private int compute(int left, int right, String item) {
if (item.equals("+"))
return left + right;
else if (item.equals("-"))
return left - right;
else if (item.equals("*"))
return left * right;
else {
if (right == 0)
throw new ArithmeticException("PostfixEval: divide by 0");
return left / right;
}
}

public PostfixEval(List<String> list) {
this.list = list;
this.operandStack = new Stack<String>();
}

public int evaluate() {
int left, right, expValue = 0;
String item;
for (int i = 0; i < list.size(); i++) {
item = list.get(i);
if (isOperator(item)) {
right = getOperand();
left = getOperand();
expValue = compute(left, right, item);
operandStack.push("" + new Integer(expValue));
} else {
operandStack.push(item);
}
}

/*
* if (!operandStack.isEmpty()) throw new
* ArithmeticException("PostfixEval: Too many operands");
*/
return Integer.parseInt(operandStack.pop());
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值