C 實現求主析取正規化 主合取正規化

2021-07-24 06:56:30 字數 1735 閱讀 1603

離散數學第一次上機實驗,突擊學完什麼是主析取正規化和主合取正規化之後粗略地想了下實現的思路。

第一步自然是把輸入的式子轉換成字尾表示式。結合資料結構書上對基本四則運算的**和前人的思路勉強寫出來乙個,但還是有很多地方可能一轉頭就會忘了怎麼實現。

轉換成字尾表示式之後的事情就顯得輕鬆多了。

下面貼**。

#include#includeusing namespace std;

class seqstack ;

seqstack::seqstack(int msize)

seqstack::~seqstack()

bool seqstack::push(char x)

bool seqstack::pop()

bool seqstack::empty()

char seqstack::top()

bool islegal(const string orgin)

return true;

}int and(int a,int b, seqstack &stack)

int or (int a, int b, seqstack &stack)

int not(int a, seqstack &stack)

int if(int a, int b, seqstack &stack)

int iif(int a, int b, seqstack &stack)

bool canin(char out,seqstack &stack)

switch (out)

if (i < o) //棧外優先順序比棧內高就進棧

return true;

else

return false;

}void infixtopostfix(string &orgin ,seqstack &stack)

} }if (number == 2)

} }return res;

}char num2bool(int t)

void print(int number, string &orgin, seqstack &stack)

}} }

if (number == 2)

} }//永真和永假的情況下長度為0

if (pdnf.length() != 0)

pdnf.erase(pdnf.length() - 2);

if (pcnf.length() != 0)

pcnf.erase(pcnf.length() - 2);

cout << "主析取正規化:" << pdnf << endl << endl;

cout << "主合取正規化:" << pcnf << endl << endl;

}/****************************主函式******************************/

int main()

cout << "輸入命題:";

int flag = 0;

do cin >> orgin;

flag = 1;

} while (!islegal(orgin));

infixtopostfix(orgin,stack);

print(number,orgin,stack);

while (1);

return 0;

}

析取正規化 合取正規化 主析取和主合取

了解一下簡單析取式和簡單合取式,簡單析取式說白了就是有限個命題變項或者他們的否定用符號v連線起來的式子,比如p,p,pvq,pv q等,簡單合取式類似,但它用的符號是 析取正規化簡單來說就是幾個簡單析取式的組合,合取正規化類似。而主合取和主析取略微有些不同,主析取是由極小項及符號v組成,極小項就是簡...

主析取正規化和主合取正規化的c 求解程式

主要思想是 先求真值表,再根據真值表輸出主析取正規化和主合取正規化。用!來表示非,用小寫v表示析取,用 來表示合取,用 來表示雙條件,用 來表示條件,用大寫字母來表示命題變元。真值表的求解 根據命題的個數n,得出所有命題變元賦值的情況個數sum,例如n 4個命題變元,則有sum 2 4種情況,並將所...

大二(上)離散數學 主析取正規化與主合取正規化

include include include include include define n 50 void panduan int b n int f 賦值函式 inttkh char sz n char ccu n int icu n int h0 分級運算函式 intfkh char sz...