
types.MultiplicationBinaryOpNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tx2ex Show documentation
Show all versions of tx2ex Show documentation
A library that parses and evaluates mathematical formulas in textual format.
The newest version!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package types;
import core.NodeRecognizedInfo;
import core.VariablesManager;
import exceptions.NonIntegralIndexException;
import java.util.ArrayList;
import java.util.List;
import core.Token;
import core.Utils;
import exceptions.EvaluationException;
import exceptions.InvalidFormatException;
/**
*
* @author haitham
*/
public class MultiplicationBinaryOpNode extends AbstractBinaryOpNode {
public MultiplicationBinaryOpNode(AbstractNode parent, List allTokens) {
super(parent, allTokens);
}
public MultiplicationBinaryOpNode(
AbstractNode parent, List allTokens,
AbstractNode op1, AbstractNode op2) {
super(parent, allTokens, op1, op2);
}
@Override
public double evaluate() throws EvaluationException {
return getChild(0).evaluate() * getChild(1).evaluate();
}
public static NodeRecognizedInfo findLastOccurence(
List targetTokens,
List allTtokens,
AbstractNode parent,
VariablesManager vm) throws InvalidFormatException {
// Loop over all tokens from the end to the beginning
for (int i = targetTokens.size() - 1; i >= 0; i--) {
// Check if the token is enclosed in brackets, braces or parentheses
if (!Utils.surrounded(targetTokens.get(i), targetTokens)) {
// Try to match the operator
if (targetTokens.get(i).getString().equals("*")) {
List> childrenTokendsLists = new ArrayList<>();
// Create left child tokens
List leftChildTokens = new ArrayList<>();
for (int j = 0; j < i; j++) {
leftChildTokens.add(targetTokens.get(j));
}
// The following error is generated if no left child is
// found. (remember that this library supports a generic
// fail-safe error checking for missing operands, however it
// does not tell details like which operand is missing.
// That's why we provide here this additional error checking
// tailored specifically for the case in hand)
if (leftChildTokens.isEmpty()) {
throw new InvalidFormatException(
targetTokens.get(i),
allTtokens,
"Multiplication operator (*) left operand is missing.");
}
childrenTokendsLists.add(leftChildTokens);
// Create right child tokens
List rightChildTokens = new ArrayList<>();
for (int j = i + 1; j < targetTokens.size(); j++) {
rightChildTokens.add(targetTokens.get(j));
}
// The following error is generated if no right child is
// found. (remember that this library supports a generic
// fail-safe error checking for missing operands, however it
// does not tell details like which operand is missing.
// That's why we provide here this additional error checking
// tailored specifically for the case in hand)
if (rightChildTokens.isEmpty()) {
throw new InvalidFormatException(
targetTokens.get(i),
allTtokens,
"Multiplication operator (*) right operand is missing.");
}
childrenTokendsLists.add(rightChildTokens);
// Return
return new NodeRecognizedInfo(
i,
i,
new MultiplicationBinaryOpNode(parent, allTtokens),
childrenTokendsLists);
}
}
}
// Return null if the operator was not found
return null;
}
@Override
public String toString() {
return String.format("(%s * %s)", getChild(0).toString(), getChild(1).toString());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy