⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expression.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jmathlib.core.tokens;

import jmathlib.core.interpreter.*;

import java.util.Enumeration;

/**Class implementing a tree where each node has a variable no of children*/
public class Expression extends OperandToken
{
    /**array containing the expressions operands*/
    private OperandToken[] children;

     /**The number of operands*/
    private int noChildren = 0;

    /*the operator being held within the node*/
    private OperatorToken data;
    
    /**Stores the index of the child being executed*/
    private int childNo;

    /**Default constructor - creates an expression with a null operator and no operands*/
    public Expression()
    {
        super(50);// , "EXPRESSION");
        data         = null;
        children     = null;  //new OperandToken[5];
    }

    /**Creates an expression with no operands
    @param _data = the expressions operator*/    
    public Expression(OperatorToken _data)
    {
        super(50); //, "EXPRESSION");
        data         = _data;
        children     = null; 
    }

    /**Creates an expression with one operand
    @param _data = the expressions operator
    @param operand = the expressions operand*/    
    public Expression(OperatorToken _data, OperandToken operand)
    {
        super(50); //, "EXPRESSION");
        data         = _data;
        children     = new OperandToken[1]; 
        children[0] = operand;
        noChildren     = 1;
    }
    
    /**Creates an expression with two operands
    @param _data = the expressions operator
    @param left  = the left hand operand
    @param right = the right hand operand*/    
    public Expression(OperatorToken _data, OperandToken left, OperandToken right)
    {
        super(50); //, "EXPRESSION");
        data         = _data;
        children    = new OperandToken[2]; 
        children[0] = left;
        children[1] = right;
          noChildren  = 2;
    }

    /**Creates an expression with three operands
    @param _data   = the expressions operator
    @param first   = the left hand operand
    @param second  = the middle operand
    @param third   = the right hand operand*/    
    public Expression(OperatorToken _data, OperandToken op1, 
                                           OperandToken op2,
                                           OperandToken op3)
    {
        super(50); 
        data        = _data;
        children    = new OperandToken[3]; 
        children[0] = op1;
        children[1] = op2;
        children[2] = op3;
        noChildren  = 3;
    }

    /**Creates an expression with an array of operands
    @param data         = the expressions operator
    @param operands     = and array of operands
    @param _noChildren     = the number of operands*/    
    public Expression(OperatorToken _data, OperandToken[] _children, int _noChildren)
    {
        super(50); //, "EXPRESSION");
        data         = _data;
        children     = _children;
        noChildren   = _noChildren;
    }

    /**retrieves the data object
    @return the operator assigned to the expression*/
    public OperatorToken getData()
    {
        return data;
    }
    
    /**@return the number of children*/
    public int getNumberOfChildren()
    {
        return noChildren;
    }

    /**Sets the operator of the expression
    @param _data = the expressions operator*/
    public void setData(OperatorToken _data)
    {
        data = _data;
    }

    /**Get the a child with a specific index
    @param childNo = the index of the operand
    @return the specified operand*/
    public OperandToken getChild(int childNo)
    {
        return children[childNo];
    }

    /**set the child with a specific index
    @param childNo = the index of the child
    @param child = the value to set it to
    */
    public void setChild(int childNo, OperandToken child)
    {
        children[childNo] = child;
    }
    
    /**Get the first child of this node
    @return the first operand*/
    public OperandToken getLeft()
    {
        return children[0];
    }

    /**Get the last child of this node
    @return the last operand*/
    public OperandToken getRight()
    {
        return children[noChildren - 1];
    }

    /**insert a child node on this Expression
    if _data is a Expression then it just gets added to the current node
    Otherwise a new node is created and this is added to the current node
    @param _data = the operand to add*/
    public void insert(Token _data)
    {
        OperandToken subExpression;

        //if the item is a Expression node then just add it to the Expression
        //without creating a new node
        if(_data instanceof Expression)
            subExpression = ((Expression)_data);
        else if(_data instanceof OperandToken)
            subExpression = ((OperandToken)_data);
        else
            subExpression = new Expression(((OperatorToken)_data));

        // check if no of children exceeds dimension of array
        if (children != null)
        {
            if (noChildren >= children.length )
            {
                int childrenOldLength = children.length;

                ErrorLogger.debugLine("Expression: expand children array "+childrenOldLength);

                OperandToken[] childrenTemp = new OperandToken[childrenOldLength];

                // save old children array to temporary array 
                for (int i=0; i<childrenOldLength; i++)
                {
                    childrenTemp[i] = children[i];
                }

                // create new children array: size +1
                children = new OperandToken[ childrenOldLength + 1 ];
        
                // restore temporary array to new children array
                for (int i=0; i<childrenOldLength; i++)
                {
                    children[i] = childrenTemp[i];
                }
            }
        }
        else
        {
            // create first children. Expression didn't have children before
            children = new OperandToken[1];
        }
        
        children[noChildren] = subExpression;

        noChildren++;
    }

    /**evaluate the data item held within this node
    @param ops = the expressions operands
    @return the result of the expression as an OperandToken*/
    public OperandToken evaluate(Token[] ops)
    {
        OperandToken result = null;
        ErrorLogger.debugLine("Expression: evaluate " + toString());
        
        // for assignments (e.g.: a=3+3) only evaluate the right side
        if(data instanceof AssignmentOperatorToken)        
        {    
            // data is an assignment (e.g. a=3 or [x,t]=func(....) or a(:,3) = [...] )
            OperandToken left  = children[0];
            OperandToken right = children[1];
            //ErrorLogger.debugLine("Expression: evaluate assignment");
            //ErrorLogger.debugLine("Expression: evaluate assignment "+left.toString());
            //ErrorLogger.debugLine("Expression: evaluate assignment "+right.toString());
            
                           
            /* Check how many arguments are on the LEFT-hand side and pass this */
            /*  number to a possible function token on the RIGHT-hand side.     */ 
            if ((left  instanceof MatrixToken)  && 
                (right instanceof FunctionToken)   ) 
            {
                int x = 1;
                
                x = ((MatrixToken)left).getSizeX();
                ErrorLogger.debugLine("Expression: [ "+x+" ]=func()");
                FunctionToken func = ((FunctionToken)right);
                func.setNoOfLeftHandArguments(x);
                children[1] = func;
            }
        
            // evaluate right-hand argument (e.g. a= 2+3)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -