# python实现真值表

    import copy

def boo(x,leng,listboo,array):              #递归实现布尔真值全排列
if x==leng:
for i in range (0,leng):
array.append(listboo[i])
return (listboo)
else:
listboo[x]=1
boo(x+1,leng,listboo,array)
listboo[x]=0
boo(x+1,leng,listboo,array)

def loc_and(l):                             #与运算
i=l.index('*')
k = l[i - 1] * l[i + 1]
del l[i - 1], l[i - 1], l[i - 1]
l.insert(i - 1, int(k))
return (l)

def loc_or(l):                              #或运算
i=l.index('+')
k = l[i - 1] + l[i + 1]
del l[i - 1], l[i - 1], l[i - 1]
if k!=0:
k=1
l.insert(i - 1, int(k))
return (l)

def loc_no(l):                              #非运算
i=l.index('-')
if l[i + 1] == 1:
k = 0
elif l[i + 1] == 0:
k=1
del l[i], l[i]
l.insert(i , int(k))
return (l)

def loc_con(l):                             #蕴含运算
i=l.index('~')
if l[i - 1] == 1 and l[i + 1]==0:
k = 0
else:
k=1
del l[i - 1], l[i - 1], l[i - 1]
l.insert(i - 1, int(k))
return (l)

def loc_eq(s):                              #全等运算
i=s.index('=')
if s[i - 1] == s[i + 1]:
k = 1
else:
k=0
del s[i - 1], s[i - 1], s[i - 1]
s.insert(i - 1, int(k))
return (s)

def fun(s):                                 #运算集成函数
while 1:
if '-' in s:
loc_no(s)
elif '+' in s:
loc_or(s)
elif '*' in s:
loc_and(s)
elif '~' in s:
loc_con(s)
elif '=' in s:
loc_eq(s)
else:
break
return (s)

def cort(expre):                            #运算栈
if "(" in expre:
while 1:
if ")" in expre:
b = expre.index(")")
for i in range(b,-1,-1):

if expre[i] == "(":
new_list = []
new_list=expre[i+1:b]
byte=fun(new_list)[0]
del(expre[i:b+1])
expre.insert(i,byte)
break
else:
return fun(expre)

else:
ans=fun(expre)
return ans

#主代码

print("规定几种运算符号: \n 或运算     +\n 与运算     *\n 非运算     -\n 蕴含运算   ~\n 全等运算   =")

listch=[]
listnum=[]
l=[]
listok=[]
array=[]
listcom=[]
t=0
newl=[]
print("请输入合式公式")
x=input()
l=list(x)

for i in range(0,len(l)):
char=l[i]
if (ord(char)>=65 and ord(char)<=90)or(ord(char)>=97 and ord(char)<=122):
if char not in listch:
listch.append(l[i])
listnum.append(0)
else:
listcom.append(l[i])
#for i in range(0,len(listch)):
#\*listnum.append(0)*\

boo(0,len(listch),listnum,array)
i=0
while i<len(array):
listok.append(array[i:i+len(listch):])
i+=len(list(listch))
print(listch,' ','结果')
for i in range(0,len(listok)):
o=0
newl=copy.deepcopy(l)
for o in range(0,len(listch)):
for j in range(0,len(l)):
char=l[j]
if char == listch[o]:
newl[j]=int(listok[i][o])
o+=1
print(listok[i],'     ',cort(newl))

input()