zoukankan      html  css  js  c++  java
  • [Java]算术表达式求值之三(中序表达式转二叉树方案 支持小数)

    Entry类 这个类对表达式的合法性进行了粗筛:

    package com.hy;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    // 此类用于把算术表达式送入解析器
    public class Entry {
        public static void main(String[] args) throws IOException{
            // 取得用户输入的表达式
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
            String rawExpression = null; 
            System.out.print("请输入算术表达式:"); 
            rawExpression = br.readLine(); 
            
            // 得到合法的算术表达式
            String expression="";    
            for(int i=0;i<rawExpression.length();i++){
                // 拿到表达式的每个字符
                char c=rawExpression.charAt(i);
                //System.out.print(c+","); 
                
                if(Character.isDigit(c) || c=='+' || c=='-' || c=='*' || c=='/' || c=='(' || c==')' || c=='.'){
                    //System.out.print(c);
                    expression+=c;
                }else{
                    System.out.print(" "+c+"不是合法的算术表达式字符.");
                    System.exit(0);
                }
            }
            
            // 送去解析
            Lexer p=new Lexer(expression);
            //p.print();
            
            // 
            Tree t=new Tree(p.getInfixList());
            try {
                System.out.println(t.evaluate());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    执行结果 以下测试用例都通过了:

    请输入算术表达式:1+2+3
    6.0
    
    请输入算术表达式:1+2*3
    7.0
    
    请输入算术表达式:2*(3+4)
    14.0
    
    请输入算术表达式:1+2*(6-4)
    5.0
    
    请输入算术表达式:1+2*(5-4)+6-7
    2.0
    
    请输入算术表达式:(1+2)*3-4*(6-5)
    5.0
    
    请输入算术表达式:(1+2)*(3+4)
    21.0
    
    请输入算术表达式:1.1*5+(3+4)*10
    75.5

    Lexer类 这个类起词法分析器的作用,其核心利器是正则表达式,分词完毕后得到一个含有中序表达式的列表,如 ”1.2,+,3,*,4“:

    package com.hy;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    // 此类用于将算术表达式解析成包含操作数和操作符的链表,扮演分词器的角色
    public class Lexer {
        private List<String> list;// 用于存储中序表达式的链表
        
        public List<String> getInfixList() {
            return list;
        }
    
        public Lexer(String expression){
            list=new ArrayList<String>();
    
            // 使用正则表达式分词
            String regExp = "(\d+(\.*)\d*)|(\+)|(\-)|(\*)|(\/)|(\()|(\))";
              
            Pattern pattern=Pattern.compile(regExp);
            Matcher matcher=pattern.matcher(expression);
            while(matcher.find()){
                list.add(matcher.group(0));
            }    
        }
        
        public void print(){
            for(String str:list){
                System.out.println(str);
            }
        }
    }

    Tree类 输入一个中序表达式列表,得到构建好的树,这棵树就是算术表达式的语法树。递归的build函数是本类代码的核心:

    package com.hy;
    
    import java.util.List;
    
    // 以算术表达式为基础构建一棵二叉树
    public class Tree {
        // 根节点
        private Node root;
        
        // infixList为分词完毕的中序表达式列表
        public Tree(List<String> infixList){
            root=build(infixList,0,infixList.size());
        }
        
        // 构建一棵树,从根节点构建起
        private Node build(List<String> list,int start,int end){
            int depth=0;//记录深度,进一层括号加一,退出来减一
            int plusDivideRightmostPos=-1;// 记录最右边的加减号位置
            int multiDivideRightmostPos=-1;// 记录最右边的乘除号位置
            
            // 操作数
            if(start==end-1){
                // 下标相差一,说明找到的是没有子节点的叶子节点,也即操作数节点
                Node leafNode=new Node(NodeType.Digit,list.get(start));
                return leafNode;
            }
            
            // 这个循环是为了找括号外最右边的运算符位置
            for(int i=start;i<end;i++){
                String operatorText=list.get(i);// 获得操作符的文字,如果是操作数直接ignore
                
                if(operatorText.equals("(")){
                    depth++;
                }else if(operatorText.equals(")")){
                    depth--;
                }else if(operatorText.equals("+") || operatorText.equals("-") ){
                    if(depth==0){
                        plusDivideRightmostPos=i;
                    }
                }else if(operatorText.equals("*") || operatorText.equals("/") ){
                    if(depth==0){
                        multiDivideRightmostPos=i;
                    }
                }
            }
            
            int rightMost=-1;
            
            if(plusDivideRightmostPos==-1 && multiDivideRightmostPos==-1){
                // 整个算式被多余的括号括起来了,去掉这层多余的括号再做
                return build(list,start+1,end-1);
            }
            
            // 优先取加减号的位置,因为它的计算优先级最低,应当最后算
            rightMost=plusDivideRightmostPos;
            
            if(plusDivideRightmostPos==-1 && multiDivideRightmostPos>0){
                // 括号外只有乘除号,如(1+2)*(3+4),这时只有取乘除号位置,
                rightMost=multiDivideRightmostPos;
            }
            
            // 如果能走到这里,则最右边括号外的运算符位置已经找到了,可以开始构建节点
            String operator=list.get(rightMost);
            Node nodeOper=new Node(operator);// 这里创建的节点都是操作符节点,不是最终的叶子节点
            
            // 以最右边的操作符为界,分两侧构建左右子节点
            nodeOper.setLeftNode(build(list,start,rightMost));
            nodeOper.setRightNode(build(list,rightMost+1,end));
            
            // 返回构建完的节点
            return nodeOper;
        }
        
        // 取二叉树的值
        public float evaluate() throws Exception{
            return this.root.getValue();
        }
    }

    Node类 这是如教科书似的二叉树定义:

    package com.hy;
    
    // 二叉树节点类
    public class Node {
        private NodeType type;
        private float value;
        private Node leftNode;// 左节点
        private Node rightNode;// 右节点
        
        public Node(){
            type=NodeType.Undifined;
            value=0.0f;
            leftNode=null;
            rightNode=null;
        }
        
        public Node(String nodeTypeText){
            if(nodeTypeText.equals("+")){
                this.type=NodeType.OP_Plus;
            }else if(nodeTypeText.equals("-")){
                this.type=NodeType.OP_Minus;
            }else if(nodeTypeText.equals("*")){
                this.type=NodeType.OP_Multi;
            }else if(nodeTypeText.equals("/")){
                this.type=NodeType.OP_Divide;
            }else{
                this.type=NodeType.Undifined;
            }
            
            value=0.0f;
            leftNode=null;
            rightNode=null;
        }
        
        public Node(NodeType type){
            this.type=type;
            value=0.0f;
            leftNode=null;
            rightNode=null;
        }
        
        public Node(NodeType type,String str){
            this.type=type;
            this.value=Float.valueOf(str);
            leftNode=null;
            rightNode=null;
        }
        
        public Node(NodeType type,float value,Node leftNode,Node rightNode){
            this.type=type;
            this.value=value;
            this.leftNode=leftNode;        
            this.rightNode=rightNode;
        }
        
        public Node(NodeType type,Node leftNode,Node rightNode){
            this.type=type;
            this.value=0;
            this.leftNode=leftNode;
            this.rightNode=rightNode;
        }
        
        public float getValue() throws Exception{
            if(this.type==NodeType.Digit){
                return value;
            }else if(this.type==NodeType.OP_Divide){
                return leftNode.getValue()/rightNode.getValue();
            }else if(this.type==NodeType.OP_Minus){
                return leftNode.getValue()-rightNode.getValue();
            }else if(this.type==NodeType.OP_Multi){
                return leftNode.getValue()*rightNode.getValue();
            }else if(this.type==NodeType.OP_Plus){
                return leftNode.getValue()+rightNode.getValue();
            }else{
                throw new Exception("Not initialize");
            }
        }
        
        public void setLeftNode(Node leftNode) {
            this.leftNode = leftNode;
        }
    
        public void setRightNode(Node rightNode) {
            this.rightNode = rightNode;
        }
        
        public Node getLeftNode() {
            return leftNode;
        }
    
        public Node getRightNode() {
            return rightNode;
        }
        
        public String toString(){
            if(this.type==NodeType.Digit){
                return String.valueOf(value)+" ";
            }else if(this.type==NodeType.OP_Divide){
                return "/ ";
            }else if(this.type==NodeType.OP_Minus){
                return "- ";
            }else if(this.type==NodeType.OP_Multi){
                return "* ";
            }else if(this.type==NodeType.OP_Plus){
                return "+ ";
            }else{
                return "? ";
            }
        }
        
        public NodeType getType() {
            return type;
        }
        
        public void setType(NodeType type) {
            this.type = type;
        }
    
        public void setValue(float value) {
            this.value = value;
        }
    }

    NodeType枚举

    package com.hy;
    
    // 节点类型
    public enum NodeType {
        Undifined,
        OP_Plus,
        OP_Minus,
        OP_Multi,
        OP_Divide,
        Digit,
    }

    到这里,将算术表达式求值的两种方式--转后序表达式或二叉树求值都已经实现过了,虽然其词法分析器和语法分析器还稚嫩,但万丈高楼总需平地起。

    参考文献:

    1.算术表达式构造二叉树; 二叉树计算算术表达式 https://blog.csdn.net/qq120848369/article/details/5673969

    2.算数表达式--二叉树 https://www.cnblogs.com/gw811/archive/2012/10/12/2720777.html

    --END-- 2019年9月4日11点08分

  • 相关阅读:
    awk二十问-【AWK学习之旅】
    awk十三问-【AWK学习之旅】
    awk分割列-【AWK学习之旅】
    securecrt重建
    SQuirrel-GUI工具安装手册-基于phoenix驱动
    phoenix部署手册-基于hbase
    MySQL5.6一键部署
    CSV文件导入导出MySQL
    Percona-Server-5.7.16 启动错误
    Linux下安装php加速软件Xcache
  • 原文地址:https://www.cnblogs.com/heyang78/p/11457783.html
Copyright © 2011-2022 走看看