An expression contains a + 、—、 *,/, () operator, to evaluate the value of an expression. Here the use of stacks, respectively, to store the operands and operators, the general idea is to use the double stack to convert infix expression to the suffix expression to calculate.
1. Splitting numbers and operators
Encountered operator truncation, storage, emptying.
2. Turn suffix expression
Stack a holds the suffix expression, stack B holds the operator
The top operator of the B stack is A1, and the current operator is A2 (if A2 is a direct a2->a of numbers)
1) If a1>a2,a1->a,a2->b
1) If a1<a2,a2->b;
2) If A1>=A2
1. If the a1>a2,a1->a; (as long as the condition is satisfied, executes, so that the high priority operator is placed in a)
2. After the advanced operators are excluded, to determine the A1=A2? (that is, whether or not to meet)
If the left and right brackets, then put the parentheses popped, otherwise the a2->b. Can always exclude advanced arithmetic, but can only offset an opening parenthesis
PS:A1>A2 indicates that the relationship between A1 A2,A1 and A2 in this formula is established in the initialization.
3. Calculating suffix expressions
Scan from left to right, handle first two digits when operator is encountered
First, convert the given string to infix expression, and use list<string> to save the expression: The program code is as follows
/**
* Convert a string to a list
*
* @param str
* @return
*/
Public arraylist<string> getstringlist (String str) {
arraylist<string> result = new arraylist<string> ();
String num = "";
for (int i = 0; i < str.length (); i++) {
if (Character.isdigit (Str.charat (i)) | | Str.charat (i) = = '. ') {
num = num + str.charat (i);
} else {
if (num! = "") {
Result.add (num);
}
Result.add (Str.charat (i) + "");
num = "";
}
}
if (num! = "") {
Result.add (num);
}
return result;
}
Then, the infix expression is converted to a suffix expression, the program code is as follows:
/**
* Convert infix expression to suffix expression
*
* @param inorderlist
* @return
*/
Public arraylist<string> Getpostorder (arraylist<string> inorderlist) {
arraylist<string> result = new arraylist<string> ();
stack<string> stack = new stack<string> ();
Stack.push ("#");
for (int i = 0; i < inorderlist.size (); i++) {
if (Character.isdigit (Inorderlist.get (i). CharAt (0))) {
Result.add (Inorderlist.get (i));
} else {
Switch (Inorderlist.get (i). CharAt (0)) {
Case ' (':
Stack.push (Inorderlist.get (i));
Break
Case ') ':
while (!stack.empty () &&!stack.peek (). Equals ("(")) {
Result.add (Stack.pop ());
}
if (!stack.empty ()) {
Stack.pop ();
}
Break
Default
while (!stack.empty () &&! (" # ". Equals (Stack.peek ()))
&& Compare (Stack.peek (), Inorderlist.get (i))) {
Result.add (Stack.pop ());
}
Stack.push (Inorderlist.get (i));
Break
}
}
}
while (!stack.empty () &&! (" # ". Equals (Stack.peek ()))) {
Result.add (Stack.pop ());
}
return result;
}
Second, you have to compare the precedence of the operators, as shown in the program code:
/**
* Comparison operator level
*
* @param Peek
* @param cur
* @return
*/
public static Boolean compare (string peek, string cur) {
if ("*". Equals (Peek)
&& ("/". Equals (cur) | | "*". Equals (cur) | | "-". Equals (cur) | | "+"
. Equals (cur)) {
return true;
} else if ("/". Equals (Peek)
&& ("/". Equals (cur) | | "*". Equals (cur) | | "-". Equals (cur) | | "+"
. Equals (cur)) {
return true;
} else if ("+". Equals (Peek) && ("+". Equals (cur) | | "-". Equals (cur))) {
return true;
} else if ("-". Equals (Peek) && ("+". Equals (cur) | | "-". Equals (cur))) {
return true;
} else if ("#". Equals (Peek)) {
return true;
}
return false;
}
Finally, the value of the expression is evaluated, and the program code is as follows:
/**
* Calculate suffix expression
*
* @param postorder
* @return
*/
Public Float Calculate (arraylist<string> postorder) {
stack<float> stack = new stack<float> ();
for (int i = 0; i < postorder.size (); i++) {
if (Character.isdigit (Postorder.get (i). CharAt (0))) {
Stack.push (float.valueof (Postorder.get (i)));
} else {
float back = (float) stack.pop ();
float front = (float) stack.pop ();
float result = 0F;
Switch (Postorder.get (i). CharAt (0)) {
Case ' + ':
result = front + back;
Break
Case '-':
result = Front-back;
Break
Case ' * ':
result = Front * back;
Break
Case '/':
result = Front/back;
Break
}
Stack.push (result);
}
}
return Stack.pop ();
}
Define a test class, test the code
public static void Main (string[] args) {
evaluateexpress exp = new evaluateexpress ();
String str = "24+ (89-23*6)/3";
arraylist<string> result = Exp.getstringlist (str);
System.out.print ("infix expression:");
for (String S:result) {
System.out.print (s);
}
System.out.println ("\ n------------------");
arraylist<string> Postresult = Exp.getpostorder (result);
System.out.print ("suffix expression:");
for (String Strs:postresult) {
System.out.print (STRs);
}
System.out.println ("\ n-------------");
float Calresult = exp.calculate (Postresult);
System.out.println ("Calculated as:" + Calresult);
}
expression evaluation based on Java language