栈的模拟应用:
#include#include #include #include #include #include #include using namespace std;string getPostfixExp(string s){ stack sta;// d n x string exp; int len = s.size(); for(int i=0; i = '0' && s[i] <= '9'){ num += s[i++]; } if(exp[0] != NULL)//后缀表达式的第一个一定是数字,用此来避免首位空格 exp += " "; exp += num; i--; } } return exp;}int calculate(string post){ stack sta; int len = post.size(); for(int i=0; i = 'a') { //如果是运算符 int a = sta.top(); sta.pop(); int b = 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 != ' ') { //如果为数字 int num = 0; while(post[i] >= '0' && post[i] <= '9') num = num*10+post[i++]-'0'; sta.push(num); i--; } } return sta.top(); }int main(){ freopen("d:\\in.txt", "r", stdin); string s; int t; cin>>t; while(t--) { cin>>s; string post = getPostfixExp(s); cout< <