opale.mathtools.parser
Class Parser

java.lang.Object
  |
  +--opale.mathtools.parser.Parser

public class Parser
extends java.lang.Object

This class can parse a string definition of a function and return its value. It's possible to choose the independent variable that must be recognized (for instance 'x', 'y', 'x1' etc...).

The recognised operations are : +, -, *, /, (, ), ^

The recognised functions are :

Moreover, to add functions is possible (void addFunc(UnaryFunction f)).

The recognised constants are : Pi, E

This parser is not case sensitive: "e" and "E" will be interpreted as the string "e".


Constructor Summary
Parser()
          Instantiate an object of this class
Parser(java.lang.String s)
          Instantiate an object of this class with specifying a string to be parsed
 
Method Summary
 void addCst(java.lang.String name, double value)
          Add a constant.
 void addFunc(UnaryFunction f)
          Add a function for the string analyser.
 void addVar(java.lang.String name)
          Add a variable.
 double eval()
          Evaluate the function represented by the string.
 double getVar(java.lang.String name)
          Get the value of a variable.
static void main(java.lang.String[] arg)
           
 void parseString()
          Parse the string defined in this class.
 void parseString(java.lang.String s)
          Parse a string
 void setVar(java.lang.String name, double value)
          Set the value of a variable.
 void setVar(java.lang.String name, java.lang.String value)
          Set the value of a variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parser

public Parser()
Instantiate an object of this class

Parser

public Parser(java.lang.String s)
Instantiate an object of this class with specifying a string to be parsed
Parameters:
s, - the string that will be parsed.
Method Detail

parseString

public void parseString(java.lang.String s)
Parse a string
Parameters:
s, - the string that will be parsed.

parseString

public void parseString()
Parse the string defined in this class.

eval

public double eval()
Evaluate the function represented by the string.
Returns:
double, the value

addFunc

public void addFunc(UnaryFunction f)
Add a function for the string analyser.
Parameters:
UnaryFunction - f, a function that must be recognized in a string

addVar

public void addVar(java.lang.String name)
Add a variable.
Parameters:
String, - the keyword of the variable that must be parsed (x, y, x2, a etc...).

setVar

public void setVar(java.lang.String name,
                   double value)
Set the value of a variable.
Parameters:
String, - the keyword of the variable to set.
double, - the value of this variable.

setVar

public void setVar(java.lang.String name,
                   java.lang.String value)
Set the value of a variable.
Parameters:
String, - the keyword of the variable to set.
String, - a string representation of the value.

getVar

public double getVar(java.lang.String name)
Get the value of a variable.
Parameters:
String, - the keyword of a variable.
Returns:
double, the value of this variable.

addCst

public void addCst(java.lang.String name,
                   double value)
Add a constant.
Parameters:
String, - the keyword of the constant.
double, - the value of this constant.

main

public static void main(java.lang.String[] arg)