java 将算式转换成后缀表达式(逆波兰表达式)

1.该方法仅仅支持常数(大于等于零的整数),不支持数字前带有负号。

2.字符与字符之间应用空格符号隔开

import java.util.Stack;

/**
* 波兰表达式转换
* @author Haidnor
* @creat 2020-03-14-14:21
*/
public class PolishNotationFactory {
/**
* 将运算式字符串转换成逆波兰表达式 (toReversePolishNotation)
* 例如: ( ( 1 + 2 ) * 1 ) * ( 1 + 2 ) - ( 3 / 1 )
* 返回: 1 2 + 1 * 1 2 + * 3 1 / -
* @param expression 运算式
* @return ReversePolishNotation
*/
public String toRPN(String expression) {
String[] split = expression.split(" ");
StringBuffer sb = new StringBuffer();
StringBuffer buffer = new StringBuffer();
Stack<String> operators = new Stack<String>();
Boolean isBracket = false;
int bracket = 0;
int i = 0;
for (String s : split) {
if (s.equals("(")) {
isBracket = true;
bracket++;
i++;
continue;
} else if (s.equals(")")) {
isBracket = false;
bracket--;
}
if (isBracket == false & buffer.length() == 0 & bracket == 0) {
if (this.isOperator(s)) {
operators.push(s);
continue;
}
}
if (isBracket & bracket >= 1) {
if (this.isOperator(s)) {
buffer.append(s + " ");
continue;
}
} else if (isBracket == false & i != 0) {
sb.append(this.transform(buffer.toString()));
buffer.setLength(0);
i--;
if (bracket >= 1) {
isBracket = true;
}
while (!operators.empty()) {
sb.append(operators.pop());
}
if (i == 0) {
if (!operators.empty()) {
sb.append(operators.pop() + " ");
}
}
continue;
}
if (isBracket == false & buffer.length() == 0) {
if (this.isOperator(s)) {
operators.push(s + " ");
continue;
} else {
buffer.append(s + " ");
}
} else {
buffer.append(s + " ");
}
}
if (buffer.length() != 0) {
sb.append(this.transform(buffer.toString()));
}
while (operators.size() != 0) {
sb.append(operators.pop());
}
return sb.toString();
}

/**
* 将运算式字符串转换成逆波兰表达式 (toReversePolishNotation)
* 不能传入括号 "(",")"
* 例如: 5 * 2 + 1 返回: 5 2 * 1 +
* @param expression 运算式
* @return ReversePolishNotation
*/
private String transform(String expression) {
String[] split = expression.split(" ");
StringBuffer sb = new StringBuffer();
Stack<String> operators = new Stack<String>();
for (String s : split) {
if (s.matches("\\d")) {
sb.append(s + " ");
} else if (s.equals("+") || s.equals("-")) {
if (!operators.empty()) {
sb.append(operators.pop() + " ");
}
}
if (operators.size() == 2) {
sb.append(operators.pop() + " ");
}
if (this.isOperator(s)){
operators.push(s);
}
}
while (!operators.empty()) {
sb.append(operators.pop() + " ");
}
return sb.toString();
}

/**
* 判断是否是四则运算符
* @param operators
* @return
*/
private Boolean isOperator(String operators) {
if (operators.equals("+") || operators.equals("-") || operators.equals("*") || operators.equals("/")) {
return true;
} else {
return false;
}
}

}

测试代码

    public static void main(String[] args) {
        // 1 2 + 1 * 1 2 + * 3 1 / -
        String exp = "( ( 1 + 2 ) * 1 ) * ( 1 + 2 ) - ( 3 / 1 )";

        // 1 2 + 1 * 2 1 * +
        //String exp = "( ( ( 1 + 2 ) * 1 ) + ( 2 * 1 ) )";

        // 1 2 + 3 /
        //String exp = "( 1 + 2 ) / 3";

        PolishNotationFactory pnf = new PolishNotationFactory();
        System.out.println("R>>>:" + pnf.toRPN(exp));
    }

猜你喜欢

转载自www.cnblogs.com/Haidnor/p/12506195.html