#include<iostream> #include<stack> #include<vector> using namespace std; int main(){ int n; int c=0; stack<int> st; int flag=1; while(cin>>n){ if(n==0) break; getchar(); string s; c++; cout<<"Case #"<<c<<":"<<endl; for(int i=0; i<n; i++){ vector<int> v(6, 0); getline(cin, s); for(int j=0; j<s.size(); j++){ if(st.empty() && s[j]>='0' && s[j]<='9'){ st.push(s[j]-48); } else if(!st.empty() && s[j]>='0' && s[j]<='9'){ int num = st.top(); st.pop(); v[flag]=num; st.push(s[j]-48); } else if(s[j]=='X'){ flag=1; int num=st.top(); v[flag-1]+=num; st.pop(); } else if(s[j]=='Y'){ flag=3; int num=st.top(); v[flag-1]+=num; st.pop(); } else if(s[j]=='Z'){ flag=5; int num=st.top(); v[flag-1]+=num; st.pop(); } } int num=st.top(); st.pop(); v[flag]=num; if(v[1]>=1){ v[0] = v[0]*v[1]; v[1]--; } if(v[3]>=1){ v[2] = v[2]*v[3]; v[3]--; } if(v[5]>=1){ v[4] = v[4]*v[5]; v[5]--; } if(v[1]>=1) cout<<v[0]<<" * X ^ "<<v[1]; else if(!(v[0]==0&&v[1]==0)) cout<<v[0]; if((!(v[0]==0&&v[1]==0))&&(!(v[2]==0&&v[3]==0))) cout<<" + "; if(v[3]>=1) cout<<v[2]<<" * Y ^ "<<v[3]; else if(!(v[2]==0&&v[3]==0)) cout<<v[2]; if((!(v[2]==0&&v[3]==0))&&(!(v[4]==0&&v[5]==0))) cout<<" + "; else if(v[2]==0&&v[3]==0&&(!(v[0]==0&&v[1]==0))&&(!(v[4]==0&&v[5]==0))) cout<<" + "; if(v[5]>=1) cout<<v[4]<<" * Z ^ "<<v[5]; else if(!(v[4]==0&&v[5]==0)) cout<<v[4]; cout<<endl; } } return 0; } |
Double click to view unformatted code.