Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #functions
  2. def editing(conv):
  3. conv= conv.replace ("not",'1')
  4. conv= conv.replace ("not",'1')
  5. conv = conv.replace ("!",'1')
  6. conv= conv.replace ("and",'2')
  7. conv= conv.replace ("&&",'2')
  8. conv= conv.replace ("AND",'2')
  9. conv= conv.replace ("OR",'3')
  10. conv= conv.replace ("or",'3')
  11. conv= conv.replace ("||",'3')
  12. conv= conv.replace ("implies",'4')
  13. conv= conv.replace ("imply",'4')
  14. conv= conv.replace ("->",'4')
  15. conv= conv.replace ("IMPLIES",'4')
  16. conv= conv.replace ("IMPLY",'4')
  17. conv= conv.replace ("<->",'5')
  18. conv= conv.replace ("iff",'5')
  19. conv= conv.replace ("IFF",'5')
  20. return conv
  21. def notGate(s):
  22. for index in range (len(s)):
  23. if s[index]=='0':
  24. s=s[0:index]+'1'+s[index+1:]
  25. else :
  26. s=s[0:index]+'0'+s[index+1:]
  27. return s
  28. def calc(s1,gate,s2):
  29. done=''
  30. if gate == '2':
  31. for index in range(len(s1)):
  32. if s1[index]=='1'and s2[index]=='1':
  33. done+='1'
  34. else: done+='0'
  35. elif gate == '3':
  36. for index in range(len(s1)):
  37. if s1[index]=='1'or s2[index]=='1':
  38. done+='1'
  39. else: done+='0'
  40. elif gate == '3':
  41. for index in range(len(s1)):
  42. if s1[index]=='1'and s2[index]=='0':
  43. done+='0'
  44. else: done+='1'
  45. elif gate == '4':
  46. for index in range(len(s1)):
  47. if (s1[index]=='1'and s2[index]=='1')or(s1[index]=='0'and s2[index]=='0'):
  48. done+='1'
  49. else: done+='0'
  50. return done
  51. #Main
  52. print("input :")
  53. n = int(input())
  54.  
  55. myMap= {}
  56. print("Enter your "+ str(n) + " variiables :")
  57. for i in range (n,0,-1):
  58. check =0
  59. mybool=1
  60. temp=''
  61. for j in range (pow(2,n)):
  62. if(check == pow(2,i-1)):
  63. check=0
  64. if(mybool == 0): mybool = 1
  65. else: mybool = 0
  66. temp+=str(mybool)
  67. check+=1
  68. myMap[str(input())] = temp
  69.  
  70. print ("Enter your equation :")
  71. eq=str(input())
  72. eq2=eq
  73. stack =[]
  74. eq=editing(eq)
  75. #making sure to put bracket in not
  76. for index in range (len(eq)):
  77. if eq[index]!='1':
  78. continue
  79. else:
  80. for indexBackward in range (index-1,0,-1):
  81. if eq[indexBackward]==' ':
  82. continue
  83. else:
  84. if eq[indexBackward]=='(':
  85. break
  86. elif eq[indexBackward] != '(':
  87. eq=eq[:index-1]+'('+eq[index]+')'+eq[index+2:]
  88. break
  89. profixed=str()
  90. for index in range (len(eq)):
  91. if eq[index]!=' ':
  92. if eq[index].isalpha():
  93. profixed +=eq[index]
  94. elif eq[index] == ')':
  95. while(stack[-1] != '('):
  96. profixed+=stack[-1]
  97. stack.pop()
  98. stack.pop()
  99. else:
  100. if not stack :
  101. stack.append(str(eq[index]))
  102. else :
  103. if stack[-1] < eq[index] and stack[-1] != '(':
  104. indexnew = index
  105. while(stack[-1] < eq[index]):
  106. profixed+=stack[-1]
  107. stack.pop()
  108. if not stack : break
  109. stack.append(str(eq[index]))
  110. else:
  111. stack.append(str(eq[index]))
  112. while stack :
  113. profixed+=stack[-1]
  114. stack.pop()
  115. #print(profixed)
  116. for index in range (len(profixed)):
  117. if profixed[index].isalpha():
  118. stack.append(myMap[profixed[index]])
  119. else :
  120. if profixed[index] != '1':
  121. temp1=stack[-1]
  122. stack.pop()
  123. temp2=stack[-1]
  124. stack.pop()
  125. stack.append (calc(temp2,profixed[index],temp1))
  126. else :
  127. temp1=stack[-1]
  128. stack.pop()
  129. stack.append(notGate(temp1))
  130. for index in myMap.keys():
  131. print (index +" | ",end=(""))
  132. length=myMap[index]
  133. print (">> "+eq2)
  134. for index in range (len(length)):
  135. for index2 in myMap.keys():
  136. #print ("index is "+str(index),end=(''))
  137. if myMap[index2][index]=='0':
  138. print ('F ',end=(''))
  139. else:
  140. print('T ',end=(''))
  141. if stack[0][index]=='0':
  142. print ('>> F ',end=(''))
  143. else:
  144. print('>> T ', end = (''))
  145. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement