离散数学中求合取范式&析取范式

这篇博客介绍了作者在大二时使用C++编程实现离散数学中的核心概念——合取范式(Conjunctive Normal Form, CNF)和析取范式(Disjunctive Normal Form, DNF)。通过阅读,读者可以了解到如何用代码形式表达这两个重要的逻辑表达式,并理解其在离散数学中的应用。" 129141405,10622267,寻找二元组:计算相同元素对的数量,"['算法', '数组操作', '哈希表']
摘要由CSDN通过智能技术生成

大二上学期时写的代码,用C++实现的。

#include <iostream>
#include <cstdlib>
#include <string> 
#include <stack>
#include <vector>
#include<math.h>
using namespace std;

//&表示合取,|表示析取,!表示非,>表示条件,=表示双条件

class ForBase
{
private:
	static const int MAXN = 92;
	int numVar;	//记录范式中的变量  
	bool variables[MAXN];	//存储变量真假值,初始为F  
	string sourceFormula;	//存储源串
	string normalCFormula;	//合取串
	string normalDFormula;	//析取串
	vector<char> vctofVar;	//向量容器--存储命题单元PQR
	vector<char> vctofPoland;	//向量容器--存储命题
	stack<char> stk;	//栈
	stack<bool> boolStk;		//存储命题中的命题单元
	bool isVar(char ch)const;	//规定命题是大写英文字母
	void addMin(int  minterm);	//计算布尔小项
	void addMax(int maxterm);	//计算布尔大项
	bool  compute(int minterm);		//逻辑运算
	void getInPod();	//栈操作--将命题公式转换成后序表达式
	int countTerms(int n);	//求小项/大项个数
	void assign(int minterm);	//逻辑判断
public:
	ForBase();	//构造
	~ForBase();	//析构
	void getSource();	//输入源式
	string getNC();	//获取主析取范式
	string getND();	//获取主合取范式
	void printSource()
	{
		cout << sourceFormula << endl;
	}
	void printDNormal()	//打印合取串
	{
		cout << normalDFormula << endl;
	}
	void printCNormal()	//打印析取串
	{
		cout << normalCFormula << endl;
	}
	void printTruthTable();		//打印真值表
};

//构造函数
ForBase::ForBase()
{
	for (int i = 0; i<MAXN; i++)
		variables[i] = false;	//初始化为F
	numVar = 0;	//个数为0
}

//析构函数
ForBase::~ForBase()
{
	while (!stk.empty())	//清除栈
		stk.pop();
	vctofVar.clear();	//清除向量
	vctofPoland.clear();	//清除向量
}

//求小项/大项个数
int ForBase::countTerms(int n)		//n为P、Q、R、Y...的个数
{
	if (n == 0)	//合法性判断
	{
		cout << "invalid input!" << endl;
		exit(0);
	}
	return pow(2, 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值