• 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));
        }
  • 相关阅读:
    linux下vim的安装及其设置细节
    vm虚拟机下ubuntu连接上ssr
    文件写入-结构体排序
    利用链表进行报数游戏
    链表——尾插法
    C#设计模式总结
    C#设计模式(20)——策略者模式(Stragety Pattern)
    Autofac在项目中应用的体会,一个接口多个实现的情况
    C#设计模式(1)——单例模式
    jquery.js与sea.js综合使用
  • 原文地址:https://www.cnblogs.com/Haidnor/p/12506195.html
Copyright © 2020-2023  润新知