| ||||||||||
| 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 | |||||||||
多重没用的括号没有问题吧?In Reply To:Re:这个谁都知道,但谁都不知道你的代码哪一块干哪些事的 Posted by:nestle at 2005-06-15 22:06:54 > #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