版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学初级教师个人工作述职报告(29篇)
- 二年级劳动暑假衔接维修维护常识方案设计题题组训练卷真题迁移版
- 制罐基础知识、基本工艺
- 2026极端环境下激光焊接花岗岩结构件服役性能衰减机制报告
- 2026专精特新小巨人企业融资视角下手动双梁起重机项目估值逻辑报告
- 2026年新疆住院医师-新疆住院医师神经外科历年参考题库含答案解析
- 2026年操作工技能考核考试-压缩机工高级技师历年参考题库含答案解析
- 2026年广西住院医师-广西住院医师临床医学历年参考题库含答案解析
- 2026年安全知识安全生产知识竞赛-电气安全知识竞赛历年参考题库含答案解析
- 2026年大学试题(财经商贸)-广告学历年参考题库含答案解析
- 2026年秋浙美版新教材小学美术五年级上册教学计划及进度表
- 2026年秋季新教材浙美版小学美术六年级上册(全册)教案(附目录p94)
- 高一数学 开学第一课 课件-2026-2027学年高一上学期数学人教A版必修第一册
- 2026年安徽矾花源景区运营管理有限公司(筹) 招聘14人考试备考题库及答案详解
- 云南省公路工程竣工文件编制及立卷归档实 用手册
- 2026秋小学湘艺版音乐三年级上册(新教材)教学计划附教学进度表
- 2026下半年上海杨浦区卫健系统事业单位专业技术人员招聘93人笔试题库附答案详解【预热题】
- 长江产业投资集团招聘笔试题目及答案解析
- (2026年秋)人教PEP版五年级上册英语教案
- 新版部编人教版六年级上册道德与法治(课件)第1课 法律是什么
- 2026年二级建造师继续教育试题加答案
评论
0/150
提交评论