-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (92 loc) · 4.17 KB
/
main.py
File metadata and controls
125 lines (92 loc) · 4.17 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import re
import unicodedata
from itertools import product
from tabulate import tabulate
def parse_input(input_text):
"""Parses the given text and returns a list of functions."""
functions = []
lines = input_text.strip().split("\n")
for line in lines:
if "=" in line:
name, expression = line.split("=", 1)
functions.append((name.strip(), expression.strip()))
return functions
def preprocess_expression(expression):
"""Preprocesses the expression to replace logical operators with Python equivalents."""
# Normalize and replace logical operators
expression = unicodedata.normalize('NFC', expression)
expression = expression.replace("∧", " and ").replace("∨", " or ").replace("¬", " not ")
# Handle overlined variables (e.g., x̅ -> not x)
expression = re.sub(r'([a-zA-Z])̅', r'not \1', expression)
# Handle negation of entire brackets (e.g., \overline{(x ∧ y)} -> not (x and y))
expression = re.sub(r'̅\((.+?)\)', r'not (\1)', expression)
return expression
def replace_variables(expression, variables):
"""Replaces variables in the expression with their values."""
for var, value in variables.items():
# Replace overlined variables (e.g., x̅ -> not value)
expression = re.sub(fr"{var}̅", f"not {value}", expression)
# Replace normal variables (e.g., x -> value)
expression = re.sub(fr"\b{var}\b", str(value), expression)
return expression
def evaluate_expression(expression, variables):
"""Evaluates the logical expression and records the computation steps."""
steps = []
# Replace variables with their values
replaced_expression = replace_variables(expression, variables)
steps.append(f"Replaced variables: {replaced_expression}")
# Preprocess logical operators
processed_expression = preprocess_expression(replaced_expression)
steps.append(f"Preprocessed expression: {processed_expression}")
try:
result = int(eval(processed_expression)) # Evaluate the expression
except Exception as e:
result = None
steps.append(f"Error during evaluation: {e}")
steps.append(f"Final result: {result}")
return result, "\n".join(steps)
def generate_truth_table(functions, variables):
"""Generates a truth table with detailed steps."""
variable_names = sorted(variables)
combinations = list(product([0, 1], repeat=len(variable_names)))
truth_table = []
for combination in combinations:
current_vars = dict(zip(variable_names, combination))
row = list(combination)
for name, expression in functions:
result, steps = evaluate_expression(expression, current_vars)
row.append(result)
row.append(steps) # Append steps for each function
truth_table.append(row)
return variable_names, truth_table
def display_truth_table(variable_names, truth_table, functions):
"""Displays the truth table including steps."""
headers = variable_names + [name for name, _ in functions] + ["Steps"]
table = []
for row in truth_table:
# Separate results and steps
steps = row[len(variable_names) + len(functions):]
table.append(row[:len(variable_names) + len(functions)] + ["; ".join(steps)])
print(tabulate(table, headers=headers, tablefmt="grid"))
if __name__ == "__main__":
print("Enter logical functions (one per line).")
print("Example: g1(x, y) = (x ∨ y̅) ∧ (x ∨ y)")
print("Press Enter on an empty line to finish input.\n")
input_lines = []
while True:
line = input()
if not line:
break
input_lines.append(line)
input_text = "\n".join(input_lines)
functions = parse_input(input_text)
if not functions:
print("No valid functions entered.")
else:
# Determine all unique variables
all_variables = set()
for _, expression in functions:
all_variables.update(re.findall(r'\b[a-zA-Z]\b', expression))
variables = {var: 0 for var in all_variables}
variable_names, truth_table = generate_truth_table(functions, variables)
display_truth_table(variable_names, truth_table, functions)