Infix Expression Calculation

Source: Internet
Author: User

This program provides another and cases of the application of the Stack besides what I had mentioned in a previous a Rticle:

(1) Class infixexpr converts an Infix expression to a Suffix expression by using a operator stack;

(2) Class Suffix calculates the value of a Suffix expression by using a stack containing operands.

  Sample Input and Output:

  

1 ImportJava.util.*;2 ImportJava.io.*;3 4 classSuffix {5     PrivateStack<integer>Stack;6     7      PublicSuffix () {8stack =NewStack<integer>();9     }Ten      Public voidAddtoken (String str) { One         if(Str.equals ("+") | | str.equals ("-") | | str.equals ("*") | | AStr.equals ("/") | | Str.equals ("^")) { -             intb =Stack.pop (). Intvalue (); -             intA =Stack.pop (). Intvalue (); the             if(Str.equals ("+")) { -Stack.add (NewInteger (A +b)); -}Else if(Str.equals ("-")) { -Stack.add (NewInteger (A-b)); +}Else if(Str.equals ("*")) { -Stack.add (NewInteger (A *b)); +}Else if(Str.equals ("/")) { AStack.add (NewInteger (A/b)); at}Else { -                 intval = 1; -                  for(inti=0;i<b;i++) { -Val *=A; -                 } -Stack.add (NewInteger (Val)); in             } -}Else{//An operand to Stack.add (integer.valueof (str)); +         } -     } the      Public intGetval () { *         returnStack.peek (). Intvalue (); $     }Panax Notoginseng      Public voidClear () { - stack.clear (); the     } + } A  the  Public classinfixexpr { +     Private StaticBufferedReader in; -     Private StaticStringTokenizer Tokenizer; $     Private StaticStack<string>Stack; $     Private StaticSuffix server; -      -     Private Static voidprintandcal (String expr) { the System.out.println (expr); -Tokenizer =NewStringTokenizer (expr);Wuyi         //Attention:we can ' t use a for-loop based on the         //Tokenizer.counttokens () since it is mutable -          while(Tokenizer.hasmoretokens ()) { WuString str =Tokenizer.nexttoken (); -             if(Str.equals ("(")) { About Stack.add (str); $}Else if(Str.equals (")")) { -String op =Stack.pop (); -                  while(!op.equals ("(")) { - Server.addtoken (OP); AOP =Stack.pop (); +                 } the}Else if(Str.equals ("+") | | str.equals ("-") | | str.equals ("*") | | -Str.equals ("/") | | Str.equals ("^")) { $                  while(!stack.isempty () &&!stack.peek (). Equals ("(") &&Prior (Stack.peek (), str)) { the Server.addtoken (Stack.pop ()); the                 } the Stack.add (str); the}Else{//An operand - Server.addtoken (str); in             } the         } the          while(!Stack.isempty ()) { About Server.addtoken (Stack.pop ()); the         } the System.out.println (Server.getval ()); the     } +     Private Static BooleanPrior (String op1,string op2) { -         //Returns Whether OP1 can be operated first the         //It is aligned with operator OP2Bayi         if(Op2.equals ("^")) { the             return false; the}Else if(Op2.equals ("*") | | Op2.equals ("/")) { -             return!op1.equals ("+") &&!op1.equals ("-")); -}Else {     the             return true; the         } the     } the      Public Static voidMain (string[] args)throwsIOException { -in =NewBufferedReader (NewFileReader ("Infix.in")); thestack =NewStack<string>(); theServer =NewSuffix (); the         intn =Integer.parseint (In.readline ());94 System.out.println (n); the          for(inti=0;i<n;i++) { the server.clear (); the printandcal (In.readline ());98         } About in.close (); -     }101}

Infix Expression Calculation

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.