离散数学合取式,合取范式与析取范式

大二前期写的代码,用c实现了。

# include iostream # includecstdlib # include string # include stack # include vector # include math.husingnamespacestd; //表示合并取,|表示分析, 表示非,表示条件,=表示双条件classforbase { private : staticconstintmaxn=92; int numVar; //范式的变量bool variables[MAXN]; //存储变量的真伪值,初始为F string sourceFormula; //存储源字符串string normalCFormula; //字符串字符串正规dformula; //提取字符串vectorchar vctofVar; //向量容器—保存命题的单元PQRvectorchar vctofPoland; //向量容器—保存命题堆栈char STK; //堆栈bool boolstk; //记住命题中的命题单元boolisvar(charch ) const; //规定命题为大写英文字母voidaddmin(intminterm ); //计算布尔小项voidaddmax(intmaxterm ); //逻辑运算void getInPod (; //堆栈操作–将命题表达式转换为后一个表达式intcountterms(intn ); //求小项/大项个数voidassign(intminterm )//逻辑判断public:ForBase (; //结构~ForBase (; 语法分析语音获取源; //输入源的字符串getnc (; //获取主分析范式string getND (; //主联接范式void printSource () {cout sourceFormula endl; } void打印d normal ()//打印组合) {cout normalDFormula endl; } void打印协议()//打印提取字符串({cout normalCFormula endl; } Void打印表(); //打印真值表}; //构造函数ForBase:ForBase () for(intI=0; iMAXN; I ) variables[i]=false; 初始化为FnumVar=0个数0}//析构函数ForBase:~ForBase () {while (! stk.empty ()//堆栈stk.pop ); vctofVar.clear (; //向量vctofPoland.clear (要清除的//清除向量(//小项/大项的个数的intforbase :3360 count terms (intn )/n是p、q、r、) 退出(0; }返回电源(2,n ); //确保输入的命题为大写英文boolforbase :3360 isvar (charch ) const (if ) ch=’a’ch=’z ‘ )返回真; 返回假; //命题公式void ForBase:getSource () cout ‘ inputthesourceformula : ‘ endl; CIN source公式; //堆栈操作–将命题表达式转换为后面的表达式voidforbase :3360 get in pod ({ wchar _ ttemp,temp1; int len=sourceFormula.size (; //取得原命题公式的长度for (inti=0; i len; I ) {temp=sourceFormula[i]; //命题要素if(Isvar(TEMP ) )//命题单元(英文) if (! variables[temp]真伪表中为F{numVar; //变量个数1VcTofvar.push_back(temp ); //连接在命题单元容器末尾的variables[temp]=true; //t }连接在变化为vctofpoland.push _ back (temp )的//命题容器之后(}else //是操作员switch(temp ) case ) ) ://斯塔) )。//堆栈STK.push(temp ); 布雷克; case”://协调while (! stk.empty () )//堆栈不为空) if ) STK.top )!=’ ‘ () ) {temp1=stk.top; //堆栈顶部元素为temp1vctofpoland.push _ back (temp1); //串联

