The application of infix expression and computing stack

Source: Internet
Author: User

Not finished finishing.

#include <iostream>#include<cstdio>#include<queue>#include<stack>using namespacestd;structPostfixexpre {stringinfix; Postfixexpre (string_infix) {infix=_infix; }    intGetpri (Charc) {Switch(c) { Case '(':                return 1;  Case '+': Case '-':                return 2;  Case '*': Case '/':                return 3;  Case ')':                return 4; default://Number returned 0                return 0; }    }    stringGetpostfix () {BOOLFrist =true; Stack<Char>s; stringPost; intn =infix.size ();  for(intI=0; i<N;) {            intPRI =Getpri (Infix[i]); Switch(PRI) { Case 0://if it is a number, add consecutive digits to the suffix                if(!frist)//used to go out the first spacePost + =" "; ElseFrist=false;  while(I<n &&!)Getpri (Infix[i])) Post+ = infix[i++];  Break;  Case 1://if yes (directly into the stackS.push (infix[i++]);  Break;  Case 2://if it is +-need to popup operator precedence in the stack than Infix[i], and add the prefix, and then infix[i] into the stack                 while(!s.empty () && Getpri (S.top ()) >=2) {Post+=" "; Post+=S.top ();                S.pop (); } s.push (Infix[i++]);  Break;  Case 3://if it is an operator with a higher or equal operator precedence than infix[i] in the */pop-up stack, and adds a prefix, then infix[i] into the stack                 while(!s.empty () && Getpri (S.top ()) >=3) {Post+=" "; Post+=S.top ();                S.pop (); } s.push (Infix[i++]);  Break;  Case 4://if yes) POPs all in the nearest (previous operator, and joins the suffix                 while(S.top ()! ='(') {Post+=" "; Post+=S.top ();                S.pop (); } s.pop (); //Eject (i++; }        }         while(!S.empty ()) {Post+=" "; Post+= +S.top ();        S.pop (); }        returnPost; }    voidPrintstack (stack<int>s) { while(!S.empty ()) {cout<<s.top () <<" ";        S.pop (); } cout<<Endl; }    BOOLIssinnum (intch) {        returnCH >=0&& CH <=9; }    //calculation of result by suffix expression requires a space between the suffix expression format number and the symbol, symbol, and symbol    intGetValue (stringPost) {Stack<int>s; intn =post.size ();  for(intI=0; i<N;) {            intres =0; Charc =Post[i]; if(c>='0'&& c<='9'){//if it is a number, extract all consecutive digits and put that number into the stack                 while(I<n && C! =' ') {res= res*Ten+c-'0'; C= post[++i]; } s.push (res);//Printstack (s);}Else{//is the symbol that pops up the stack in front of the two numbers and calculates and then results into the stack                intb =s.top (); S.pop (); intA =s.top (); S.pop (); Switch(c) { Case '+': res = a+b; Break;  Case '-': res = a-B; Break;  Case '*': res = a*b; Break;  Case '/': res = A/b; Break; } s.push (res);//Printstack (s);i++; } I++;//Skip Spaces        }        returnS.top (); }};intMain () {Postfixexpre P ("121+10* (53-49+20)/((35-25) *2+10) +11"); cout<<p.getpostfix () <<Endl; cout<<p.getvalue ("121 49-20 + * 25-2 * +/+ +");// $GetChar (); return 0;}

The application of infix expression and computing stack

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.