• 递归向下解析算术表达式(三)


    本例代码下载:https://files.cnblogs.com/files/heyang78/MathAnalyzer2-20200524-1.zip

    较原有版本,是多了Node类和TreeBuilder类,二者用于构建表达式树:

    Node类:

    package com.heyang;
    
    public class Node {
        private String text;
        private Node leftChild;
        private Node rightChild;
        
        public Node() {
            this("");
        }
        
        public Node(String text) {
            this.text=text;
        }
        
        public String getText() {
            return text;
        }
        public void setText(String text) {
            this.text = text;
        }
        public Node getLeftChild() {
            return leftChild;
        }
        public void setLeftChild(Node leftChild) {
            this.leftChild = leftChild;
        }
        public Node getRightChild() {
            return rightChild;
        }
        public void setRightChild(Node rightChild) {
            this.rightChild = rightChild;
        }
    }

    TreeBuilder类:

    package com.heyang;
    
    import java.util.List;
    
    public class TreeBuilder {
        private List<Token> tokens;
        private int tokenIdx;
        private Node root;
        
        public TreeBuilder(List<Token> tokens)  throws AnalyzerExp{
            this.tokens=tokens;
            this.tokenIdx=0;
            
            root=parse_expression();
        }
        
        public Node getRoot() {
            return root;
        }
        
        private Token fetchToken() {
            if(tokenIdx>=tokens.size()) {
                return null;
            }else {
                Token t=tokens.get(tokenIdx);
                tokenIdx++;
                return t;
            }        
        }
        
        private void returnToken() {
            if(tokenIdx>0) {
                tokenIdx--;
            }
        }
        
        private Node parse_expression() throws AnalyzerExp{
            Node left,right;
            Token currentToken;
            
            left=parse_term();
            for(;;) {
                currentToken=fetchToken();
                if(currentToken==null) {
                    return left;
                }
                
                if(currentToken.getType()!=Token.TYPE_PLUS && currentToken.getType()!=Token.TYPE_MINUS) {
                    returnToken();
                    break;
                }
                
                right=parse_term();
                
                Node plusMinusNode=new Node(currentToken.getText());
                plusMinusNode.setLeftChild(left);
                plusMinusNode.setRightChild(right);
                left=plusMinusNode;
            }
            
            return left;
        }
        
        private Node parse_term() throws AnalyzerExp{
            Node left,right;
            Token currentToken;
            
            left=parse_primary_exp();
            for(;;) {
                currentToken=fetchToken();
                if(currentToken==null) {
                    return left;
                }
                
                if(currentToken.getType()!=Token.TYPE_MULTI && currentToken.getType()!=Token.TYPE_DIVIDE) {
                    returnToken();
                    break;
                }
                
                right=parse_primary_exp();
                
                Node multiDivideNode=new Node(currentToken.getText());
                multiDivideNode.setLeftChild(left);
                multiDivideNode.setRightChild(right);
                left= multiDivideNode;
            }
            
            return left;
        }
        
        private Node parse_primary_exp() throws AnalyzerExp{
            Token currToken;
            
            currToken=fetchToken();
            if(currToken==null) {
                return null;
            }
            
            if(currToken.getType()==Token.TYPE_DIGITAL) {
                return new Node(currToken.getText());
            }else if(currToken.getType()==Token.TYPE_LEFT_PAREN){
                Node retval=parse_expression();
                
                currToken=fetchToken();
                if(currToken==null) {
                    return retval;
                }
                
                if(currToken.getType()!=Token.TYPE_RIGHT_PAREN) {
                    throw new AnalyzerExp("missing )");
                }
                
                return retval;
            }else {
                throw new AnalyzerExp(currToken+" should be a digital.");
            }
        }
    }

    解析效果:

    1+2+3=6.0
          +      
        /       
      +       3  
     /          
    1   2        
    1+2+3+4=10.0
                +            
             /              
          +           4      
        /                   
      +       3              
     /                      
    1   2                    
    1+2+3+4+5=15.0
                            +                        
                        /                           
                    +               5                
                 /                                  
              +           4                          
            /                                       
          +       3                                  
         /                                          
        1   2                                        
    1+(2+3)*4=21.0
                +            
             /              
          1           *      
                    /       
                  +       4  
                 /          
                2   3        
    1+(2+3)*4+5=26.0
                            +                        
                        /                           
                    +               5                
                 /                                  
              1           *                          
                        /                           
                      +       4                      
                     /                              
                    2   3                            

    --2020年5月24日--

  • 相关阅读:
    Markdown 图片与图床使用
    gitignore
    设置或更改Mac文件的默认打开程序
    Hive时间处理
    csv大文件处理方案-数据量超表格最大容纳行数解决方案
    js中的闭包之我理解
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(73)-微信公众平台开发-消息管理
    关于23种设计模式的有趣见解
    一步一步写算法(之 算法总结)
    ajax跨域通信-博客园老牛大讲堂
  • 原文地址:https://www.cnblogs.com/heyang78/p/12950357.html
Copyright © 2020-2023  润新知