使用栈实现四则运算中缀到后缀的转换,代码如下:
package com.mytest;
import java.util.Stack;
public class MyTest {
public static void main(String[] args) {
String[] infixExpression = { "9", "+", "(", "3", "-", "1", ")", "*", "3", "+", "10", "/", "2" };// 中缀表达式
Stack<String> operatorStack = new Stack<String>();// 操作符栈
Stack<String> postfixStack = new Stack<String>();// 后缀表达式栈
for (int i = 0; i < infixExpression.length; i++) {
String ope = infixExpression[i];
if (ope.matches("\\d+")) {
postfixStack.push(ope);
} else {
pushpopstack(operatorStack, postfixStack, ope);
}