Change infix expression to suffix expression

Source: Internet
Author: User

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
    
     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.