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 |
大牛们帮我看看啊,或者给歌数据也行啊,实在不知道什么bt数据不过啊#include<iostream.h> #define N 10000000 char input[N],stack[400]; int flag; int P(char ch) { if(ch=='('||ch=='@')return 0; else if(ch=='|')return 1; else if(ch=='&')return 2; else return 3; } bool value(char ch) { if(ch=='V')return true; else return false; } char cvalue(bool x) { if(x==true)return 'V'; else return 'F'; } void change()//中缀表达式转后缀 { int i=0; flag=0; char s[100];int f=0; s[f++]='@'; char ch=input[0]; while(ch!='\0') { if(ch==' ')ch=input[++i]; else if(input[i]=='(') { s[f++]='('; ch=input[++i]; } else if(ch==')') { while(s[--f]!='(') stack[flag++]=s[f]; if(s[f-1]=='!')stack[flag++]=s[--f]; ch=input[++i]; } else if(ch=='!') { if(s[f-1]=='!')f--; else s[f++]='!'; ch=input[++i]; } else if(ch=='&'||ch=='|') { char w=s[f-1]; while(P(w)>=P(ch)) { stack[flag++]=w; f--; w=s[f-1]; } s[f++]=ch; ch=input[++i]; } else { stack[flag++]=ch; if(s[f-1]=='!')stack[flag++]=s[--f]; ch=input[++i]; } } while(s[f-1]!='@') { stack[flag++]=s[--f]; } } bool cal()//计算后缀表达式的值 { char s[100]; int f=1;s[f-1]='@'; int i=0; char ch=stack[i]; bool x; while(i<flag) { switch(ch) { case '!':x=!value(s[--f]);break; case '|':x=value(s[--f])|value(s[--f]);break; case '&':x=value(s[--f])&value(s[--f]);break; default :x=value(ch); } if(s[f-1]=='!')s[f-1]=cvalue(!x);//特殊处理"!"; else s[f++]=cvalue(x); ch=stack[++i]; } return value(s[1]); } int main() { int ca=0; while(cin.getline(input,N)) { // cout<<input<<endl; if(input[0]=='\0')continue; ca++; change(); cout<<"Expression "<<ca<<": "<<cvalue(cal())<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator