Change infix expression to suffix expression
* ** Infix expression to suffix expression **: converts a long string of computation expressions to a string that is easy to operate on computers, design of the calculator ** involved conversion operator * + -/*() ^ % *** use StringBuilder to save the converted suffix expression * use stack operator *** conversion principle ** 1. the preceding characters have no priority value (), +-the priority value is 1,/* % the priority value is 2, and ^ The priority value is 3*2. for an expression to be calculated, check each character one by one from left to right * 3. if you encounter a number, append it directly to StringBuilder * 4. encounter (parentheses, push directly into the stack * 5. encounter) parentheses, pop the operator in the stack and append it to StringBuilder until it encounters (parentheses, if not found before the stack is empty (parentheses, throw an exception * 6. for other operators, if the top of the stack is (, direct push into the stack * 7. otherwise, if the top-stack operator has a priority value greater than this value, before the stack is empty, Pop the operators in the stack and append them to StringBuilder. * The operator with a lower priority than the stack does not come out of the stack, finally, push the operator into the stack * 8. after checking each character, if there are operators in the stack, append these operators directly to StringBuilder. ** the suffix expression ***/import java is saved in StringBuilder. 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 {writable post EPO = new writable post (); Buffered Reader bf = new BufferedReader (new InputStreamReader (System. in); String infix, postfix = null; while (infix = bf. readLine ())! = Null) {postfix = IBD. Fixed post (infix); System. out. println (postfix) ;}} class fixed post {public String Fixed post (String infix) {Stack
Sk = new Stack
(); StringBuilder sb = new StringBuilder (); char [] ch = infix. toCharArray (); for (int I = 0; I
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