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 |
谁能帮我看一下啊?Discuss的所有建议都通过了,但就是WA#include<iostream> #include<string> #include<cstdlib> #include<cstdio> #include<cmath> #include <stack> #define max 200 using namespace std; long double str2num(const char *s) { long double num; sscanf(s, "%lf", &num); return num; } char ex[max]; //存储后缀表达式 string str; //存储原算术表达式 void trans() //将算术表达式转化为后缀表达式 { char stack[max]; //作为栈使用 char ch; int i,j,t,top=0; i=0; //获取用户输入的表达式 str.append("#"); t=0;i=0; ch=str[i];i++; while(ch!='#'){ switch(ch){ case '(': //判定为左括号 stack[++top]=ch; break; case ')': //判定为右括号 while(stack[top]!='('&&top>-1) ex[t++]=stack[top--]; top--; break; case '|': //判定为或 while(top!=0&&stack[top]!='(') ex[t++]=stack[top--]; stack[++top]=ch; break; case '&': //判定为且 while(stack[top]=='&') ex[t++]=stack[top--]; stack[++top]=ch; break; case '!': stack[++top] = ch; break; case '#': break; case ' ': break; case 'V': case 'F': while(ch=='V'||ch=='F') //判定为布尔 { ex[t++]=ch; ch=str[i++]; } i--; break; } ch=str[i];i++; } while(top!=0) ex[t++]=stack[top--]; ex[t]='#'; } void compvalue(int Case){ //计算后缀表达式的值 bool stack[max],d; //作为栈使用 char ch; int t=0,top=0; //t为ex下标,top为stack下标 ch=ex[t++]; string digit; while(ch!='#'){ switch(ch){ case '|': if(stack[top-1]==1||stack[top]==1)stack[top-1]=1; else stack[top-1]=0; top--; break; case '&': if(stack[top-1]==1&&stack[top]==1)stack[top-1]=1; else stack[top-1]=0; top--; break; case '!': if(stack[top]==1)stack[top]=0; else stack[top]=1; break; case 'V': stack[++top]=1; break; case 'F': stack[++top]=0; break; } ch=ex[t];t++; } printf("Expression %d: %c\n", Case, stack[top]?'V':'F'); } int main(){ int Case = 0; char *s = new char[255]; while(gets(s)) { str = string(s); Case++; trans(); compvalue(Case); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator