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

📄 conditiontoken.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.core.tokens;

import jmathlib.core.interpreter.*;
import jmathlib.core.tokens.numbertokens.DoubleNumberToken;

/**Used to implement if-then-else operations within an expression*/
public class ConditionToken extends OperandToken
{
    /**contains the expression condition*/
    private OperandToken condition;
    
    /**contains the code to execute if the condition is true*/
    private OperandToken code;
    
    /**Constructor setting ifRelation and ifCode
       @param _ifRelation = the test relation
       @param _ifCode     = the code to execute if the test is true*/
    public ConditionToken(OperandToken _condition, OperandToken _code)
    {
	   condition   = _condition;
	   code        = _code;
    }

    /**
     * 
     * @return
     */
    public OperandToken getCondition()
    {
	   return condition;
    }

    /**
     * 
     * @return
     */
    public OperandToken getExpression()
    {
	   return code;
    }

    /**evaluates the operator
    @param operands = the operators operands
    @return the result of the test as an OperandToken*/
    public OperandToken evaluate(Token[] operands)
    {
    	if(condition != null)
    	{
	    	ErrorLogger.debugLine("ConditionToken: testing " + condition.toString());
	    	OperandToken result = condition.evaluate(null);
	    	
	    	if(result instanceof DoubleNumberToken)
	    	{
                DoubleNumberToken num = (DoubleNumberToken)result;
                boolean     tag = true;
                
                // all elements must by unequal zero
                for (int i=0; i<num.getNumberOfElements(); i++)
                {
                    if (num.getValueRe(i)==0.0)
                        tag = false;
                }
                
	    		if(tag)
	    		{
	    			// evaluate Code
                    code.evaluate(null);
	    			return DoubleNumberToken.one;
                }
	    	}
            else if(result instanceof LogicalToken)
            {
                LogicalToken l   = (LogicalToken)result;
                boolean      tag = true;
                
                // all elements must be unequal "false"
                for (int i=0; i<l.getNumberOfElements(); i++)
                {
                    if (l.getValue(i)==false)
                        tag = false;
                }

                if (tag)
                {
                    // evaluate Code
                    code.evaluate(null);
                    return DoubleNumberToken.one;
                }
            }
	 	}
        else
        {
            code.evaluate(null);
	 	    return DoubleNumberToken.one;
        }
        	   	
    	return null;
    }
    

    /**Convert the operator to a string
     * @return the operator as a string
     */
    public String toString()
    {
		String retString = "";
        
        if (condition != null)
	        retString = "condition: " + condition.toString();
        else
            retString = "else: ";

        if (code != null)
            retString += " code: " + code.toString();
        else
            retString += " code: blank ";

        return retString;
    }

} // end ConditionToken

⌨️ 快捷键说明

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