課本是高等教育出版社出版的《離散數學及其應用》。
程式會自動分析輸入的表示式,並且列出真值表,最後列印出主析取正規化和主合取正規化,最多支援256 個變元。
主要用到的演算法:中綴表示式轉字尾表示式、字尾表示式求值還有乙個二進位制加法模擬。
下面上2 個圖,第乙個是表示式開頭沒有非運算的(課本p85 例3.5.5):
第二個不但表示式開頭有非運算,而且非運算之後並不是乙個數值,而是乙個操作符(課本p83 例3.5.4):
下面是**,如果csdn 的編輯器弄亂了就將就著看吧,本來**的縮排和排版都是整潔的。
file:/
/* main.c
* use mingw developer studio to compile
* by ispeller (shell_way@foxmail.com)
*/#include #include #include #define _buf_len (1<<10)
#define _stack_len (_buf_len/2)
#define _prop_len (1<<7)
#define void_num (0) /* 不存在的運算數 */
typedef char * string;
typedef int bool;
#define true (1)
#define false (0)
typedef int data_t;
/* 防止各種老式編譯器來大姨媽 */
#ifndef _size_t
typedef unsigned int size_t;
#endif
#ifndef _ssize_t
typedef int ssize_t;
#endif
/* 正規化型別, 析取, 合取 */
enum paradigm ;
/* 優先順序大小 等於, 小於, 大於 */
enum priorities ;
/* 聯結詞(包括英文圓括號)優先順序,全真 */
/* 左括號, 非, 合取, 析取, 蘊含, 右括號, 結束符 */
enum ;
/* 4個棧,expr 儲存字尾表示式,truth_expr 是expr 的真值解釋
* ops 儲存聯結詞, truth 做字尾表示式求值棧,truth 最後存放表示式真值
*/struct stack expr, truth_expr, ops, truth;
string input_buf = null; /* 存放輸入的表示式 */
struct prop ;
struct table prop_table; /* 原子命題變元的列表 */
typedef int truth_item;
struct truth_table truth_table; /* 真值表 */
#define push(s,a) ((s).data[++(s).len] = (a))
#define pop(s) ((s).data[(s).len--])
#define is_empty(s) ((s).len+1)
#define get_top(s) ((s).data[(s).len])
#define init(s,m) (((s).len = -1) || ((s).len_max = (m)))
#define strcmp(a,r,b) (strcmp ((a), (b)) r 0)
/* 判斷字元是否是原子命題變元
*/bool
is_op (char c)
}/* 判斷運算子的優先順序
*/enum priorities
get_priority (char op1, char op2)
/* 進行資料運算
*/data_t
do_op (char op, bool num1, bool num2)
return truth;
}/* 判斷識別的命題變元是否已經存在
*/bool
prop_find (struct prop item)
/* 要求使用者輸入表示式和原子命題變元的真值
*/void
get_input ()
/* 獲取表示式 */
do input_buf[strlen (input_buf)-1] = '#'; /* 結束符號 */
if (strcmp ("exit#", ==, input_buf))
} while (strcmp ("#", ==, input_buf));
/* 識別原子命題變元和聯結詞並壓入變元列表 */
size = strlen (input_buf);
for (loc = input_buf; loc-input_buf < size; ++loc) }}
/* input_buf 中的表示式轉換為字尾表示式
*/void
make_postfix_expr () {
data_t item;
ssize_t count;
enum priorities level;
push (ops, '#'); /* 棧底元素,結束符號,優先順序最小 */
for (count=0; count0)
/* 同時要處理前面的位 */
for (count2=1; count2
條件命題p q的真值表的解疑
條件命題p q 1 若p是真,q是真,則 p q是真 2 若p是真,q是假,則 p q是假 3 若p是假,q是真,則 p q是真 4 若p是假,q是假,則 p q是真。我們主要的疑問集中在3和4。即為什麼條件p為假的時候,不管結果q如何,p q這個結論是真。的看上去比較靠譜的見解 這是規定而已,因為...
構造命題公式的真值表 biaobiao88
對給出的任意乙個命題公式 不超過四個命題變元 使學生會用c語言的程式程式設計表示出來,並且能夠計算它在各組真值指派下所應有的真值,畫出其真值表。includeusing namespace std struct int q 4 m int main for int i 0 i 4 i cout p交...
輸出命題公式的真值表 字尾表示式)
時限 1000ms 記憶體限制 10000k 總時限 3000ms 描述先輸入乙個正整數n n小於等於10 表示共有n個命題變元,再輸入乙個類似於逆波蘭表示式的字串表示乙個命題公式,約定在該字串中用一位的十進位制數表示乙個命題變元,用a o n i e分別表示且 或 非 蘊含 等值,用類似於逆波蘭表...