构造任意合式公式的真值表

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

构造任意合式公式的真值表#includestdio.h#includethesis.hintmain(){Thesisa[30];charx='1';inti=0,N;cout请输入命题变元(不超过30个)(输入'0'结束输入):endl;while(int(x)!=48){cinx;if(i19){coutError:变元个数太多!endl;break;}if(x!='0'){a[i].inname(x);i++;}}N=i;intM;M=N;stringA;cout请输入命题公式(否定:!,合取:&,析取:|)endl;cinA;coutA的真值表为:endl;for(intj=0;jM;j++)coutchar(a[j].getvalue());cout真值endl;assignment(A,N,M,&a[0]);system(pause);return0;}#includethesis.h#ifndefTHESIS_H#defineTHESIS_H#includestring#includestdlib.h#includeiostreamusingnamespacestd;classThesis//命题类{intvalue;charname;//value:命题的真值(0/1)name:命题名public:Thesis(){value=2;name='A';};friendThesisoperator!(Thesis&q){q.invalue(1-q.getvalue());returnq;}//重载逻辑运算符friendThesisoperator&(Thesis&p,Thesis&q){p.invalue((p.getvalue()+q.getvalue())/2);returnp;}friendThesisoperator|(Thesis&p,Thesis&q){if(p.getvalue()+q.getvalue()0)p.invalue(1);elsep.invalue(0);returnp;}friendThesisoperator(Thesis&p,Thesis&q){if(p.getvalue()==1&&q.getvalue()==0)p.invalue(0);elsep.invalue(1);returnp;}friendThesisoperator(Thesis&p,Thesis&q){if(p.getvalue()==q.getvalue())p.invalue(1);elsep.invalue(0);returnp;}voidinvalue(intx){value=x;}//输入valuevoidinname(charx){name=x;}//输入nameintgetvalue(){returnvalue;}//获取真值intgetname(){returnname;}//获取命题名};voidassignment(stringA,intN,intM,Thesisa[]);//声明函数intbds(stringA,intN,Thesisa[]);intrun(stringA,int&i,intL,intN,Thesisa[]);voidassignment(stringA,intN,intM,Thesisa[])//命题赋值并计算真值{for(intj=0;j2;j++){a[N-1].invalue(j);if(N1){assignment(A,N-1,M,&a[0]);}else{for(inti=0;iM;i++){couta[i].getvalue();}coutbds(A,M,&a[0])endl;}}}intbds(stringA,intN,Thesisa[])//识别输入的表达式并计算真值{Thesisanswer,Temp;chard[5]={'!','|','&','',''};//定义运算符号集合intL;inti=0;L=strlen(&A[0]);//表达式长度while(iL){if(A[i]=='('){intk=1;for(intj=i+2;jL;j++){if(k!=0){if(A[j]=='(')k++;if(A[j]==')')k--;}if(k==0){intl=j-i-1;char*p=newchar[l+1];stringB;for(intm=0;ml;m++)p[m]=A[i+1+m];p[l]='\0';B=p;deletep;Temp.invalue(bds(B,N,&a[0]));if(i==0)answer.invalue(Temp.getvalue());i=j+1;break;}}}else{if(A[i]=='!')//否定的计算{Temp.invalue(run(A,i,L,N,&a[0]));answer=!Temp;i++;continue;}elseif(A[i]=='|'){Temp.invalue(run(A,i,L,N,&a[0]));answer=answer|Temp;i++;continue;}elseif(A[i]=='&'){Temp.invalue(run(A,i,L,N,&a[0]));answer=answer&Temp;i++;continue;}elseif(A[i]==''){Temp.invalue(run(A,i,L,N,&a[0]));answer=answerTemp;i++;continue;}elseif(A[i]==''){Temp.invalue(run(A,i,L,N,&a[0]));answer=answerTemp;i++;continue;}else{for(intj=0;jN;j++){if(A[i]==char(a[j].getname())){Temp.invalue(a[j].getvalue());if(i==0)answer.invalue(Temp.getvalue());i++;break;}}}}}returnanswer.getvalue();}intrun(stringA,int&i,intL,intN,Thesisa[]){ThesisTemp;if(A[i+1]=='('){intk=1;for(intj=i+2;jL;j++){if(k!=0){if(A[j]=='(')k++;if(A[j]==')')k--;}if(k==0){intl=j-i-1;char*p=newchar[l+1];stringB;for(intm=0;ml-1;m++)p[m]=A[i+2+m];p[l-1]='\0';B=p;deletep;Temp.invalue(bds(B,N,&a[0]));i=i+j;break;}}}else{intj=0;while(jL){if(A[i+1]==char(a[j].getname())){i++;Temp=a[j];break;}j++;}}returnTemp.getvalue();}#endif

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功