|
![]() |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsunlabs.brazil.util.Calculator (view source)
public class Calculator
Calculator implements a simple arithmetic expression evaluator. It
can evaluate typical expressions with the "normal" operators and
precedence. Formally, the BNF for the supported grammar is:
A <letter> is defined as a Java
<stmt> ::= <var> = <expr> | <expr>
<expr> ::= <rexpr> | <expr> <bool op> <rexpr>
<bool op> ::= && | <or>
<or> ::= ||
<rexpr> ::= <aexpr> | <rexpr> <rel op> <aexpr>
<rel op> ::= < | <= | > | >= | == | !=
<aexpr> ::= <term> | <aexpr> <add op> <term>
<add op> ::= + | -
<term> ::= <factor> | <term> <mult op> <factor>
<mult op> ::= * | / | %
<factor> ::= <var> | <num> | ! <factor> | ( <expr> )
<var> ::= <letter> | <var> <var2>
<var2> ::= <letterordigit> | . | _
<num> ::= <unum> | + <unum> | - <unum>
<unum> ::= <int> | <int> . | <int> . <int> | . <int>
<int> ::= <digit> | <int> <digit>
char
for which
Char.isLetter(char)
is true
. A
<letterordigit> is defined as a Java char
for which
Char.isLetterOrDigit(char)
is true
. A digit
is defined as a Java char
for which
Char.isDigit(char)
is true
.
Values for <var>
are looked up in the supplied
Dictionary
. If <var>
can not be found,
it is assumed to have the value zero. If the value found is "true" or
"yes" (case insensitive), it is assumed to be one. Similarly, if the
value found is "false" or "no", it is assumed to be zero. Assignment
to <var>
stores the computed value in the same
Dictionary
.
The period in <unum>
, if there is one, must be
immediately adjacent to surrounding <int>
s.
Field Summary | |
---|---|
boolean |
debugging
Set true for debug output. |
Constructor Summary | |
---|---|
Calculator()
The no argument constructor will create an internal Hashtable in which it looks up and stores values
associated with variables. |
|
Calculator(Dictionary d)
This constructor will use the Dictionary parameter to
lookup and store values associated with variables. |
Method Summary | |
---|---|
String |
getValue(String stmt)
Computes the value of the statement passed in the parameter string and returns a string representation of the result. |
String |
getValue(String stmt,
Dictionary d)
Computes the value of the statement passed in the parameter string and returns a string representation of the result. |
static void |
main(String[] args)
A test driver for the calculator. |
void |
stringsValid(boolean allStringsValid)
Normally, variables whose values are "on", "yes", or "true" and converted to "1.0", while the values "off", "no", and "false" are converted to "0.0". |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public boolean debugging
true
for debug output. The output probably won't
make sense to anyone other than the author.
Constructor Detail |
---|
public Calculator()
Hashtable
in which it looks up and stores values
associated with variables.
Hashtable
public Calculator(Dictionary d)
Dictionary
parameter to
lookup and store values associated with variables.
d
- the Dictionary
object
that serves as a symbol tableDictionary
Method Detail |
---|
public void stringsValid(boolean allStringsValid)
public String getValue(String stmt) throws ArithmeticException
null
is
returned.
stmt
- a string representation of
an arithmetic expression or
assignment
null
ArithmeticException
- occurs when a result is
improper (e.g. infinity) or
when the input statement can
not be parsedpublic String getValue(String stmt, Dictionary d) throws ArithmeticException
null
is
returned. The second parameter is used as a symbol table
for the duration of this method call. Note this method is
not thread safe!
stmt
- a string representation of
an arithmetic expression or
assignmentd
- the temporary symbol table
null
ArithmeticException
- occurs when a result is
improper (e.g. infinity) or
when the input statement can
not be parsedpublic static void main(String[] args)
args
- required signature for
main
method, not used
|
Version Kenai-svn-r24, Generated 08/18/09 Copyright (c) 2001-2009, Sun Microsystems. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |