版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、#include #include #include #include using namespace std;char str100; 輸入的命題公式int tv20 = 0;/真值指派的數(shù)組int length;/命題公式長度char expression100;/將命題公式中的命題變元變?yōu)檎嬷岛蟮臄?shù)組int icp(const char c)/聯(lián)結(jié)詞的棧外優(yōu)先級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)/聯(lián)結(jié)詞的棧內(nèi)優(yōu)先級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()/將輸入的字符串轉(zhuǎn)化為可計算的表達式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; /邏輯聯(lián)結(jié)詞不變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; /相同的命題變元復(fù)制賦值int Calculator:Run()SeqStack s1;/ 聯(lián)結(jié)詞棧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; 輸出并返回最終結(jié)果 void Calculator:Solve()cout *cout *cout *cout *cout *co
11、ut *cout *歡迎進入邏輯運算軟件主范式,支持括號)(可運算真值表,用!表示not 用I表示or用&表示and用表示蘊含 endl;/標語 * endl;* endl;* endl;* endl;* endl;* endl;cout * endl;cout str;/輸入命題公式length = strlen(str) - 1;char index10;/命題變元數(shù)組for (int i = 0; i length; i+) /逐次添加命題變元if (isalpha(stri) & (flag = 1)indexcount+ = stri;flag=1;for (int k = 0; k
12、 count; k+)if (indexk = stri+1)flag=0;if (!count)cout ”無命題變元,重新輸入!” endl;system(pause);system(cls);Solve();cout ”真值表:vv 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 V ”;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) cout ” / ;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年貴州裝備制造職業(yè)學院單招職業(yè)適應(yīng)性考試題庫及參考答案詳解1套
- 2026年鶴壁職業(yè)技術(shù)學院單招職業(yè)適應(yīng)性測試題庫參考答案詳解
- 2026年成都藝術(shù)職業(yè)大學單招職業(yè)傾向性測試題庫及參考答案詳解一套
- 2026年四川化工職業(yè)技術(shù)學院單招職業(yè)技能考試題庫含答案詳解
- 2026年滄州航空職業(yè)學院單招職業(yè)傾向性測試題庫及參考答案詳解
- 工廠消防面試題及答案
- 四川省南充市嘉陵一中2024-2025學年高二上學期第二次月考(11月)英語試題含聽力含答案英語試題
- 萬洋安全員面試題及答案
- 2025年溫州東甌中學招聘工作人員備考題庫含答案詳解
- 2025年安慶市桐城師范高等??茖W校公開招聘工作人員8人備考題庫完整參考答案詳解
- 2025安徽淮北相山區(qū)招考村(社區(qū))后備干部66人模擬筆試試題及答案解析
- 藥物警戒培訓課件
- 汽車銷售實務(wù)(第3版)課件 學習情境七 車輛交付
- 煙花爆竹行業(yè)事故應(yīng)急救援處置培訓
- 外貿(mào)企業(yè)出口退稅計算及賬務(wù)處理會計分錄
- 中國甲狀腺相關(guān)眼病診斷和治療指南(2022年)解讀
- 壓力容器生產(chǎn)單位壓力容器質(zhì)量安全日管控、周排查、月調(diào)度制度(含表格記錄)
- 遼寧省《公共機構(gòu)能源資源消費統(tǒng)計制度》實施方案
- 初三語文下冊課文威尼斯商人原文
- GB/T 19362.1-2003龍門銑床檢驗條件精度檢驗第1部分:固定式龍門銑床
- GB/T 14647-2008氯丁二烯橡膠CR121、CR122
評論
0/150
提交評論