一份可以作为模板的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
from sympy import * A, B, C, D, F, F2 = symbols('A, B, C, D, F, F2') print('N A B C D | F')
bool_list = [] for i in range(16): tmp_list = [] str = '{0:0>4b}'.format(i) for j in range(4): num = int(str[j]) tmp_list.append(num) bool_list.append(tmp_list)
num = 0 for eachone in bool_list: print('{0:<4}'.format(num), end='') num += 1 for j in eachone: print('{0:<3}'.format(j), end='') F = (A | B | (C & D)).subs({A:eachone[0], B:eachone[1], C:eachone[2], D:eachone[3]}) each_res = 1 if F == True else 0 print('{0:<3}'.format('|'), end='') print('{0:<3}'.format(each_res), end='') print()
|
输出是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| N A B C D | F 0 0 0 0 0 | 0 1 0 0 0 1 | 0 2 0 0 1 0 | 0 3 0 0 1 1 | 1 4 0 1 0 0 | 1 5 0 1 0 1 | 1 6 0 1 1 0 | 1 7 0 1 1 1 | 1 8 1 0 0 0 | 1 9 1 0 0 1 | 1 10 1 0 1 0 | 1 11 1 0 1 1 | 1 12 1 1 0 0 | 1 13 1 1 0 1 | 1 14 1 1 1 0 | 1 15 1 1 1 1 | 1
|
对于四元逻辑函数,我们在求不同函数的真值表时,只需要更换下面这一行代码即可
| F = (A | B | (C & D)).subs({A:eachone[0], B:eachone[1], C:eachone[2], D:eachone[3]})
|