对给出的任意一个命题公式(不超过四个命题变元),使学生会用C语言的程序编程表示出来,并且能够计算它在各组真值指派下所应有的真值,画出其真值表。
#include<iostream> using namespace std; struct { int p[4] = {1,1,0,0}; int q[4] = {1,0,1,0}; }m; int main() { cout << " p:"; for(int i = 0;i < 4;i++) cout << m.p[i] << " "; cout << endl; cout << " q:"; for(int i = 0;i < 4;i++) cout << m.q[i] << " "; cout << endl; //非p cout << " 非p:"; int copyp[4]; int k = 0; int feip[k]; for(int i = 0;i < 4;i++) copyp[i] = m.p[i]; for(int i = 0;i < 4;i++) { if(copyp[i] == 0) copyp[i] = 1; else copyp[i] = 0; } for(int i = 0;i < 4;i++) { feip[i] = copyp[i]; cout << copyp[i] << " "; } cout << endl; //p交q cout << "p合取q:"; for(int i = 0;i < 4;i++) { if(m.p[i] == 0 || m.q[i] == 0) cout << 0 << " "; else cout << 1 << " "; } cout << endl; //p并q cout << "p析取q:"; for(int i = 0;i < 4;i++) { if(m.p[i] == 1 || m.q[i] == 1) cout << 1 << " "; else cout << 0 << " "; } cout << endl; //p蕴含q cout << "p蕴含q:"; for(int i = 0;i < 4;i++) { if(feip[i] == 1 || m.q[i] == 1) cout << 1 << " "; else cout << 0 << " "; } cout << endl; //p当且仅当q cout << "p等价q:"; for(int i = 0;i < 4;i++) { if((m.p[i] == 1 && m.q[i] == 1) || (m.p[i] == 0 && m.q[i] == 0)) cout << 1 << " "; else cout << 0 << " "; } cout << endl; return 0; }
运行结果:
运行结果:
来源:博客园
作者:恶魔岛
链接:https://www.cnblogs.com/biaobiao88/p/11777719.html