中缀表达式转后缀表达式
public class InfixToSuffix {
public static void main(String[] args) {
String expression = "(10+20/2*3)/2+8";
expression = infixToSuffix(expression);
System.out.println(expression);
}
public static String infixToSuffix(String expression) {
//操作符的栈
ArrayStack<String> opStack = new ArrayStack<>();
//后缀表达式的线性表
ArrayList<String> suffixList = new ArrayList<>();
//格式化字符串
expression = insertBlanks(expression);
String[] tokens = expression.split(" ");
for (String token : tokens){
//过滤空串
if (token.length() == 0){
continue;
}
if (isOperator(token)){
/*
什么时候操作符进栈
1.栈为空
2.栈顶是(
3.栈顶是操作符,且优先级比当前token小
什么时候需要将栈顶操作符出栈?
1.栈顶操作符的优先级 >= 当前token
*/
while(true){
if (opStack.isEmpty() || opStack.peek().equals("(") || priority(opStack.peek()) < priority(token)){
opStack.push(token);
break;
}
suffixList.add(opStack.pop());
}
}else if (token.equals("(")){
opStack.push(token);
}else if (token.equals(")")){
while (!opStack.peek().equals("(")){
suffixList.add(opStack.pop());
}
opStack.pop();
}else if (isNumber(token)){
suffixList.add(token);
}else {
throw new IllegalArgumentException("wrong char:" + expression);
}
}
while (!opStack.isEmpty()){
suffixList.add(opStack.pop());
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < suffixList.size(); i++) {
sb.append(suffixList.get(i));
sb.append(' ');
}
return sb.toString();
}
private static int priority(String token) {
if (token.equals("+") || token.equals("-")){
return 0;
}
if (token.equals("*") || token.equals("/")){
return 1;
}
return -1;
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
private static boolean isOperator(String token) {
return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
}
//对原表达式进行格式化处理 给所有的非数字字符左右添加空格
private static String insertBlanks(String expression) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c == '(' || c ==')' || c == '+' || c =='-' || c == '*' || c == '/'){
sb.append(' ');
sb.append(c);
sb.append(' ');
}else {
sb.append(c);
}
}
return sb.toString();
}
}
对于后缀表达式,我们利用栈进行计算,遍历后缀表达式,遇到数字我们直接进栈,碰到+ - * /我们提取两个数字进行计算后直接入栈
public class SuffixCalculator {
public static void main(String[] args) {
String infixExpression = "(10+20/2*3)/2+8";
String suffixExpression = InfixToSuffix.infixToSuffix(infixExpression);
int result = evaluateSuffix(suffixExpression);
System.out.println(result);
}
private static int evaluateSuffix(String expression) {
ArrayStack<Integer> stack = new ArrayStack<>();
String[] tokens = expression.split(" ");
for (String token: tokens) {
if (token.length() == 0 ){
continue;
}
if (isNumber(token)){
stack.push(new Integer(token));
}else {
processAnOperator(stack,token);
}
}
return stack.pop();
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
private static void processAnOperator(ArrayStack<Integer> stack, String token) {
int num1 = stack.pop();
int num2 = stack.pop();
if (token.equals("+")){
stack.push(num2 + num1);
}else if (token.equals("*")){
stack.push(num2 * num1);
}else if (token.equals("-")){
stack.push(num2 - num1);
}else if (token.equals("/")){
stack.push(num2 / num1);
}
}
}