构造命题公式的真值表--biaobiao88
2019-11-01 09:56:30来源:博客园 阅读 ()
构造命题公式的真值表--biaobiao88
对给出的任意一个命题公式(不超过四个命题变元),使学生会用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; }
运行结果:
以下代码为可输入的代码,用布尔值实现
#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 == 1 && y == 0) a = 0; else a = 1; if(y == 1 && x == 0) b = 0; else b = 1; if(x == y) c = 1; else c = 0; 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 0; }
运行结果:
原文链接:https://www.cnblogs.com/biaobiao88/p/11777719.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ 析构函数 2020-06-03
- C++ 构造函数 2020-06-03
- 类的使用(类和对象,构造函数和析构函数) 2020-04-05
- c++-构造函数练习和delete,new 2019-12-21
- c++-构造函数 2019-12-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash