利用真值表法求取主析取范式以及主合取范式的实现

上传人:人*** 文档编号:487594079 上传时间:2023-08-07 格式:DOCX 页数:9 大小:14.17KB
返回 下载 相关 举报
利用真值表法求取主析取范式以及主合取范式的实现_第1页
第1页 / 共9页
利用真值表法求取主析取范式以及主合取范式的实现_第2页
第2页 / 共9页
利用真值表法求取主析取范式以及主合取范式的实现_第3页
第3页 / 共9页
利用真值表法求取主析取范式以及主合取范式的实现_第4页
第4页 / 共9页
利用真值表法求取主析取范式以及主合取范式的实现_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《利用真值表法求取主析取范式以及主合取范式的实现》由会员分享,可在线阅读,更多相关《利用真值表法求取主析取范式以及主合取范式的实现(9页珍藏版)》请在金锄头文库上搜索。

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() = 0

4、;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;tem

5、plateSeqStack: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()i

6、f(IsEmpty()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(b

7、ool op) s.Push(op);bool Calculator:GetOperands(bool & opl, bool & op2)获取栈顶两个元素if (!s.Top(op1)cerr Missing operand! endl;return false;s.Pop();if (!s.Top(op2)cerr Missing operand! : s.Push(!right II 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;

9、t+)for (int 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 != (; sl.Top

10、(y), s1.Pop()pi+ = y;elseif(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 *cout *cout *cout *cout *cout *cout *欢迎进入逻辑运算软件主范式,支持括号)(可运算真值表,用!表示not 用I表示or用&表示and用表示蕴含 endl;/标语 * endl;* endl;* endl;* endl;* endl;* endl;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号