Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #functions
- def editing(conv):
- conv= conv.replace ("not",'1')
- conv= conv.replace ("not",'1')
- conv = conv.replace ("!",'1')
- conv= conv.replace ("and",'2')
- conv= conv.replace ("&&",'2')
- conv= conv.replace ("AND",'2')
- conv= conv.replace ("OR",'3')
- conv= conv.replace ("or",'3')
- conv= conv.replace ("||",'3')
- conv= conv.replace ("implies",'4')
- conv= conv.replace ("imply",'4')
- conv= conv.replace ("->",'4')
- conv= conv.replace ("IMPLIES",'4')
- conv= conv.replace ("IMPLY",'4')
- conv= conv.replace ("<->",'5')
- conv= conv.replace ("iff",'5')
- conv= conv.replace ("IFF",'5')
- return conv
- def notGate(s):
- for index in range (len(s)):
- if s[index]=='0':
- s=s[0:index]+'1'+s[index+1:]
- else :
- s=s[0:index]+'0'+s[index+1:]
- return s
- def calc(s1,gate,s2):
- done=''
- if gate == '2':
- for index in range(len(s1)):
- if s1[index]=='1'and s2[index]=='1':
- done+='1'
- else: done+='0'
- elif gate == '3':
- for index in range(len(s1)):
- if s1[index]=='1'or s2[index]=='1':
- done+='1'
- else: done+='0'
- elif gate == '3':
- for index in range(len(s1)):
- if s1[index]=='1'and s2[index]=='0':
- done+='0'
- else: done+='1'
- elif gate == '4':
- for index in range(len(s1)):
- if (s1[index]=='1'and s2[index]=='1')or(s1[index]=='0'and s2[index]=='0'):
- done+='1'
- else: done+='0'
- return done
- #Main
- print("input :")
- n = int(input())
- myMap= {}
- print("Enter your "+ str(n) + " variiables :")
- for i in range (n,0,-1):
- check =0
- mybool=1
- temp=''
- for j in range (pow(2,n)):
- if(check == pow(2,i-1)):
- check=0
- if(mybool == 0): mybool = 1
- else: mybool = 0
- temp+=str(mybool)
- check+=1
- myMap[str(input())] = temp
- print ("Enter your equation :")
- eq=str(input())
- eq2=eq
- stack =[]
- eq=editing(eq)
- #making sure to put bracket in not
- for index in range (len(eq)):
- if eq[index]!='1':
- continue
- else:
- for indexBackward in range (index-1,0,-1):
- if eq[indexBackward]==' ':
- continue
- else:
- if eq[indexBackward]=='(':
- break
- elif eq[indexBackward] != '(':
- eq=eq[:index-1]+'('+eq[index]+')'+eq[index+2:]
- break
- profixed=str()
- for index in range (len(eq)):
- if eq[index]!=' ':
- if eq[index].isalpha():
- profixed +=eq[index]
- elif eq[index] == ')':
- while(stack[-1] != '('):
- profixed+=stack[-1]
- stack.pop()
- stack.pop()
- else:
- if not stack :
- stack.append(str(eq[index]))
- else :
- if stack[-1] < eq[index] and stack[-1] != '(':
- indexnew = index
- while(stack[-1] < eq[index]):
- profixed+=stack[-1]
- stack.pop()
- if not stack : break
- stack.append(str(eq[index]))
- else:
- stack.append(str(eq[index]))
- while stack :
- profixed+=stack[-1]
- stack.pop()
- #print(profixed)
- for index in range (len(profixed)):
- if profixed[index].isalpha():
- stack.append(myMap[profixed[index]])
- else :
- if profixed[index] != '1':
- temp1=stack[-1]
- stack.pop()
- temp2=stack[-1]
- stack.pop()
- stack.append (calc(temp2,profixed[index],temp1))
- else :
- temp1=stack[-1]
- stack.pop()
- stack.append(notGate(temp1))
- for index in myMap.keys():
- print (index +" | ",end=(""))
- length=myMap[index]
- print (">> "+eq2)
- for index in range (len(length)):
- for index2 in myMap.keys():
- #print ("index is "+str(index),end=(''))
- if myMap[index2][index]=='0':
- print ('F ',end=(''))
- else:
- print('T ',end=(''))
- if stack[0][index]=='0':
- print ('>> F ',end=(''))
- else:
- print('>> T ', end = (''))
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement