南邮离散数学实验1 (简单版) 根据真值求真值表和主范式
来源:互联网 发布:苹果美版网络锁查询 编辑:程序博客网 时间:2024/06/06 02:19
#include <iostream>#include <cmath>using namespace std;int const MAX = 1e6;short true_value[MAX]; //真值short true_table[MAX][10]; //真值表short pdnf[MAX]; //主析取范式short pcnf[MAX]; //主合取范式char varible[10] = {'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'}; //定义变元int cnt1 = 0, cnt2 = 0; //cnt1记录真值为1的数目,cnt2记录真值为0的数目void Output_pdnf() //输出主析取范式{ cout << "主析取范式为 : " << endl; if(cnt2 == 1) cout << 'm' << pdnf[0] << endl; else { for(int i = 0; i < cnt2 - 1; i++) cout << 'm' << pdnf[i] << " V "; cout << 'm' << pdnf[cnt2 - 1] << endl; }}void Output_pcnf() //输出主合取范式{ cout << "主合取范式为 : " << endl; if(cnt1 == 1) cout << 'M' << pcnf[0] << endl; else { for(int i = 0; i < cnt1 - 1; i++) cout << 'M' << pcnf[i] << " ∧ "; cout << 'M' << pcnf[cnt1 - 1] << endl; }}int main(){ int n, i; cout << "请输入命题变元的个数 :" << endl; cin >> n; cout << "请输入表达式的真值 :" << endl; for(i = 0; i < (int) pow(2.0, n * 1.0); i++) { cin >> true_value[i]; //输入真值 if(true_value[i]) pcnf[cnt1++] = i; else pdnf[cnt2++] = i; } cout << "真值表为 :" << endl; for(i = 0; i < n; i++) cout << "\t" << varible[i]; cout << "\t" << 'A' << endl; for(i = 0; i < (int) pow(2.0, n * 1.0); i++) { int tmp = (int) pow(2.0, n * 1.0) - 1 - i; for(int j = n - 1; j >= 0; j--) { true_table[i][j] = tmp % 2; tmp /= 2; } } for(i = 0; i < (int) pow(2.0, n * 1.0); i++) true_table[i][n] = true_value[i]; for(i = 0; i < (int) pow(2.0, n * 1.0); i++) { for(int j = 0; j < n + 1; j++) cout << "\t" << true_table[i][j]; cout << endl; } Output_pdnf(); Output_pcnf(); return 0;}
修改版:
#include <iostream>#define MAX 100000using namespace std;short trueValue[MAX]; //真值short trueTable[MAX][10]; //真值表short pdnf[MAX]; //主析取范式short pcnf[MAX]; //主合取范式int cnt1 = 0, cnt2 = 0; //cnt1记录真值为1的数目,cnt2记录真值为0的数目int n, i;void Output_pdnf() {//输出主析取范式 cout << "主析取范式为 : " << endl; string ans = ""; int tmpcnt = 0; for (int i = 0; i < (1 << n); i ++) { if (trueTable[i][n] == 1) { tmpcnt ++; ans += '('; for (int j = 0; j < n; j ++) { if (trueTable[i][j] == 0) { ans += '!'; } ans += (j + 'P'); if (j != n - 1) { ans += " ∧ "; } } ans += ')'; if (tmpcnt < cnt1) { ans += " V "; } } } cout << ans << endl;}void Output_pcnf() {//输出主合取范式 cout << "主合取范式为 : " << endl; string ans = ""; int tmpcnt = 0; for (int i = 0; i < (1 << n); i ++) { if (trueTable[i][n] == 0) { tmpcnt ++; ans += '('; for (int j = 0; j < n; j ++) { if (trueTable[i][j] == 1) { ans += '!'; } ans += (j + 'P'); if (j != n - 1) { ans += " V "; } } ans += ')'; if (tmpcnt < cnt2) { ans += " ∧ "; } } } cout << ans << endl;}int main() { cout << "请输入命题变元的个数 :" << endl; cin >> n; cout << "请输入表达式的真值 :" << endl; for (i = 0; i < (1 << n); i ++) { cin >> trueValue[i]; //输入真值 if (trueValue[i]) { pcnf[cnt1 ++] = i; } else { pdnf[cnt2 ++] = i; } } for (i = 0; i < (1 << n); i ++) { int tmp = i; for (int j = n - 1; j >= 0; j--) { trueTable[i][j] = tmp % 2; tmp /= 2; } } for (i = 0; i < (1 << n); i++) { trueTable[i][n] = trueValue[i]; } Output_pdnf(); Output_pcnf(); return 0;}
0 0
- 南邮离散数学实验1 (简单版) 根据真值求真值表和主范式
- 南邮离散数学实验1 (栈版) 根据表达式求真值表和主范式
- 南邮离散数学实验-利用真值表输出主析取范式主合取范式
- 南邮离散数学实验 利用真值表法求取主析取范式以及主合取范式的实现
- 离散数学实践:真值表与范式
- 「离散数学」 打印任意命题公示的真值表和主范式
- 离散数学输入表达式打印真值表和主析/合取范式
- 【离散数学】实验一 利用真值表法求取主析取范式以及主合取范式的实现
- 离散数学实验 命题变元的运算,真值表....
- 关于离散数学的真值表的求解
- [栈的应用] 打印任意命题公示的真值表和主范式(这是转载同学的)
- 真值表(C++)
- C语言 实现离散数学合式公式真值表
- 求两个变量的真值表C++
- [离散] 编程求命题公式真值表
- 基本rs触发器真值表和状态图
- 真值和机器数
- 真值表生成器
- TCP/IP三次握手与四次挥手
- JavaScript提高:006:ASP.NET使用easyUI TABS标签updatepanel
- Spring核心包
- JAVA反射机制及应用例子
- MySQL索引概念以及创建方法
- 南邮离散数学实验1 (简单版) 根据真值求真值表和主范式
- Python的html和xml解析库Beautiful Soup
- 年轻的开发者,请善待你自己
- shell中for循环总结
- [HDU 5042 GCD pair] 离线+按GCD相同分段
- VC++ 6.0点打开按钮出现 “Microsoft (R) Developer Studio 已停止工作”的解决方法
- 云帆教育大数据分享-Flume-0.9.4源码编译及一些编译出错解决方法
- 云凡教育分享-Flume-0.9.4源码编译依赖的thrift插件安装
- Linux中智能小开关rfkill