Reference from: http://blog.sina.com.cn/s/blog_6047c8870100qapb.html
Package com.lmiky.test;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Stack; /** * * * @author lmiky * */public class Expressiontest {/** * priority comparison * @param operator1 comparison value * @param operato R2 is compared to the value * @return is less than or equal to return false, greater than return true */public boolean Compareprior (String Operator1, String operator2) {if ("(").
Equals (Operator2)) {return true; if ("*". Equals (Operator1) | | "/". Equals (Operator1)) {if ("+". Equals (Operator2) | |
"-". Equals (Operator2)) {return true;
return false;
/** * to the suffix expression: * 1, if it is "(") directly into the stack stack. * 2, if it is ")", then add the stack stack popup operator to the end of the array newexpressionstrs, knowing that "(; * 3, if not parentheses, compare the scanned operator, and the operator at the top of the stack stack.") If the operator that is scanned is higher than the top of the stack operator, the operator is pushed into the stack. Otherwise, the operator in the stack is popped up to the end of the array newexpressionstrs until it encounters an operator or stack empty with a priority lower than the scan, and pushes the scanned operator into the stack. Scan it in turn, until it's over.
If the scan is over, and there are elements in the stack, then the end of the array newexpressionstrs is popped, and the suffix expression is obtained. * @param expressionstrs * @return/public string[] Tosuffixexpression (string[] expressionstrs) {//new form of expression List <String>Newexpressionstrs = new arraylist<string> ();
stack<string> stack = new stack<string> (); for (int i = 0; i < expressionstrs.length i++) {if () (. Equals (Expressionstrs[i))) {//If the opening parenthesis, then the stack Stack.push (
Expressionstrs[i]); else if ("+". Equals (expressionstrs[i)) | | "-". Equals (Expressionstrs[i]) | | "*". Equals (Expressionstrs[i]) | |
'/'. Equals (Expressionstrs[i]) {if (!stack.empty ()) {//Remove first-stack operator String s = Stack.pop ();
if (Compareprior (Expressionstrs[i], s)) {//If the stack value priority is less than the value to be pushed into the stack, continue to push the stack Stack.push (s);
else {//otherwise fetch value newexpressionstrs.add (s);
} stack.push (Expressionstrs[i]); else if (")". Equals (Expressionstrs[i])) {//if it is ")", then out stack until it encounters ("while!stack.empty ()) {String s = stack.pop ()
; if (!) (". Equals (s))
{Newexpressionstrs.add (s);
} else {break;
}} else {Newexpressionstrs.add (expressionstrs[i]); } while (!stack.empty ()) {String s = stack.Pop ();
Newexpressionstrs.add (s);
Return Newexpressionstrs.toarray (new string[0]); The public static void main (string[] args) {//foreground passes the character format, so the test is written in this format String expressionstr = "5;+;(; 4;-;5;+;1;);
(; 6;-;5;+;3;); +;2; ";
Split into an expression array string[] Expressionstrs = Expressionstr.split (";");
string[] Newexpressionstrs = new Expressiontest (). Tosuffixexpression (Expressionstrs);
for (int i = 0; i < newexpressionstrs.length i++) {System.out.print (newexpressionstrs[i]);
} System.out.println ();
Expressionstr = "5;+;(; 4;-;5;*;1;-;4;/;(; 6;*;5;+;3;);/;2;";
Expressionstrs = Expressionstr.split (";");
Newexpressionstrs = new Expressiontest (). Tosuffixexpression (Expressionstrs);
for (int i = 0; i < newexpressionstrs.length i++) {System.out.print (newexpressionstrs[i]); }
}
}