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 |
能过sample,!!!!!F,((((((V))))))这些数据逻辑上应该没有问题啊In Reply To:又加了一点注释 能不能帮忙看看 或是给个出错的数据 谢了 Posted by:nestle at 2005-06-15 22:14:20 > #include <iostream> > #include <cstdio> > #include <cstring> > #include <map> > using namespace std; > int operater[200]; //1 > int operand[200]; //2 > map <char,int> isp; //栈内优先数 > map <char,int> icp; //栈外优先数 > char f[2]={'F','V'}; > int top1; //运算符栈的栈顶 > int top2; //运算数栈的栈顶 > > void init(){ > isp['(']=1; isp['&']=5; isp['|']=3; isp['!']=6; isp[')']=8; > icp['(']=8; icp['&']=4; icp['|']=2; icp['!']=7; icp[')']=1; > return ; > } //初始化优先数 > > void cal(char c){ > int num,left,right; > if(c=='!'){ > num=operand[--top2]=='V'?1:0; > operand[top2++]=f[!num]; > } //单目运算 只要取一个数 > > else { > right=operand[--top2]=='V'?1:0; > left=operand[--top2]=='V'?1:0; > if(c=='|'){ > operand[top2++]=f[left||right]; > } > else if(c=='&'){ > operand[top2++]=f[left&&right]; > } > } //双目运算,取两个数 > > return ; > } //运算 > > int main(){ > init(); > int kase=0; > char ch; > while((ch=getchar())!=EOF){ > top1=top2=0; > while(1){ > if(ch=='\n'||ch==EOF) break; > > if(ch==' '){ > ch=getchar(); > continue; > } //如果读入空格就继续读 > > else if(ch=='V'||ch=='F'){ > operand[top2++]=ch; > ch=getchar(); > }//V或F就进操作数的栈 > > else { > if(top1&&icp[ch]<isp[operater[top1-1]]){ > cal(operater[--top1]); > } //操作符优先级小于 栈顶 对栈顶进行运算 > else if(top1&&icp[ch]==isp[operater[top1-1]]){ > --top1; > ch=getchar(); > } //相等 抵消 主要是左右括号 > else{ > operater[top1++]=ch; > ch=getchar(); > } //操作符入栈 > //运算符进行处理 > } > } > if(ch=='\n'){ > while(top1){ > cal(operater[--top1]); > } //将操作符里的操作符全部运算掉 > printf("Expression %d: %c\n",++kase,operand[top2-1]); > } > else if(ch==EOF) break; > } > return 0; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator