zoukankan      html  css  js  c++  java
  • 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));
        }
  • 相关阅读:
    SQLSERVER中的sp_reset_connection存储过程的作用
    SQLSERVER数据库经常置疑的原因
    sqlserver2005数据库邮件
    SQLSERVER书签查找的通俗理解
    msdb数据库里的表究竟存储什么信息
    造成阻塞和死锁的3大原因:
    SQLSERVER中的锁资源类型RID KEY PAG EXT TAB DB FIL
    总结一下要影响SQLSERVER锁的申请和释放行为要考虑的因素
    Linux下getsockopt/setsockopt 函数说明
    HTTP协议详解(转)
  • 原文地址:https://www.cnblogs.com/Haidnor/p/12506195.html
Copyright © 2011-2022 走看看