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)