蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线穷举四大基本逻辑门真值表、德摩根定律与生活门禁场景映射

PYTHONWindows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。explore-boolean-algebra-logic-gates.py
explore-boolean-algebra-logic-gates.py159 行
"""Offline Computer Organization lab for Chapter 07 (boolean-algebra-logic-gates)."""
import json

VERSION = "CPython 3.12.14 offline computer organization simulation verifier"


def AND(a: int, b: int) -> int:
    return 1 if (a == 1 and b == 1) else 0


def OR(a: int, b: int) -> int:
    return 1 if (a == 1 or b == 1) else 0


def NOT(a: int) -> int:
    return 1 if a == 0 else 0


def XOR(a: int, b: int) -> int:
    return 1 if a != b else 0


def case_basic_gates_truth_tables():
    pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
    and_tt = [AND(a, b) for a, b in pairs]
    or_tt = [OR(a, b) for a, b in pairs]
    not_tt = [NOT(a) for a in (0, 1)]
    xor_tt = [XOR(a, b) for a, b in pairs]
    xor_from_basic = [OR(AND(NOT(a), b), AND(a, NOT(b))) for a, b in pairs]
    actual = {
        "AND": and_tt,
        "OR": or_tt,
        "NOT": not_tt,
        "XOR": xor_tt,
        "xor_composite_matches": xor_tt == xor_from_basic,
    }
    expected = {
        "AND": [0, 0, 0, 1],
        "OR": [0, 1, 1, 1],
        "NOT": [1, 0],
        "XOR": [0, 1, 1, 0],
        "xor_composite_matches": True,
    }
    return {
        "name": "basic_gates_truth_tables",
        "summary": "穷举校验 AND/OR/NOT/XOR 四种基本逻辑门真值表及 XOR=(A'·B)+(A·B') 组合等价性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_demorgan_laws_exhaustive():
    pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
    law1_ok = all(NOT(AND(a, b)) == OR(NOT(a), NOT(b)) for a, b in pairs)
    law2_ok = all(NOT(OR(a, b)) == AND(NOT(a), NOT(b)) for a, b in pairs)
    details = [
        {
            "a": a,
            "b": b,
            "law1": (NOT(AND(a, b)), OR(NOT(a), NOT(b))),
            "law2": (NOT(OR(a, b)), AND(NOT(a), NOT(b))),
        }
        for a, b in pairs
    ]
    actual = {"law1_all_hold": law1_ok, "law2_all_hold": law2_ok, "cases": details}
    expected = {
        "law1_all_hold": True,
        "law2_all_hold": True,
        "cases": [
            {"a": 0, "b": 0, "law1": (1, 1), "law2": (1, 1)},
            {"a": 0, "b": 1, "law1": (1, 1), "law2": (0, 0)},
            {"a": 1, "b": 0, "law1": (1, 1), "law2": (0, 0)},
            {"a": 1, "b": 1, "law1": (0, 0), "law2": (0, 0)},
        ],
    }
    return {
        "name": "demorgan_laws_exhaustive",
        "summary": "对全部 4 组二元输入穷举验证德摩根第一、第二定律",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_combined_truth_table_and_laws():
    pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
    combined = [
        [a, b, AND(a, b), OR(a, b), NOT(a), NOT(b), XOR(a, b)]
        for a, b in pairs
    ]
    # Boolean algebra laws: idempotent OR(1,1)=1, double negation NOT(NOT(a))=a
    idempotent_ok = (OR(1, 1) == 1) and (AND(1, 1) == 1)
    double_neg_ok = all(NOT(NOT(a)) == a for a in (0, 1))
    actual = {
        "combined_table": combined,
        "idempotent_ok": idempotent_ok,
        "double_neg_ok": double_neg_ok,
    }
    expected = {
        "combined_table": [
            [0, 0, 0, 0, 1, 1, 0],
            [0, 1, 0, 1, 1, 0, 1],
            [1, 0, 0, 1, 0, 1, 1],
            [1, 1, 1, 1, 0, 0, 0],
        ],
        "idempotent_ok": True,
        "double_neg_ok": True,
    }
    return {
        "name": "combined_truth_table_and_laws",
        "summary": "校验交互真值表全量 7 列输出及布尔代数幂等律(1+1=1)与双重否定律",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_real_world_gate_scenarios():
    actual = {
        "vault_two_keys_both_present": AND(1, 1),
        "vault_two_keys_one_missing": AND(1, 0),
        "building_two_doors_one_open": OR(0, 1),
        "building_two_doors_both_closed": OR(0, 0),
        "status_sign_flip_from_free": NOT(0),
        "two_lamps_mutex_valid": XOR(1, 0),
        "two_lamps_mutex_alarm_when_same": NOT(XOR(1, 1)),
    }
    expected = {
        "vault_two_keys_both_present": 1,
        "vault_two_keys_one_missing": 0,
        "building_two_doors_one_open": 1,
        "building_two_doors_both_closed": 0,
        "status_sign_flip_from_free": 1,
        "two_lamps_mutex_valid": 1,
        "two_lamps_mutex_alarm_when_same": 1,
    }
    return {
        "name": "real_world_gate_scenarios",
        "summary": "验证门禁双钥匙(AND)、双入口(OR)、状态翻转(NOT)与互斥双灯(XOR)四类现实逻辑场景",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_basic_gates_truth_tables(),
        case_demorgan_laws_exhaustive(),
        case_combined_truth_table_and_laws(),
        case_real_world_gate_scenarios(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。

入口文件:projects/computer-organization-boolean-algebra-logic-gates-lab/explore-boolean-algebra-logic-gates.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-boolean-algebra-logic-gates.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:计算机组成原理

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2617.zip

下载课程关联工程包:计算机组成原理

课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习