[Henan Province ACM-fourth session] expression evaluation (NYOJ 305)

Source: Internet
Author: User

Simulation Applications for Stacks:

#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<algorithm>#include<stack>using namespacestd;stringGetpostfixexp (strings) {Stack<Char> STA;//d n x    stringexp; intLen =s.size ();  for(intI=0; i<len; i++){        CharCH =S[i]; if(ch = ='a'|| ch = ='m') {//if it's a letteri + =2;        Sta.push (S[i]); } Else if(ch = ='(') {sta.push (CH); } Else if(ch = =')'){//at this point the top must be (Sta.pop ();//Bounce Away (Exp + =" "; Exp+=Sta.top ();        Sta.pop (); } Else if(ch! =','){//Digital            stringnum;  while(S[i] >='0'&& S[i] <='9') {num+ = s[i++]; }             if(exp[0]! = NULL)//the first of the suffix expressions must be a number to avoid the first spaceExp + =" "; Exp+=num; I--; }     }    returnexp;}intCalculatestringPost) {Stack<int>STA; intLen =post.size ();  for(intI=0; i<len; i++){        intCH =Post[i]; if(Ch >='a') {//if the operator            intA =sta.top (); Sta.pop (); intb =sta.top (); Sta.pop (); if(ch = ='D') Sta.push (a+b); Else if(ch = ='x') Sta.push (Max (A, b)); Else if(ch = ='N') Sta.push (min (A, b)); } Else if(ch! =' ') {//if the number            intnum =0;  while(Post[i] >='0'&& Post[i] <='9') Num= num*Ten+post[i++]-'0';            Sta.push (num); I--; }     }     returnsta.top ();}intMain () {Freopen ("D:\\in.txt","R", stdin); strings; intT; CIN>>T;  while(t--) {cin>>s; stringPost =Getpostfixexp (s); cout<<calculate (POST) <<Endl; }    return 0;}

[Henan Province ACM-fourth session] expression evaluation (NYOJ 305)

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.