对给出的任意一个命题公式(不超过四个命题变元),使学生会用C语言的程序编程表示出来,并且能够计算它在各组真值指派下所应有的真值,画出其真值表。

#include<iostream>
using namespace std;
struct
{
int p[] = {,,,};
int q[] = {,,,};
}m; int main()
{
cout << "   p:";
for(int i = ;i < ;i++)
cout << m.p[i] << " ";
cout << endl;
cout << "   q:";
for(int i = ;i < ;i++)
cout << m.q[i] << " ";
cout << endl;
//非p
cout << "  非p:";
int copyp[];
int k = ;
int feip[k];
for(int i = ;i < ;i++)
copyp[i] = m.p[i];
for(int i = ;i < ;i++)
{
if(copyp[i] == )
copyp[i] = ;
else
copyp[i] = ;
}
for(int i = ;i < ;i++)
{
feip[i] = copyp[i];
cout << copyp[i] << " ";
}
cout << endl;
//p交q
cout << "p合取q:";
for(int i = ;i < ;i++)
{
if(m.p[i] == || m.q[i] == )
cout << << " ";
else
cout << << " ";
}
cout << endl;
//p并q
cout << "p析取q:";
for(int i = ;i < ;i++)
{
if(m.p[i] == || m.q[i] == )
cout << << " ";
else
cout << << " ";
}
cout << endl;
//p蕴含q
cout << "p蕴含q:";
for(int i = ;i < ;i++)
{
if(feip[i] == || m.q[i] == )
cout << << " ";
else
cout << << " ";
}
cout << endl;
//p当且仅当q
cout << "p等价q:";
for(int i = ;i < ;i++)
{
if((m.p[i] == && m.q[i] == ) || (m.p[i] == && m.q[i] == ))
cout << << " ";
else
cout << << " ";
}
cout << endl;
return ;
}

运行结果:构造命题公式的真值表--biaobiao88-LMLPHP

以下代码为可输入的代码,用布尔值实现

#include<iostream>
using namespace std; void function(bool x,bool y)
{
bool m = x&y;
bool n = x|y;
bool a,b,c;
if(x == && y == )
a = ;
else
a = ;
if(y == && x == )
b = ;
else
b = ;
if(x == y)
c = ;
else
c = ;
cout << "p q ﹁p ﹁q p∩q p∪q p→q q→p p←→p p⊕q" << endl;
cout << x << " " << y << " " << !x << " " << !y << " ";
cout << m << " " << n << " ";
cout << a << " " << b << " " << c << " " << !c << endl;
} int main()
{
bool p,q;
int n;
cout << "请输入需要测试几组数据:";
cin >> n;
while(n--)
{
cout << "请输入命题p和命题q的真值:";
cin >> p >> q;
function(p,q);
}
return ;
}

运行结果:构造命题公式的真值表--biaobiao88-LMLPHP

05-12 00:32