📄 expression.java
字号:
children[1] = right.evaluate(null);
//check LEFT side for submatrices (e.g. a(1,:) = [1,2,3] )
if (left instanceof FunctionToken)
{
// A function can never be valid on the left side of an expression
// therefore this MUST be variable
// (e.g. a(3,2)=5 )
FunctionToken function = (FunctionToken)left;
ErrorLogger.debugLine("Expression: eval: function/variable on left side");
// create variable token from function data
children[0] = new VariableToken(function.getName(), function.getOperands());
}
}
else if(data instanceof UnaryOperatorToken && (((UnaryOperatorToken)data).getValue() == '+' ||
((UnaryOperatorToken)data).getValue() == '-' ) )
{
//!!! is this line really necessary !!!!???
//do nothing
}
else if(data instanceof DotOperatorToken)
{
// (e.g. a.getLambda() or a.color or a.argument1)
// don't evaluate children.
}
else
{
// the data of this expression is null or of no interest
// evaluate all children
boolean dispB = false;
for(int i = 0; i < noChildren; i++)
{
if(children[i] != null)
{
// check if result should be displayed
dispB = children[i].isDisplayResult();
// evaluate children
children[i] = children[i].evaluate(null);
// check if result should be displayed before
if (dispB && children[i]!=null)
children[i].setDisplayResult(true);
}
} // end for
}
// ******************************************************************************
// evaluate operator with its children
if(data != null)
{
// display "a=11", do not display "a=11;"
if (isDisplayResult() && (data instanceof AssignmentOperatorToken))
data.setDisplayResult(true);
// evaluate expression
result = data.evaluate(children);
/* set the display state of the result, if the original expression
also has the display state set */
if (isDisplayResult() && (result != null))
{
result.setDisplayResult(true);
}
}
else
{
// operator data is null
// the result of this expression might be hidden in the first child
// (e.g. (2+3)*4 here (2+3) will be hidden inside an expression)
result = children[0];
/*store operand of expressions without data in "ans" variable*/
for(int i = 0; i < noChildren; i++)
{
if (children[i]!=null)
{
ErrorLogger.debugLine("Expression: store ans "+children[i].toString());
Variable answervar = getVariables().createVariable("ans");
answervar.assign(children[i]);
}
/* display the result this expression in the user console*/
if ((children[i] != null) &&
children[i].isDisplayResult() )
{
//ErrorLogger.debugLine("Expression: !!!!!!!!! showResult");
getInterpreter().displayText(" ans = "+ children[i].toString());
}
}
}
return result;
} // end evaluate
/**Converts the expression to a string
@return string representation of expression */
public String toString()
{
String result = "";
if (data != null)
{
result = (data).toString(children);
}
else
{
if (children == null)
return "";
if (children[0] != null)
result = children[0].toString();
}
return result;
}
/**Performs a multiplication
@param argument = the value to multiply the expression by
@return the result as an OperandToken*/
/*public OperandToken multiply(OperandToken argument)
{
return null;
}
*/
/**Raise this object to the power of arg
@param = the value to raise it to the power of
@return the result as an OperandToken*/
/*public OperandToken powerOf(OperandToken argument)
{
return null;
}*/
/**add this token to another
@param arg = the amount to add to it
@return the result as an OperandToken*/
/*public OperandToken add(OperandToken argument)
{
return null;
}
*/
/**subtract this token from another
@param arg = the amount to subtract from it
@return the result as an OperandToken*/
/*public OperandToken subtract(OperandToken argument)
{
return null;
}*/
/**Builds an expression tree
@param op = the expressions operator
@param left = the left hand operand
@param right = the right hand operand
@return the expression created*/
private Expression buildTree(OperatorToken op, OperandToken left, OperandToken right)
{
Expression tree = new Expression(op);
tree.insert(left);
tree.insert(right);
return tree;
}
/**Checks if this operand is a numeric value
@return true if this is a number, false if it's
an algebraic expression*/
public boolean isNumeric()
{
boolean numeric = true;
for(int childNo = 0; childNo < noChildren; childNo++)
{
if(!children[childNo].isNumeric())
numeric = false;
}
return numeric;
}
/**@return the index number of the current child expression*/
public int getChildNo()
{
return childNo;
}
/**@return the expression being executed*/
public OperandToken getCurrentChild()
{
return children[childNo];
}
/**checks if this is a leaf node of the expression tree
@return false if this expression has any children*/
public boolean isLeaf()
{
return (noChildren == 0);
}
/**function to access all children of a node within the expression tree
@return all the nodes children as a enumeration*/
public Enumeration getChildren()
{
return new ExpressionEnumeration();
}
class ExpressionEnumeration implements Enumeration
{
private int index;
public ExpressionEnumeration()
{
index = 0;
}
public boolean hasMoreElements()
{
return (index < noChildren);
}
public Object nextElement()
{
OperandToken element = children[index];
index++;
return element;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -