Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
简单模拟--注意!单目运算符的处理就好#include <iostream> #include <stack> #include <string> #include <cstdio> using namespace std; int main() { string s; int kase=1; //freopen("Atext.in","r",stdin); while(getline(cin,s)){ int x,y,c; stack<int> val; stack<char> op; for(int i=0;i<s.size();i++){ switch(s[i]) { case '(': op.push(s[i]); break; case '!': //注意如:!!!F,一开始没处理好RE了; if(!op.empty()&&op.top()=='!'){ op.pop();//注意!为单目运算符,一开始没处理好! }else op.push(s[i]); break; case '|': case '&': case ')': while(!op.empty()){ if(op.top()=='!'){ x=val.top(); val.pop(); val.push((!x)); op.pop(); }else if(op.top()=='|') { x=val.top(); val.pop(); y=val.top(); val.pop(); val.push((x|y)); op.pop(); }else if(op.top()=='&') { x=val.top(); val.pop(); y=val.top(); val.pop(); val.push((x&y)); op.pop(); }else break; } if(s[i]==')') op.pop(); else op.push(s[i]); break; case 'V': val.push(1); break; case 'F': val.push(0); break; default : break; } } while(!op.empty()){ if(op.top()=='!'){ x=val.top(); val.pop(); val.push((!x)); op.pop(); }else if(op.top()=='|') { x=val.top(); val.pop(); y=val.top(); val.pop(); val.push((x|y)); op.pop(); }else if(op.top()=='&') { x=val.top(); val.pop(); y=val.top(); val.pop(); val.push((x&y)); op.pop(); } } // cout << val.size() << endl; //cout << op.size() << endl; printf("Expression %d: ",kase++); if(val.top()==1) printf("V\n"); else printf("F\n"); } return 0; } //新运算符比栈顶元素优先级高,入栈否则栈顶元素出栈运算, //直至栈顶元素运算符优先级低于要入栈的运算符或栈空; Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator