离散数学实验报告(一).doc—、实验内容:构造任意合式公式的真值表
二、实验源码:
#include <>
#include""
int main()
{
Thesis a[30];
char x='1';
int i=O,N;
cout«"请输入命题变元(不超过30个)(输入O结束 输入):"«endl;
while(int(x)!=48)
{
cin> >x;
{cout«"Error:变元个数太多!"«endl;break;}
if(x!='O')
{
a[i].i nn ame(x);
i++;
}
}
N=i;
int M;
M=N;
string A;
cout«"请输入命题公式(否定:!,合取:& ,析取:
| )"«endl;
cin»A;
cout«A«"的真值表为:"«endl;
for(int j=O;j<M;j++)
cout«char(a[j].getvalue())«""
cout«"真值"«endl;
assig nm ent(A,N,M,&a[O]);
system("pause");
return 0;
}
#include""头文件
#ifndefTHESIS_H
#define THESIS_H
#in clude<string> #in clude<>
#in clude<iostream> using namespace std;
class Thesis 〃命题类
{
int value;char name; //value:命题的真值(0/1 )
name :命题名
public:
Thesis(){value=2;name-A';};
friend Thesis operator !(Thesis &q)
{(1-()); return q;} //
重载逻辑运算符
friend Thesis operator &(Thesis &p,Thesis &q)
{((()+())/2); return
P;}
friend Thesis operator |(Thesis &p,Thesis &q)
{if(()+()>O) (1);
else (O);
return p;}
friend Thesis operator >(Thesis &p,Thesis &q)
{if(()==1&&()==0)
value(O);
else (1);
return p;}
friend Thesis operator <(Thesis &p,Thesis &q)
{if(()==()) value(1);
else (O);
return p;}
void invalue(int x){value=x;} 〃输入 value
void inname(char x){name=x;} 〃输入
n ame
〃获取真值
int getvalue(){return value;}
int getname(){return name;} 〃获取命题名
};
void assignment(string A,int N,int M,Thesis a[]); // 声明函数
int bds(string A,int N,Thesis a[]);
int run(string A,int &i,int L,int N,Thesis a[]);
void assignment(string A,int N,int M,Thesis a[])〃命
题赋值并计算真值
{
for(intj=0;j<2;j++)
{
a[N-1].invalue(j);
assignment(A,N-1 ,M,&a[O]);
}
else
{
for(int i=O;i<M;i++)
{
cout«a[i].getvalue()«""
} cout«bds(A,M,&a[0])«endl;
}
}
〃识别输
}
int bds(string A,int N,Thesis a[])
入的表达式并计算真值
Thesis answer,Temp;
char d[5]〃定义运算符号集合
离散数学实验报告(一) 来自淘豆网m.daumloan.com转载请标明出处.