system("pause");return0;
}
#include"thesis.h"
#ifndefTHESIS_H#defineTHESIS_H#include
#include#includeusingnamespacestd;
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;j<2;j++)
{
a[N-1].invalue(j);if(N>1)
{
assignment(A,N-1,M,&a[0]);
}
else
{
for(inti=0;i{
cout<}
cout<}
}
}
intbds(stringA,intN,Thesisa[])//识别输入的表达式并计算真值
{
Thesisanswer,Temp;
chard[5]={'!
','|','&','>','<'};//定义运算符号集合intL;
inti=0;
L=strlen(&A[0]);//表达式长度while(i{
if(A[i]=='(')
{
intk=1;
for(intj=i+2;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;mp[l]='\0';
B=p;
}
}
else
{
deletep;Temp.invalue(bds(B,N,&a[0]));if(i==0)
answer.invalue(Temp.getvalue());i=j+1;
break;
}
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=answeri++;
continue;
}
elseif(A[i]=='>')
{
Temp.invalue(run(A,i,L,N,&a[0]));answer=answer>Temp;
i++;
continue;
}
else
{
for(intj=0;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;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;mp[l-1]='\0';
B=p;
deletep;
Temp.invalue(bds(B,N,&a[0]));i=i+j;
break;
}
}
}
else
{
intj=0;while(j{
if(A[i+1]==char(a[j].getname()))
{
i++;
Temp=a[j];break;
}
j++;
}
}
returnTemp.getvalue();
}
#endif