利用真值表法求取主析取范式以及主合取范式的实现_第1页
利用真值表法求取主析取范式以及主合取范式的实现_第2页
利用真值表法求取主析取范式以及主合取范式的实现_第3页
利用真值表法求取主析取范式以及主合取范式的实现_第4页
利用真值表法求取主析取范式以及主合取范式的实现_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include #include #include #include using namespace std;char str100;/输入的命题公式int tv20 = 0;/真值指派的数组int length;/命题公式长度char expression100;/将命题公式中的命题变元变为真值后的数组int icp(const char c)/联结词的栈外优先级int result = -1;switch(c)case #: result = 0; break;case (: result = 10; break;case !: result = 9; break;case &: res

2、ult = 6; break;case |: result = 4; break;case : result = 2; break;case ): result = 1;return result;int isp(const char c)/联结词的栈内优先级int result = -1;switch(c)case #: result = 0; break;case (: result = 1; break;case !: result = 8; break;case &: result = 7; break;case |: result = 5; break;case : result =

3、 3; break;case ): result = 10;return result;void Plus(int a,int q)/二进制加法指派真值aq=aq+1;for (int i = q; ai = 2; i-)ai=0;ai-1=ai-1+1;templateclass Stackpublic:virtual bool IsEmpty() const = 0;virtual bool IsFull() const = 0;virtual bool Top(T& x) const = 0;virtual bool Push(T x) = 0;virtual bool Pop() =

4、0;virtual void Clear() = 0;templateclass SeqStack: public Stack/顺序栈类public:SeqStack(int mSize = 30);SeqStack()delete s;bool IsEmpty() constreturn top = -1;bool IsFull() constreturn top = maxTop;bool Top(T& x) const;bool Push(T x);bool Pop();void Clear()top = -1;private:int top;int maxTop;T* s;templa

5、teSeqStack:SeqStack(int mSize)maxTop = mSize - 1;s = new TmSize;top = -1;templatebool SeqStack:Top(T& x) constif (IsEmpty()cout Empty endl;return false;x = stop;return true;templatebool SeqStack:Push(T x)if (IsFull()cout Overflow endl;return false;s+top=x;return true;templatebool SeqStack:Pop()if(Is

6、Empty()cout Underflow endl; top-;return true;class Calculatorpublic:Calculator(int maxSize):s(maxSize)void Change();int Run();void Solve();void Clear()s.Clear();private:SeqStack s;/运算栈void PushOperand(bool);bool GetOperands(bool &, bool &);void DoOperator(char);void Calculator:PushOperand(bool op)s.

7、Push(op);bool Calculator:GetOperands(bool & op1, bool & op2)/获取栈顶两个元素if (!s.Top(op1)cerr Missing operand! endl;return false;s.Pop();if (!s.Top(op2)cerr Missing operand! : s.Push(!right | left); break;/条件运算elseClear();void Calculator:Change()/将输入的字符串转化为可计算的表达式 int k = 0, t = 0; int flag = 1;/标记,防止相同的

8、命题变元赋入不同的值 int count = 0; for (int i = 0; i pow(2,count); i+)k=1;for (int m = 0; m = 0; t-)if (strm+1 = strt) & isalpha(strm+1) & isalpha(strt)flag = 0;elseexpressionm = strm;/逻辑联结词不变for (t = m; t = 0; t-)if (strm+1 = strt) & isalpha(strm+1) & isalpha(strt)flag = 0;for(int t = 0; t length; t+)for (i

9、nt j = t; j length; j+)if (strt = strj)expressionj = expressiont;/相同的命题变元复制赋值int Calculator:Run()SeqStack s1;/联结词栈char ch, y;char p100;int i = 0;s1.Push(#);for (int temp=0; temp length ; temp+)ch = expressiontemp;if (isdigit(ch)pi+ = ch;else if(ch = )for(s1.Top(y), s1.Pop(); y != (; s1.Top(y), s1.Po

10、p()pi+ = y;else if(ch = !)pi+ = 1;/非运算,在!前加1,将!视作双目操作符for(s1.Top(y), s1.Pop(); icp(ch) : DoOperator(pi); break;default:cin.putback(pi);cin newop; PushOperand(newop); break;if (s.Top(newop)cout (int)newop endl;return (int)newop;/输出并返回最终结果void Calculator:Solve()cout * endl;/标语cout * 欢迎进入逻辑运算软件 * endl;

11、cout * (可运算真值表,主范式,支持括号) * endl;cout * * endl;cout * 用!表示not 用&表示and * endl;cout 表示蕴含 * endl;cout * * endl;cout * endl;cout str;/输入命题公式length = strlen(str) - 1; char index10;/命题变元数组for (int i = 0; i length; i+)/逐次添加命题变元 if (isalpha(stri) & (flag = 1) indexcount+ = stri;flag=1;for (int k = 0; k count

12、; k+) if (indexk = stri+1)flag=0; if (!count)cout 无命题变元,重新输入! endl;system(pause);system(cls);Solve();cout 真值表: endl;for (int w = 0; w count; w+)cout indexw ;for (w = 0; w length; w+)cout strw;cout endl;int *truth = new intpow(2,count);int xx = 0, dx = 0;/小项,大项for (int r = 0; r pow(2,count); r+)/输出真值表for (int j = 1; j = count; j+)cout tvj 1)/输出主析取范式 int flag_xx = 0;cout 主析取范式为 ;for(r = 0; r pow(2,count); r+)if (truthr)if (flag_xx)cout / ;cout m r;flag_xx = 1;cout endl;elsecout 没有主析取范式! 1)/输出主合取范式 int flag_dx = 0;cout 主合取范式为 ;for(r = 0; r pow(2,count); r+)if (!truthr)if (flag_dx

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论