public class TermParser extends Object implements Serializable
operator
and can be displayed by implementing the following snippet:
String[] text = { "Variables","Functions", "Binary Infix Operators","Ternary Operators" }; for (int i = 0; i < TermParser.operator.length; i++) { System.out.println(text[i] + ":"); for (int j = 0; j < TermParser.operator[i].length; j++) { System.out.print(TermParser.operator[i][j] + ", "); } System.out.println("\n"); }Fractional numbers (i.e.,
double
values) have to be
inputted with a decimal point (not a comma).Modifier and Type | Field and Description |
---|---|
static String[] |
constantName
Names of predefined constants.
|
static double[] |
constantValue
Values of the predefined constants.
|
static int |
maxOpLength
Maximum number of letters an operator can have.
|
static String[][] |
operator
Table of predefined operators.
|
Constructor and Description |
---|
TermParser(String formula)
Creates a term parser from a single formula in usual notation,
infix for binary operators and prefix for functions and ternary operators.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
checkSyntax(String formula)
Analyses and parses the function being input as a string.
|
static BigDecimal |
evaluate(String formula,
MathContext mc)
Evaluates the function (in postfix notation) at the value x.
|
String |
getFormula(boolean postfix)
Returns a string of the formula, either in postfix or in
usual notation.
|
public static final String[] constantName
public static final double[] constantValue
public static final String[][] operator
public static final String[][] operator = { {// unary operators: "ln", "ld", "exp", "log", "sqrt", "w", "sin", "cos", "tan", "cot", "sec", "csc", "asin", "acos", "atan", "acot", "sinh", "cosh", "tanh", "coth", "arsinh", "arcosh", "artanh", "arcoth", "brown" }, {// binary operators: "+", "-", "*", "/", "%", "^", "mod", "=", "<", ">", "==", "<=", ">=", "&&", "|", "&", "and", "or", "xor", ";", "," }, {// ternary operators: "if", "modPow" }, };
public static int maxOpLength
public TermParser(String formula)
operator
and can be displayed by implementing the following snippet:
String[] text = { "Variables","Functions", "Binary Infix Operators","Ternary Operators" }; for (int i = 0; i < TermParser.operator.length; i++) { System.out.println(text[i] + ":"); for (int j = 0; j < TermParser.operator[i].length; j++) { System.out.print(TermParser.operator[i][j] + ", "); } System.out.println("\n"); }Fractional numbers (i.e.,
double
values) have to be
inputted with a decimal point (not a comma).formula
- a string of the function in usual notationpublic static boolean checkSyntax(String formula)
formula
- a string representing a functionpublic String getFormula(boolean postfix)
((2*x) + 2)
, and prefix notation for functions and
ternary operators, e.g., sin(x)
or
if(-1 < x && x < 1; 1; 0)
.postfix
- specifies whether the returned string is in postfix notationpublic static BigDecimal evaluate(String formula, MathContext mc)
formula
- the formula in postfix notation, each array element being a postfix componentmc
- the mathcontext determining the precision