
com.zsoltfabok.arithmetic.expression.evaluator.Function Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of arithmetic.expression.evaluator Show documentation
Show all versions of arithmetic.expression.evaluator Show documentation
A very simple arithmetic expression evaluator
The newest version!
package com.zsoltfabok.arithmetic.expression.evaluator;
import java.util.Enumeration;
import java.util.Hashtable;
public class Function
{
public Function()
{
intervals = new Hashtable();
}
public void addInterval(Interval interval, String function)
{
intervals.put(interval, function);
}
public double getValue(double x)
throws NotInDomainException
{
Enumeration keys = intervals.keys();
boolean found = false;
Interval actualInterval = null;
while(keys.hasMoreElements() && !found)
{
actualInterval = (Interval)keys.nextElement();
if(actualInterval.inInterval(x))
found = true;
}
double value;
if(found)
{
String function = (String)intervals.get(actualInterval);
function = function.replaceAll("x", "(" + (new Double(x)).toString() + ")");
value = Calculate.evaluate(function);
} else
{
throw new NotInDomainException(x);
}
return value;
}
private Hashtable intervals;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy