Common class function
{
Common string function;
public int number _ of _ arguments
Public function (string function, int number_of_arguments)
{
This.function = function;
this . number _ of _ arguments = number _ of _ arguments;
}
Common string toString ()
{
Return function;
}
}
Common class operator
{
Common string operator;
Common byte priority;
Common Operator (String Operator, Byte Priority)
{
this.operator = operator
This.priority = priority;
}
Common string toString ()
{
Return operator;
}
}
Common class variable
{
Common string variable;
Common binary value;
Public variables (string variables, double-precision values)
{
This.variable = variable;
this.value = value
}
Common string toString ()
{
Returns a variable;
}
}
Listing 2 shows the token class.
Listing 2. Token class
Public class token
{
Public object token;
Public toll sign;
Public int location;
Public int length;
Common token (object token, character token, integer position, integer length)
{
this.token = token
this.mark = mark
This.position = position;
This.length = length;
}
Common string toString ()
{
return token . tostring()+";" +mark+";" +Position+";" +Length+"
";
}
}
Listing 3. Three kinds of stents
Import java.util.stack;
Public class parentheses _check
{
Public static boolean value is _ open _ parentheses (char c)
{
if ( c=='(' || c=='[' || c=='{ ')
Return true
other
Returns false
}
Public static boolean value is _ closed _ parentheses (char c)
{
if ( c==')' || c==']' || c=='} ')
Return true
other
Returns false
}
Private static Boolean bracket _match( char open, char closed)
{
if(open = = '(' & amp; & ampclosed== ')')
Return true
else if(open = = '[' & amp; & ampclosed==']')
Return true
else if(open = = ' { ' & amp; & closed = ='}')
Return true
other
Returns false
}
Public static Boolean parentheses _valid (string expression)
{
Stack s = new Stack();
int I;
Char current _ char
Character c;
char c 1;
boolean ret = true
for(I = 0; I & ltexp.length (); i++)
{
current _ char = exp . charat(I);
If(is _ left parenthesis (current_char))
{
C = new character (current _ char);
s . push(c);
}
Else if(is _ closed _ parentheses (current_char))
{
if ( s.isEmpty())
{
ret = false
Break;
}
other
{
c =(Character)s . pop();
c 1 = c . char value();
If (! Parentheses _match( c 1, current_char))
{
ret = false
Break;
}
}
}
}
If (! s.isEmpty())
ret = false
Return to ret
}
}
Listing 4. Check the beginning of the correct expression
Private static boolean begin_check (vector marker, range r, StringBuffer err)
{
Character mark;
token t;
t =(Token)tokens . element at(0);
mark = t.mark
if ( mark=='P ')
err . append(messages . begin _ operator);
else if ( mark== ')')
Err. append(messages. begin _ parentheses);
else if ( mark=='Z ')
err . append(messages . begin _ comma);
other
Return true
r . start = 0;
r . end = t . length;
Returns false
}
Listing 5. Find the first closing parenthesis
Public static int pos _ first _ closed _ parentheses (vector marker)
{
token t;
for(int I = 0; I< tokens. size (); i++)
{
t =(Token)tokens . element at(I);
if ( t.mark== ')')
Return I;
}
Returns 0;
}
Listing 6. Found a matching opening parenthesis.
Public static int pos _ open _ bracket (vector marker, int closed _ bracket)
{
int I;
token t;
I = closed _ bracket-2;
while(I & gt; =0 )
{
t =(Token)tokens . element at(I);
if ( t.mark== '(')
{
Return I;
}
I-;
}
Returns 0;
}
Listing 7. Find the operator with the highest priority
Public static int pos_operator (vector mark, range r)
{
Maximum priority of bytes = bytes. MAX _ VALUE
int max _ pos = 0;
Byte priority;
String operator;
token t;
for(int I = r . start+2; I< = r.end-2; i++)
{
t =(Token)tokens . element at(I);
If (t.mark! ='P ')
Continue;
Priority =((Operator)t.token). Priority;
operator=((Operator)t.token)。 Operator;
If (priority & lt max _ priority || (operator. equals ("") ||
operator . equals(" * *))& amp; & Priority = = Maximum Priority)
{
Max _ priority = priority;
max _ pos = I;
}
}
return max _ pos
}
Listing 8. Check whether there are other operators.
...
int poz _ max _ op = pos _ operator(token,range);
//If there is no operator,
if ( poz_max_op==0)
{
If (no more parentheses)
{
Returns false
}
other
{
Double results;
result=function_result( tokens,range . start- 1);
function_tokens_removal( tokens,range . start- 1);
t = new Token ( new Double(result),' D ',0,0);
tokens.setElementAt( t,range . start- 1);
Parentheses _ removed (tokens, range. start-1);
Return true
}
}
...
Listing 9. Get operands and perform operations. ...
Double operand 1, operand 2;
//The first operand comes first. ...
t =(Token)tokens . element at(poz _ max _ op- 1);
Operand 1 = operand _ value (t);
//... The second operand follows the operator.
t =(Token)tokens . element at(poz _ max _ op+ 1);
Operand2 = operand _ value (t);
//operator
t =(Token)tokens . element at(poz _ max _ op);
String op=((Operator)t.token)。 Operator;
double result = operation _ result(operand 1,operand2,op);
tokens . removeelementat(poz _ max _ op+ 1);
tokens . removeelementat(poz _ max _ op);
t = new Token ( new Double(result),' D ',0,0);
tokens.setElementAt( t,poz _ max _ op- 1);
Parentheses _ removed (tokens, poz _ max _ op-1);
...
Listing 10. Get operand
Public Static Double Operand _ Value (Tag T)
{
if ( t.mark=='V ')
Return ((variable) t.token). Value;
else if ( t.mark=='D ')
return ((Double)t.token)。 double value();
else if ( t.mark=='H ')
return base _ convert((String)t . token)。 Substring (2),16);
else if ( t.mark=='O ')
return base _ convert((String)t . token)。 Substring (2), 8);
else if ( t.mark=='B ')
return base _ convert((String)t . token)。 Substring (2), 2);
}
Listing 1 1. Convert a number into a decimal number.
public static long base _ convert(String s,int base)
{
Length r = 0;;
int i,j;
for ( i=s.length()- 1,j = 0; I & gt=0; i -,j++)
r = r+digit _ weight(s . charat(I))*(long)math . pow(base,j);
return r;
}
Public static integer number weight (c character)
{
if ( Character.isDigit( c))
Return to c-48;
else if(' A ' & lt; = c & amp& ampc & lt='f ')
Return to c-55;
else if(' a ' & lt; = c & amp& ampc & lt='f ')
Return to c-87;
return- 1;
}
Listing 13. Remove redundant brackets.
Private static empty parentheses _ remove (vector token, int pos)
{
If (
pos & gt 1。 & amp
amp& amp& amp
amp
((Token)tokens.elementAt( poz-2))。 Mark. = ' F ' & amp& amp
amp& amp& amp
amp
((Token)tokens . element at(poz- 1))。 mark = = '(' & amp; & amp
amp& amp& amp
amp
((Token)tokens . element at(poz+ 1))。 mark== ')'
||
pos = = 1 & amp; & amp
amp& amp& amp
amp
((Token)tokens.elementAt( 0))。 mark = = '(' & amp; & amp
amp& amp& amp
amp
((Token)tokens.elementAt( 2))。 mark== ')'
)
{
tokens . removeelementat(poz+ 1);
tokens . removeelementat(poz- 1);
}
Return;
}
Listing 14. Combines symbols and displays the results.
Public static string token_join (vector token)
{
String result = new string ();
token t;
for(int I = 0; I< tokens. size (); i++)
{
t =(Token)tokens . element at(I);
if ( t.mark=='D ')
{
double n=((Double)t.token)。 double value();
Result = result+formed _ number (n);
}
other
Result = result+t.token;
if ( result.endsWith( ".0 "))
result=result.substring( 0,result . length()-2);
Result = Result+"";
}
Return the result;
}
conclusion
This paper analyzes an applet, which can calculate arithmetic expressions step by step. At the same time, the most interesting code fragments are reviewed in order, and two different expression evaluation methods are discussed.
The next version of W3Eval is expected to be enhanced in various aspects, including the ability to add user-defined functions; Support fractions, complex numbers and matrices; Improved graphical user interface (GUI); Size and speed optimization and security enhancement. I encourage you to provide your own ideas on enhancements.
I hope you will find W3Eval a useful online tool for expression evaluation, which is simpler and more natural than the classical method to some extent. I also hope that the codes and algorithms mentioned here can make you understand that the Java language is helpful to deal with mathematical problems.
! Strongly demand extra points!