离散数学实验二:命题逻辑(2)_第1页
离散数学实验二:命题逻辑(2)_第2页
离散数学实验二:命题逻辑(2)_第3页
离散数学实验二:命题逻辑(2)_第4页
离散数学实验二:命题逻辑(2)_第5页
已阅读5页,还剩16页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、离散数学实验报告1【实验序号】实验二 命题逻辑(2)2【实验目的】熟悉掌握命题逻辑中真值表,进一步能用它们来解决实际问题。3【实验内容】求任意一个命题公式的真值表4. 【实验要求】对于给定的命题公式,生成相应真值表,然后用函数运算,输出结果:如生成逻辑非、合取、析取、条件、双条件表达式的真值表,例如:输入 !a输出真值表如下:a !a0 11 0输入a&&b输出真值表如下:a b ab0 0 00 1 01 0 01 1 1输入a|b输出真值表如下:a b ab0 0 00 1 11 0 11 1 1输入a->b输出真值表如下:a b ab0 0 10 1 11 0 01

2、 1 1输入a<>b (其中<>表示双条件)输出真值表如下:a b ab0 0 10 1 01 0 01 1 1通过以下界面提示实现相应逻辑运算,列出其真值表*请选择(16)要进行的真值表运算:1. 逻辑非( P)2. 合取(PQ)3. 析取(PQ)4. 条件(PQ)5. 双条件(PQ)6. 继续/退出(y/n)*5. 【算法描述】真值表:列出命题公式真假值的表,通常以1表示真,0 表示假。命题公式的取值由组成命题公式的命题变元的取值和命题联结词决定,命题联结词的真值表给出了真假值的算法。 6. 【源程序(带注释)】#include<iostream>#in

3、clude<stdlib.h>using namespace std;class mathchar p, q;int result;public:math(char x, char y);int pdp(char x);int pdq(char y);void luojifei(char x, int t);void hequ(char x, char y, int t);void xiqu(char x, char y, int t);void tiaojian(char x, char y, int t);void shuangtiaojian(char x, char y,

4、int t);void caidan();void yunsuancaidan();void zzbcaidan();void zzbluojifei();void zzbhequ();void zzbxiqu();void zzbtiaojian();void zzbshuangtiaojian();void fengefu();math:math(char x, char y)p = x; q = y;/判断p是否为1或0int math:pdp(char x)int a = 1;p = x;if (x != '0'&&x != '1')a

5、= 0;cin.clear();cin.ignore(100000, 'n');return a;/判断q是否为1或0int math:pdq(char y)q = y;int a = 1;if (y != '0'&&y != '1')a = 0;cin.clear();cin.ignore(100000, 'n');return a;void math:luojifei(char x, int t)p = x; result = t;if (x = '1') t = 0;else if (x =

6、'0') t = 1;cout << "真值为:" << t <<",请继续选择选择(1-6)要进行的逻辑运算:"/合取运算void math:hequ(char x, char y, int t)p = x; q = y; result = t;if (x = '1'&&y = '1') t = 1;else t = 0;cout << "真值为:" << t <<",请继续选择选择(1-

7、6)要进行的逻辑运算:"/析取运算void math:xiqu(char x, char y, int t)p = x; q = y; result = t;if (x = '0'&&y = '0') t = 0;else t = 1;cout << "真值为:" << t <<",请继续选择选择(1-6)要进行的逻辑运算:"/条件运算void math:tiaojian(char x, char y, int t)p = x; q = y; result =

8、t;if (x = '1'&&y = '0') t = 0;else t = 1;cout << "真值为:" << t <<",请继续选择选择(1-6)要进行的逻辑运算:"/双条件运算void math:shuangtiaojian(char x, char y, int t)p = x; q = y; result = t;if (x = y) t = 1;else t = 0;cout << "真值为:" << t <

