* * infix expression-to-suffix expression * Function: Convert a long list of expressions into a computer-easy-to-manipulate character sequence for the design of the calculator * * participate in the conversion operator * +-/* () ^% * * * use StringBuilder to save the converted suffix expression Type * Use stack to manipulate operator * * * Conversion principle * 1. There is no priority value in the above character () and +-priority value is 1,/*% priority value is 2,^ priority value is 3 * 2. For an expression to be evaluated, check each character from left to right (3). Encountered number, directly append to Str Ingbuilder * 4. Encountered (parentheses, direct push into stack * 5. Encountered) parentheses, pop out of the stack of operators and append to StringBuilder until encountered (parentheses, if not found before stack empty (parentheses, throws exception * 6. For other operators, if The top of the stack is (, direct push into the stack * 7. Otherwise, if the top operator precedence value is greater than it, then before the stack is empty, the pop is out of the stack and append to StringBuilder, * until it encounters an operator that is smaller than its priority, the operator that is smaller than its precedence does not stack, Finally, push this operator into the stack * 8. After each character is checked, if there are operators in the stack, the operators are append directly into StringBuilder * * Last StringBuilder is the suffix expression * **/import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.util.Stack;public Class Change {public static void main (string[] args) throws Ioexception{intopost itp=new intopost (); BufferedReader bf=new BufferedReader (New InputStreamReader (system.in)); String Infix,postfix=null;while ((Infix=bf.readline ())!=null) {postfix=itp.intopost (infix); System.out.println (Postfix);}}} Class intopost{public string Intopost (string infix) {stack<operator> sk=new stack<operator> (); StringBuilder sb=new StringBuilder (); Char[] Ch=infix.tochararray (); for (int i=0;i<ch.length;i++) {if (Character.isdigit (Ch[i])) Sb.append (Ch[i]), else operators (Sb,sk,ch[i]);} while ( !sk.isempty ()) {Sb.append (Sk.pop (). tmp);} return sb.tostring (); }//dealing with non-numeric portions public void operators (StringBuilder sb,stack<operator> Sk,character ch) {operator chh=new operator ( CH); if (Sk.isempty () | | | ch.equals (' (') | | sk.peek (). Tmp.equals (' (')) {Sk.push (CHH); return;} if (Ch.equals (') ')) {if ( Sk.isempty ()) throw new RuntimeException (); while (!sk.peek (). Tmp.equals (' (')) {sb.append (Sk.pop (). tmp), if (Sk.isempty ()) throw new RuntimeException ();} sk.pop () ; Return } if (CHH. Rank<sk.peek (). RANK) while ((!sk.isempty ()) && (!sk.peek (). Tmp.equals (' ()) && Chh. Rank<=sk.peek (). RANK) {sb.append (Sk.pop (). tmp);} sk.push (CHH); }}///operator Class class operator{int RANK; Character tmp; Public OPerator (Character ch) {this.tmp=ch; if (ch.equals (' + ') | | | ch.equals ('-')) this. rank=1; if (ch.equals (' * ') | | | ch.equals ('/') | | ch.equals ('% ')) this. rank=2; if (ch.equals (' ^ ')) this. rank=3; }}//Test Result input 1-2* (3^6-((9-8) *6) ^2*6)-8 output 1236^98-6*2^6*-*-8-
Infix expression to postfix expression