-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
164 lines (123 loc) · 4.1 KB
/
main.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
print("-"*160)
def convert_str_to_lst(p):
return [char for char in p if char != ' ']
def replace_operators(prop):
for i in range(len(prop)):
if prop[i] == '~':
prop[i] = '⇒'
elif prop[i] == '/':
prop[i] = '∨'
elif prop[i] == '=':
prop[i] = '⇔'
elif prop[i] == '&' or prop[i] == '^':
prop[i] = '∧'
return prop
def get_atoms(prop):
interpreters = {}
for i in prop:
if 'A' <= i <= 'Z':
if i not in interpreters:
interpreters[i] = "null"
return interpreters
def receive_interpretations(atomi):
print()
for i in sorted(atomi):
print("Interpretarea lui", i, "este: ", end='')
atomi[i] = int(input())
print()
return atomi
def replace_atoms_with_interpretations(prop, atomi):
for i in range(len(prop)):
if prop[i] in atomi:
prop[i] = atomi[prop[i]]
return prop
prop = input("Introduceti sintaxa abstracta data de programul ce returneaza sub forma de lista formula propozitionala: ")
prop = prop.split()
p = input("Introduceti formula propozitionala originala: ")
p = convert_str_to_lst(p)
prop = replace_operators(prop)
p = replace_operators(p)
p = ''.join(p)
atomi = get_atoms(prop)
atomi = receive_interpretations(atomi)
prop = replace_atoms_with_interpretations(prop, atomi)
# print(prop) # - debug
rez = "null"
for i in range(len(prop)-1, -1, -1):
# Warning approach
if prop[i] == '!':
prop[i+1] = int(not(prop[i+1]))
rez = prop[i+1]
for j in range(i, len(prop)):
if prop[i] != 0 or prop[i] != 1:
prop[i] = '*'
# print(prop)# - debug
elif prop[i] in ['⇒','∨','⇔','∧']:
if prop[i] == '∧':
a, b = '*', '*'
for j in range(i+1, len(prop)):
if prop[j] == 1 or prop[j] == 0:
if a == '*':
a = prop[j]
prop[j] = '*'
continue
else:
b = prop[j]
prop[j] = '*'
break
# print(a, b)# - debug
prop[i] = int(int(a) and int(b))
rez = prop[i]
# print(prop)# - debug
elif prop[i] == '⇔':
a, b = '*', '*'
for j in range(i+1, len(prop)):
if prop[j] == 0 or prop[j] == 1:
if a == '*':
a = prop[j]
prop[j] = '*'
continue
else:
b = prop[j]
prop[j] = '*'
break
# print(a,b)# - debug
prop[i] = int(a == b)
rez = prop[i]
# print(prop)# - debug
elif prop[i] == '⇒':
a, b = '*', '*'
for j in range(i+1, len(prop)):
if prop[j] == 1 or prop[j] == 0:
if a == '*':
a = prop[j]
prop[j] = '*'
continue
else:
b = prop[j]
prop[j] = '*'
break
# print(a,b)# - debug
prop[i] = int(not(a) or b)
rez = prop[i]
# print(prop)# - debug
if prop[i] == '∨':
a, b = '*', '*'
for j in range(i+1, len(prop)):
if prop[j] == 1 or prop[j] == 0:
if a == '*':
a = prop[j]
prop[j] = '*'
continue
else:
b = prop[j]
prop[j] = '*'
break
# print(a,b)# - debug
prop[i] = int(a or b)
rez = prop[i]
# print(prop)# - debug
# print(prop)# - debug
print()
print("Valoarea propozitiei", p, "sub intepretarea I₀:", [atomi[x] for x in atomi], "este", rez, '(', 'True' if rez == 1 else 'False', ')')
print("-"*160)