《利用真值表法求主析取范式及主合取范式的实现》由会员分享,可在线阅读,更多相关《利用真值表法求主析取范式及主合取范式的实现(22页珍藏版)》请在金锄头文库上搜索。
1、实验报告( / 学年第一学期)课程名称离散数学实验名称利用真值表法求主析取范式及主合取范式的实现实验时间年月日指导单位指导教师学生姓名学院 (系)班级学号专 业实验报告实验名称实验类型利用真值表法求主析取范式及主合取范式的实现上机实验学时4指导教师实验时间一、实验目的和要求能够列出合式公式的真值表并给出相应主析取范式和主合取范式。二、 实验环境 ( 实验设备 )硬件 :PC 机。软件 :Code:Blocks(C+ )三、实验原理及内容内容:编程实现用真值表法求任意含三个以内变量的合式公式的主析取范式和主合取范式。原理:首先读入变元个数,然后读入合式公式,用堆栈的知识将中缀表达式转化为后缀表达
2、式,调用否定、析取、合取、条件、双条件的函数计算P、Q、R取不同真值时合式公式的真值,然后输出真值表,调用计算主析取范式和主合取范式的函数并输出。程序:#include#include#include#includeusing namespace std;string OriginalForm;/原式string Hequ;/主合取范式string Xiqu;/主析取范式class SeqStack/建立一个堆栈,利用将中缀表达式转为后缀表达式public:SeqStack(int mSize);SeqStack();char Top();bool Push(char x);bool Pop(
3、);private:char *st;int top;int maxtop;SeqStack:SeqStack(int mSize)maxtop = mSize - 1;top = -1;st = new charmSize;SeqStack:SeqStack()deletest;char SeqStack:Top()return sttop;bool SeqStack:Push(char x)if(top = maxtop)return false;st+top = x;return true;bool SeqStack:Pop()if(top = -1)return false;top-;
4、return true;int p, q, r, s, t, u;int a, b, result;int v =0;int number;/用 number表示变元的个数SeqStack stack(200);void Not()/否定a = ();();result = a = 1(result);0:1;void Or()/析取result = a + b;result = result 1 1 : result;(result);void And()/合取result = a * b;(result);void If()/条件 ,b-aresult = (b = 1 & a = 0)
5、0 : 1;(result);void Doubleif() /双条件result = (b = a) 1 : 0;(result);bool CanIn(char out)/优先级的判断char in = ();int i, o;switch(in)case #:i = 0; break;case (:i = 1; break;case -:i = 3; break;case :i = 5; break;case |:i = 7; break;case &:i = 9; break;case !:i = 11; break;case ):i = 12; break;switch(out)ca
6、se #:o = 0; break;case (:o = 12; break;case -:o = 2; break;case :o = 4; break;case |:o = 6; break;case &:o = 8; break;case !:o = 10; break;case ):o = 1; break;if(i o)return true;elsereturn false;void InfixToPostfix()/中缀表达式转后缀表达式string tmp = ;(#);for(int i = 0; (unsigned)i (); i+)if(OriginalFormi |Or
7、iginalFormi = R |= POriginalFormi|OriginalFormi= Q= S|OriginalFormi= T | OriginalFormi = U)tmp=tmp+OriginalFormi;continue;if(CanIn(OriginalFormi)(OriginalFormi);else if(OriginalFormi = )while() != ()tmp = tmp + ();();();elsedotmp = tmp + ();(); while(!CanIn(OriginalFormi); (OriginalFormi);while() !=
8、 #)tmp = tmp + ();();();OriginalForm = tmp;void Calculate()/计算主析取范式和主合取范式的函数if(number = 3)for(int i = 0; (unsigned)i :If(); break;case |:Or(); break;case &:And(); break;case !:Not(); break;if(number = 2)for(int i = 0; (unsigned)i (); i+)if(OriginalFormi= P|OriginalFormi= Q)v = OriginalFormi = P p : q;(v);continue;if(OriginalFormi != !)a = ();();b = ();();switch(OriginalFormi)