1 Packaget201401;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 ImportJava.util.Stack;8 9 Public classT1502 {Ten One /** A * Priority Comparison - * @paramOperator1 Comparison Value - * @paramOperator2 By comparison value the * @returnless than or equal to return false, greater than return True - */ - Static BooleanCompareprior (String operator1, String operator2) { - if("(". Equals (Operator2)) { + return true; - } + if("*". Equals (Operator1) | | "/". Equals (Operator1)) { A if("+". Equals (Operator2) | | "-". Equals (Operator2)) { at return true; - } - } - return false; - } - in /** - * to suffix expression: to * 1, if it is "(" press directly into the stack stack.) + * 2, if ")", from the stack stack pop-up operator is added to the end of the array newexpressionstrs, know to Encounter "("; - * 3, if the non-brackets, compare the scan to the operator, and stack stack top operator. If the scanned operator takes precedence over the top of the stack operator, the operator is pressed into the stack. Otherwise, the stack operator pops up to the end of the array newexpressionstrs until it encounters a lower priority than the scan-to operator or stack, and presses the scanned operator into the stack. Scan it in turn, until you know it's over. If the scan is finished and there are elements in the stack, the suffix expression is appended to the end of the array newexpressionstrs. the * @paramExpressionstrs * * @return $ */Panax Notoginseng Staticstring[] tosuffixexpression (string[] expressionstrs) { - //the newly formed expression theList<string> Newexpressionstrs =NewArraylist<string>(); +stack<string> stack =NewStack<string>(); A for(inti = 0; i < expressionstrs.length; i++) { the if("(". Equals (Expressionstrs[i)) {//if it is an opening parenthesis, 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-in-stack operators $String s =Stack.pop (); - if(Compareprior (Expressionstrs[i], s)) {//if the stack value priority is less than the value to be placed on the stack, continue to press into the stack - Stack.push (s); the}Else{//otherwise , the value is removed - Newexpressionstrs.add (s);Wuyi } the } - Stack.push (Expressionstrs[i]); Wu}Else if(")". Equals (Expressionstrs[i])) {//if ")", then out of the stack, until you encounter "(" - while(!Stack.empty ()) { AboutString s =Stack.pop (); $ if(!" (". Equals (s)) { - Newexpressionstrs.add (s); -}Else { - Break; A } + } the}Else { - Newexpressionstrs.add (Expressionstrs[i]); $ } the } the while(!Stack.empty ()) { theString s =Stack.pop (); the Newexpressionstrs.add (s); - } in returnNewexpressionstrs.toarray (NewString[0]); the } the About Public Static voidMain (string[] args) { the the //the string expression to use theString expressionstr = "a+b/(a+a) +c"; +map<string,double> map =NewHashmap<string,double>(); -Map.put ("A", 1.0); theMap.put ("B", 2.0);BayiMap.put ("C", 3.0); the the - //split into an array of expressions -string[] STRs = Expressionstr.split (""); thestring[] Newstrs =tosuffixexpression (STRs); thestack<double> stack =NewStack<double>(); the Double Cache; the for(inti = 0; i < newstrs.length; i++) { - if("*". Equals (Newstrs[i])) { theStack.push (Stack.pop () *Stack.pop ()); the}Else if("/". Equals (Newstrs[i])) { theCache =Stack.pop ();94Stack.push (Stack.pop ()/cache); the}Else if("+". Equals (Newstrs[i])) { theStack.push (Stack.pop () +Stack.pop ()); the}Else if("-". Equals (Newstrs[i])) {98Cache =Stack.pop (); AboutStack.push (Stack.pop ()-cache); -}Else{101 Stack.push (Map.get (newstrs[i));102 }103 }104 the System.out.println (Stack.pop ());106 }107 108}
Inverse Polish formula for calculating strings