|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
java.lang.Object | +--ParsedFunction
A simple function parser. Operates recursively on the entire input.
The standard arithmetic operators (+ - * /) are supported, with
the caret symbol (^) meaning "to the power of".
The sin, cos, tan, abs
and exp functions are supported; function arguments should be
enclosed within brackets (eg. sin(x) instead of
sin x. The letter 'x' represents the x
parameter to the function; the name "pi" will give the value of pi.
Extra whitespace between operators is permitted, but not between a function
name and the opening bracket.
The string is searched for a top-level operator (outside of any brackets).
If one is found, the string is split in half, and each half parsed separately
to produce to Expression objects. These are combined with an
appropriate object to represent the operator
(eg. Addition). Lower-precedence operators are searched
for first. Functions names have the opening bracket appended to prevent
precedence confusion (eg. "sin(" is treated as an operator).
| Inner Class Summary | |
(package private) class |
ParsedFunction.Abs
|
(package private) class |
ParsedFunction.Addition
|
(package private) class |
ParsedFunction.Constant
|
(package private) class |
ParsedFunction.Cosine
|
(package private) class |
ParsedFunction.Division
|
(package private) class |
ParsedFunction.Equal
|
(package private) class |
ParsedFunction.Exp
|
(package private) class |
ParsedFunction.Expression
Object to represent an expression which may be evaluated with some parameter x. |
(package private) class |
ParsedFunction.Fn
|
(package private) class |
ParsedFunction.GreaterThan
|
(package private) class |
ParsedFunction.GreaterThanEqual
|
(package private) class |
ParsedFunction.LessThan
|
(package private) class |
ParsedFunction.LessThanEqual
|
(package private) class |
ParsedFunction.Multiplication
|
(package private) class |
ParsedFunction.NotEqual
|
(package private) class |
ParsedFunction.Power
|
(package private) class |
ParsedFunction.Sine
|
(package private) class |
ParsedFunction.Subtraction
|
(package private) class |
ParsedFunction.Tan
|
(package private) class |
ParsedFunction.X
|
| Field Summary | |
ParsedFunction.Expression |
expression
The Expression object representing this function. |
java.lang.String |
string
The string from which the function was parsed. |
| Constructor Summary | |
ParsedFunction(java.lang.String s)
Creates a new function by parsing the supplied string. |
|
| Method Summary | |
double |
evaluate(double x)
|
static int |
findOperator(char op,
java.lang.String s)
Helper method to search for a top-level operator in a string. |
static int |
findOperator(char op,
java.lang.String s,
int startPos)
Helper method to search for a top-level operator in a string, from a specified starting index. |
static void |
main(java.lang.String[] args)
Test harness used for debugging |
ParsedFunction.Expression |
parseExpression(java.lang.String s)
Creates a new Expression object to represent the supplied
string. |
void |
setString(java.lang.String s)
Changes the function and causes a reparse. |
java.lang.String |
toString()
Returns the string from which the function was parsed. |
| Methods inherited from class java.lang.Object |
|
| Field Detail |
public ParsedFunction.Expression expression
Expression object representing this function.public java.lang.String string
| Constructor Detail |
public ParsedFunction(java.lang.String s)
| Method Detail |
public static void main(java.lang.String[] args)
public ParsedFunction.Expression parseExpression(java.lang.String s)
Expression object to represent the supplied
string.
public static int findOperator(char op,
java.lang.String s)
indexOf() method of String,
as there is the additional requirement that the character must occur
outside of any brackets.op - The character to search fors - The string to search
public static int findOperator(char op,
java.lang.String s,
int startPos)
indexOf() method of String, as there is the
additional requirement that the character must occur outside of any
brackets. (Bracket counting is applied to the whole string, regardless of
the starting index.)op - The character to search fors - The string to searchstartPos - The minimum index at which the character should be foundpublic double evaluate(double x)
public void setString(java.lang.String s)
public java.lang.String toString()
|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||