大二上学期时写的代码,用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,