9、;<",请继续选择选择(1-6)要进行的逻辑运算:"/逻辑运算菜单void math:caidan()cout << "1.逻辑运算真值表" << endl;cout << "2.逻辑运算" << endl;cout << "3.退出/继续" << endl;cout << "请选择(1-3)要进行操作:"void math:yunsuancaidan()/*/cout << "1.逻

10、辑非( P)" << endl;cout << "2.合取运算(PQ)" << endl;cout << "3.析取运算(PQ)" << endl;cout << "4.条件运算(PQ)" << endl;cout << "5.双条件运算(PQ)" << endl;cout << "6.返回上级菜单" << endl;cout << "

11、;请选择(1-6)要进行的逻辑运算:"void math:zzbcaidan()cout << "1.逻辑非( P)真值表" << endl;cout << "2.合取运算(PQ)真值表" << endl;cout << "3.析取运算(PQ)真值表" << endl;cout << "4.条件运算(PQ)真值表" << endl;cout << "5.双条件运算(PQ)真值表"

12、 << endl;cout << "6.返回上级菜单" << endl;cout << "请选择(1-6)查看真值表:"void math:zzbluojifei()cout << "a !a" << endl;cout << "0 1" << endl;cout << "1 0" << endl;cout << "请选择(1-6)查看真值表:"

13、void math:zzbhequ()cout << "a b ab" << endl;cout << "0 0 0" << endl;cout << "0 1 0" << endl;cout << "1 0 0" << endl;cout << "1 1 1" << endl;cout << "请选择(1-6)查看真值表:"void math:

14、zzbxiqu()cout << "a b ab" << endl;cout << "0 0 0" << endl;cout << "0 1 1" << endl;cout << "1 0 1" << endl;cout << "1 1 1" << endl;cout << "请选择(1-6)查看真值表:"void math:zzbtiaojia

15、n()cout << "a b ab" << endl;cout << "0 0 1" << endl;cout << "0 1 1" << endl;cout << "1 0 0" << endl;cout << "1 1 1" << endl;cout << "请选择(1-6)查看真值表:"void math:zzbshuangtiaojia

16、n()cout << "a b ab" << endl;cout << "0 0 1" << endl;cout << "0 1 0" << endl;cout << "1 0 0" << endl;cout << "1 1 1" << endl;cout << "请选择(1-6)查看真值表:" ;void math:fengefu()cout

17、<< "#" << endl;void main()char x = '0', y = '0', n = '0', n1, n2, n3;int t = 0;math math1(x, y);loop:cin.clear();cin.sync();math1.fengefu();math1.caidan();while (n)switch (n = cin.get()case '1':cin.clear();/cin.ignore(100000, 'n');cin.syn

18、c();math1.fengefu();math1.zzbcaidan();loop2:switch (n1 = cin.get()case '1':math1.zzbluojifei();break;case '2':math1.zzbhequ();break;case '3':math1.zzbxiqu();break;case '4':math1.zzbtiaojian();break;case '5':math1.zzbshuangtiaojian();break;case '6':goto

19、 loop;break;default:cin.clear();/cin.ignore(100000, 'n');cin.sync();cout << "输入错误,请选择(1-6)查看真值表:"goto loop2;break;cin.clear();cin.sync();goto loop2;break;case '2':cin.clear();/cin.ignore(100000, 'n');cin.sync();math1.fengefu();cout << "输入P:"x =

20、 cin.get();while (math1.pdp(x) = 0)cout << "输入错误,重新输入P:"x = cin.get();cout << "输入Q:"y = cin.get();while (math1.pdq(y) = 0)cout << "输入错误,重新输入Q:"y = cin.get();math1.yunsuancaidan();loop3:switch (n2 = cin.get()/*case '1':math1.luojifei(x, t);break

21、;case '2':math1.hequ(x, y, t);break;case '3':math1.xiqu(x, y, t);break;case '4':math1.tiaojian(x, y, t);break;case '5':math1.shuangtiaojian(x, y, t);break;case '6':goto loop;break;*/case '1':math1.luojifei(x, t);break;case '2':math1.hequ(x, y, t);break;case '3':math1.xiqu(x, y, t);break;case '4':math1.tiaojian(x, y, t);break;case '5':math1.shuangtiaoj

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论