在命题向量后stk.pop();//出栈}else break;}stk.push(temp);//进栈break;case’|’:while (!stk.empty()){temp1 = stk.top();if (stk.top() == ‘&’ || stk.top() == ‘!’){vctofPoland.push_back(temp1);//串联在命题向量后面stk.pop();//操作符出栈}else break;}stk.push(temp);//操作符进栈break;case ‘>’:while (!stk.empty())//栈不为空{if (stk.top() == ‘&’ || stk.top() == ‘!’ || stk.top() == ‘|’){vctofPoland.push_back(temp1);//串联在命题向量后stk.pop();//出栈}else break;}stk.push(temp);//进栈break;case ‘=’:while (!stk.empty())//栈不为空{if (stk.top() == ‘&’ || stk.top() == ‘!’ || stk.top() == ‘|’ || stk.top() == ‘>’){vctofPoland.push_back(temp1);//串联在命题向量后stk.pop();//出栈}else break;}stk.push(temp);//进栈break;case’)’:while (!stk.empty()){if (stk.top() != ‘(‘){temp1 = stk.top();vctofPoland.push_back(temp1);stk.pop();}else break;}if (stk.empty())exit(0);//栈为空,结束stk.pop();//把左括号pop出去 break;}}while (!stk.empty())//把剩余全部top出来{temp1 = stk.top();vctofPoland.push_back(temp1);stk.pop();}}//逻辑赋值void ForBase::assign(int minterm){int temp = minterm;for (vector<char>::const_iterator itr = vctofVar.begin(); itr != vctofVar.end(); itr++){variables[(int)*itr] = bool(temp & 1);temp >> 1;}}//逻辑运算bool ForBase::compute(int minterm){assign(minterm);wchar_t temp;bool valueA, valueB;vector<char>::const_iterator itr = vctofPoland.begin();while (itr != vctofPoland.end()){temp = *itr;if (isVar(temp))//是命题单元boolStk.push(variables[temp]);//存储在boolstk栈中elseswitch (temp)//是操作符{case’&’://合取{if (boolStk.size()<2)exit(0);//错误处理valueA = boolStk.top();boolStk.pop();valueB = boolStk.top();boolStk.pop();valueA = valueA &&valueB;boolStk.push(valueA);break;}case’|’://析取{if (boolStk.size()<2)exit(0);valueA = boolStk.top();boolStk.pop();valueB = boolStk.top();boolStk.pop();valueA = valueA || valueB;boolStk.push(valueA);break;}case’>’://条件{if (boolStk.size()<2)exit(0);valueA = boolStk.top();boolStk.pop();valueB = boolStk.top();boolStk.pop();if (valueA == false && valueB == true)valueA = false;elsevalueA = true;boolStk.push(valueA);break;}case’=’://双条件{if (boolStk.size()<2)exit(0);valueA = boolStk.top();boolStk.pop();valueB = boolStk.top();boolStk.pop();if (valueA == valueB)valueA = true;elsevalueA = false;boolStk.push(valueA);break;}case’!’://非{if (boolStk.empty())exit(0);valueA = !(boolStk.top());boolStk.pop();boolStk.push(valueA);break;}}itr++;}if (boolStk.size() != 1)//保证最后只剩一个元素(结果){cout << “Error in computing the value of minterm” << endl;exit(0);}valueA = boolStk.top();boolStk.pop();return valueA;}//合成布尔小项void ForBase::addMin(int minterm){vector<char>::const_iterator itr = vctofVar.begin();//迭代器指向合取串向量normalCFormula += ‘(‘;while (itr != vctofVar.end())//保证每个布尔小项包含每一个命题单元{if (!variables[(int)*itr])normalCFormula += ‘!’;normalCFormula += *itr;//命题单元串联在后面normalCFormula += ‘&’;//括号内命题单元是合取符号itr++;}normalCFormula += “\b)|”;//括号间是析取符号}//合成布尔大项void ForBase::addMax(int maxterm){vector<char>::const_iterator itr = vctofVar.begin();normalDFormula += ‘(‘;while (itr != vctofVar.end()){if (variables[(int)*itr])normalDFormula += ‘!’;normalDFormula += *itr;normalDFormula += ‘|’;itr++;}normalDFormula += “\b)&”;}//获取主析取范式string ForBase::getNC(){if (vctofPoland.size() == 0)//合法判断getInPod();int n = countTerms(numVar);//获取布尔小(大)项个数normalCFormula = ‘ ‘;for (int i = 0; i<n; i++){if (compute(i)) addMin(i);//调用逻辑运算,为真加入到最后结果(剔除永假项)}normalCFormula += “\b “;return normalCFormula;}//获取主合取范式string ForBase::getND(){if (vctofPoland.size() == 0)//合法判断getInPod();int n = countTerms(numVar);normalCFormula = ‘ ‘;for (int i = 0; i<n; i++){if (!compute(i)) addMax(i);//调用逻辑运算,为假加入到最后结果(剔除永真项)}normalDFormula += “\b “;return normalDFormula;}//输出真值表void ForBase::printTruthTable(){int i = 0;int count = countTerms(numVar);cout << ” 真值表 \n”;cout << endl;for (i = 0; i<numVar; i++)cout << vctofVar[i] << ‘\t’;cout << sourceFormula << endl;cout << endl;for (i = 0; i<count; i++){int temp = i;for (int j = 0; j<numVar; j++){if (bool(temp & 1))cout << “F\t”;else cout << “T\t”;temp = temp >> 1;}if (this->compute(i))cout << “F\t”;else cout << “T\t”;cout << ‘\t’ << endl;cout << endl;}}//主函数int main(){ForBase fb;fb.getSource();cout << “主析取范式是:” << fb.getNC() << endl;cout << “主合取范式是:” << fb.getND() << endl;fb.printTruthTable();return 0;}

 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注