진리표 생성기
AND, OR, NOT, XOR 불리언 표현식을 입력하면 완전한 진리표를 생성합니다.
Operators: AND, OR, NOT, XOR, NAND, NOR, XNOR (or &, |, !, ^)
자주 묻는 질문
코드 구현
from itertools import product
def truth_table(variables, expression):
"""Generate a truth table for a logical expression."""
headers = variables + [expression]
print(" | ".join(f"{h:>6}" for h in headers))
print("-" * (9 * len(headers)))
for values in product([False, True], repeat=len(variables)):
env = dict(zip(variables, values))
# Replace logical notation with Python
expr = expression
expr = expr.replace("AND", "and").replace("OR", "or").replace("NOT", "not")
expr = expr.replace("∧", " and ").replace("∨", " or ").replace("¬", " not ")
result = eval(expr, {}, env)
row = [str(v) for v in values] + [str(result)]
print(" | ".join(f"{v:>6}" for v in row))
# Example: P AND Q
truth_table(["P", "Q"], "P and Q")
print()
# Example: P OR (NOT Q)
truth_table(["P", "Q"], "P or (not Q)")
print()
# Check tautology: P OR NOT P
truth_table(["P"], "P or (not P)")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.