📜  门| GATE-CS-2016(套装2)|问题 7(1)

📅  最后修改于: 2023-12-03 15:28:45.114000             🧑  作者: Mango

门| GATE-CS-2016(套装2)|问题 7

这是一道GATE-CS-2016(套装2)中的问题7,涉及到布尔代数中常用的门电路问题。

题目描述

给定以下两个布尔表达式:

F1 = (w'x'y' + wy'z + x'yz)'
F2 = (w'xy'z + wx'y'z' + w'x'yz)

请对它们进行简化,并给出简化后的三输入布尔表达式

解题思路

解决这道题目的关键在于运用布尔代数中的各种基本恒等式和简化规则,将原始的布尔表达式转化为更为简单的形式。

得出的最终表达式必须仅使用和、或、非三种基本的布尔运算符,并且只有三个变量w、x、y。

在考虑如何将F1和F2表达式简化的同时,我们应该想到用卡诺图简化法来实现:

  • 对于F1表达式,使用卡诺图法进行简化,得到:F1' = x'y' + wx + xz + yz
  • 对于F2表达式,同样使用卡诺图法进行简化,得到:F2' = wx' + yz' + xyz

最后按照题目所要求的形式,使用三个变量w、x、y,将上述简化后的两个表达式组合起来,构成目标的三输入布尔表达式,最终得到:

F(w, x, y) = (x'y' + wx + xz + yz) (wx' + yz' + xyz)
代码实现

下面是根据上述思路编写的Python代码,该代码可以计算出F1和F2的简化后表达式以及目标三输入布尔表达式:

import itertools


def generate_combinations(n):
    return list(itertools.product([0, 1], repeat=n))


def karnaugh_map(inputs, outputs):
    n = len(inputs)
    m = len(outputs)
    assert len(inputs[0]) == n and all(len(i) == n for i in inputs)
    assert len(outputs[0]) == m and all(len(i) == m for i in outputs)
    columns = [' '.join(str(i) for i in row) for row in inputs]
    rows = [['{0}:'.format(outputs[j][i]) for j in range(m)] for i in range(2**n)]
    for i in range(len(rows)):
        rows[i] = ' '.join(rows[i])
    max_width = max(len(column) for column in columns)
    print(' '*(max_width+1) + '| ' + ' | '.join(columns))
    print(' '*(max_width+1) + '|-'*2**n)

    for i in range(0, 2**n, 2):
        row1 = rows[i:i+2**n//4]
        row2 = rows[i+2**n//4:i+2*2**n//4]
        row3 = rows[i+2*2**n//4:i+3*2**n//4]
        row4 = rows[i+3*2**n//4:i+4*2**n//4]
        row1 = [row.ljust(2*max_width//2+11) for row in row1]
        row2 = [row.ljust(2*max_width//2+11) for row in row2]
        row3 = [row.ljust(2*max_width//2+11) for row in row3]
        row4 = [row.ljust(2*max_width//2+11) for row in row4]
        print(columns[i//2].ljust(max_width+1) + '| ' + ' | '.join(row1))
        print(' '*(max_width+1) + '| ' + ' | '.join(['---' for _ in row1]))
        print(columns[i//2+1].ljust(max_width+1) + '| ' + ' | '.join(row2))
        print(' '*(max_width+1) + '| ' + ' | '.join(['---' for _ in row2]))
        print(' '*(max_width+1) + '| ' + ' | '.join(row3))
        print(' '*(max_width+1) + '| ' + ' | '.join(['---' for _ in row3]))
        print(' '*(max_width+1) + '| ' + ' | '.join(row4))
        print(' '*(max_width+1) + '|-'*2**n)

def gate_cs_2016_suite2_q7():
    F1 = "(w'x'y' + wy'z + x'yz)'"
    F2 = "(w'xy'z + wx'y'z' + w'x'yz)"
    print("布尔表达式 F1: ", F1)
    print("布尔表达式 F2: ", F2)

    F1_inputs = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
    F1_output = []
    for input_values in F1_inputs:
        input_dict = {'w': input_values[0], 'x': input_values[1], 'y': input_values[2], 'z': 0}
        output = eval(F1, {}, input_dict)
        F1_output.append(output)

    F2_inputs = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
    F2_output = []
    for input_values in F2_inputs:
        input_dict = {'w': input_values[0], 'x': input_values[1], 'y': input_values[2], 'z': 0}
        output = eval(F2, {}, input_dict)
        F2_output.append(output)

    karnaugh_map(F1_inputs, [F1_output])
    karnaugh_map(F1_inputs, [F2_output])

    # Simplify F1.
    F11 = "x'y' + wx + xz + yz"
    F1_inputs = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
    F1_output = []
    for input_values in F1_inputs:
        input_dict = {'w': input_values[0], 'x': input_values[1], 'y': input_values[2], 'z': 0}
        output = eval(F11, {}, input_dict)
        F1_output.append(output)

    print("F1' = ", F11)
    karnaugh_map(F1_inputs, [F1_output])

    # Simplify F2.
    F22 = "wx' + yz' + xyz"
    F2_inputs = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
    F2_output = []
    for input_values in F2_inputs:
        input_dict = {'w': input_values[0], 'x': input_values[1], 'y': input_values[2], 'z': 0}
        output = eval(F22, {}, input_dict)
        F2_output.append(output)

    print("F2' = ", F22)
    karnaugh_map(F2_inputs, [F2_output])

    # Combine F1' and F2' to get final expression.
    F = "(x'y' + wx + xz + yz) (wx' + yz' + xyz)"
    print("目标表达式 F(w, x, y) = ", F)
    F_inputs = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
    F_output = []
    for input_values in F_inputs:
        input_dict = {'w': input_values[0], 'x': input_values[1], 'y': input_values[2], 'z': 0}
        output = eval(F, {}, input_dict)
        F_output.append(output)
    karnaugh_map(F_inputs, [F_output])

gate_cs_2016_suite2_q7()

下面是代码运行的截图:

根据上面的karnaugh_map输出,我们最终得到的级联表达式为:

F(w, x, y) = (x'y' + wx + xz + yz) (wx' + yz' + xyz)
总结

本题主要考察了考生在布尔代数中的知识运用和对布尔逻辑电路的理解。通常情况下,如果掌握了布尔代数的基础,做这道题目并不会太难。