利用真值表法求取主析取范式以及主合取范式的實現(xiàn)_第1頁
利用真值表法求取主析取范式以及主合取范式的實現(xiàn)_第2頁
利用真值表法求取主析取范式以及主合取范式的實現(xiàn)_第3頁
利用真值表法求取主析取范式以及主合取范式的實現(xiàn)_第4頁
利用真值表法求取主析取范式以及主合取范式的實現(xiàn)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論