Você está na página 1de 4

##

##
##
##
##
##
##
##

Copyright (C) 2016 Originaly by Jeshurun Rojas - All Rights Reserved


This program demonstrates Quine Mc-Cluskey method to simplify boolean
functions. Unfortunately, the program is still under improvement. It
is only able to produce an unsimplified form of boolean expression.
Try to input the examples from our lecture notes and see the results ^^.
You may use, distribute and modify this code. Thank you and Godbless!
P.S. Epic ung Final Reduction, di pa kase sya able magretrieve from previous
reduction, kaya for now nireretrieve ko muna from Truth table

from copy import deepcopy


from nltk.tokenize import word_tokenize
inputhere = raw_input('Enter Sum of Minterms: ')
minterms_list = map(int, inputhere.split(','))
minterms_copy = []
for a in minterms_list:
minterms_copy.append(a)
##Here are some examples:
## 1) Enter Sum of Minterms: 0,2,4,5,6,8,10,11,12,14
##Ung sagot, hindi pa super simplified kasi imbis na iretrieve ko sa
##previous reduction table ung digit 5 and 11, niretrieve ko sila sa mismong
##truth table... medyo mahirap ung algorithm sa part na to kasi kelangan mong
##magcode ng feedback system to recheck previous reductions whether it contain
##the missing digits... which is mahirap, sa tingin ko, nangangalahati palang ak
o
##sa coding...
## 2) Enter Sum of Minterms: 0,2,5,6,7,10,13,14,15
##both essential and non essential prime implicants are coexisting, which is als
o
##a major concern. medyo mahirap rin kasi ung algorithm sa Final Tabulation..
## 3) Enter Sum of Minterms: 1,4,6,7,8,9,10,11,15
##anlayo ng sagot diba.. haha, pero kung mapapansin nyo, ung reduction table nya
,
##is tama... only that, digits 1,4,6,7,15 were retrieved from the truth table
##and not in the first reduction... teka ginagawa ko pa yan haha
##BITLEVEL COUNTER-nalalaman nya ung bitlength at kung ilang variable ang
##magagamit based sa input mong decimals.
bit_length_temp = 0
bit_zero = 0
for a in minterms_list:
for b in range(0,100):
if a<2**(b+1) and a>=2**b:
bit_length_temp = b+1
break
if bit_length_temp>bit_zero:
bit_length = bit_length_temp
##END
##DECIMAL TO BINARY-convert nya lang lahat ng input mong decimal
binary = []
minterms_dict = {}
for a in minterms_list:
element = a
while element !=0:
r = element/2
s = element%2

element = r
binary.insert(0,s)
while len(binary)<bit_length:
binary.insert(0,0)
minterms_dict[a] = binary
binary = []
##END
##BITLENGTH REVERSE-hmmm.. kase sa para pag nagreduction na mamaya, magsisimula
sya
##sa LSB which is nonsense naman.. ehh kaso gusto ko dun magsimula ng checking e
e..
j = 0
bit_level = []
while j!=bit_length:
bit_level.insert(0,j)
j = j+1
##END
##MULTI REDUCTION - ito po ay infinite reduction ng input mo... titigil lang sya
##once na wala na syang marereduce pa xD haha reduce... This is the heart of the
##code... but not the brain... kung para san yang mga for loop na yan, nagiging
3d kasi
##ung table twing nag cocompare tau ng mano mano... di lang natin namamalayan,,
##Yun ang purpose ng for loops a, b, and c... ung for loop e and f naman ay to c
heck
##na halimbawa... first reduction palang, so dapat one dash lang ang meron or el
se,
##tatanggalin nya... pag second reduction, dalawang dash naman dapat... blabla..
next_reduction = {}
next_reduction[0] = [0,0,0,0]
reg = []
min_temp = []
reduction_counter = 1
reduction_counter_2 = 1
while next_reduction != {}:
reduction_counter_2 = reduction_counter*reduction_counter
next_reduction.clear()
for shadow in minterms_dict:
min_temp.append(shadow)
for a in minterms_dict:
min_temp.pop(0)
for b in min_temp:
j=0
for c in bit_level:
if minterms_dict[a][c] == minterms_dict[b][c]:
reg.insert(0,minterms_dict[a][c])
else:
j = j+1
if j==1:
reg.insert(0,"-")
else:
break
d = 0
g = 0
i = 0
for e in reg:
h = 0

for f in reg:
if reg[i] == reg[h] and reg[h] == "-":
g = g+1
h = h+1
i = i+1
i = 0
if g == reduction_counter_2 and len(reg) == bit_length:
next_reduction[a,b] = reg
reg = []
else:
reg = []
g = 0
if next_reduction == {}:
break
else:
minterms_dict.clear()
minterms_dict = deepcopy(next_reduction)
reduction_counter = reduction_counter + 1
##END
##MISSING DECIMAL - malalaman nya kung aling decimal ung di namarkan sa final ta
bulation
keys = minterms_dict.keys()
keys_list = word_tokenize(str(keys))
add_term = []
for a in minterms_copy:
if str(a) not in keys_list:
add_term.append(a)
for a in add_term:
element = a
while element !=0:
r = element/2
s = element%2
element = r
binary.insert(0,s)
while len(binary)<bit_length:
binary.insert(0,0)
minterms_dict[a] = binary
binary = []
##END
print '\nFinal Reduction: '
for a in minterms_dict:
print str(a)+" = "+ str(minterms_dict[a])
##VARIABLES - ginagawa nyang variable ung final reduction table, at missing deci
mal
##halimbawa 4 bit length, so w,x,y,z where w is MSB... hal 7 bit length,
##so t,u,v,w,x,y,z where t is MSB..
variable = []
term = []
boolean_expression = []
alphabet = ['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j',
'i','h','g','f','e','d','c','b','a']
for a in bit_level:

variable.append(alphabet[a])
for a in minterms_dict:
c = 0
for b in bit_level:
if minterms_dict[a][b] != '-':
if minterms_dict[a][b] == 1:
term.insert(0,str(variable[bit_level[c]]))
else:
term.insert(0,str(variable[bit_level[c]])+"'")
c = c+1
boolean_expression.append(term)
term = []
##END
##ELIMINATE REPEATING - tatanggalin nya sa array ung umuulet na term...
##halimbawa, may xyz na, tas may xyz ulet, tas meron ulet... isa lang ititira ny
a
print "\n"
pop_count = 0
boolean_shadow = []
final_boolean_expression = []
for shadow in boolean_expression:
boolean_shadow.append(shadow)
final_boolean_expression.append(shadow)
for a in boolean_expression:
boolean_shadow.pop(0)
for b in boolean_shadow:
if a == b:
final_boolean_expression.pop(pop_count)
pop_count = pop_count - 1
break
pop_count = pop_count + 1
##END
##SOP - auko ng POS </3... si bestfriend kasee </3
super_final_boolean_expression = ''
for a in range(0,len(final_boolean_expression)):
for b in range(0,len(final_boolean_expression[a])):
super_final_boolean_expression = super_final_boolean_expression + str(fi
nal_boolean_expression[a][b])
if a != len(final_boolean_expression)-1:
super_final_boolean_expression = super_final_boolean_expression + '+'
print 'Boolean Expression: '
print super_final_boolean_expression
##END
## REFERENCES: walang tungkol sa mcclaouwzkiiie dyan.. sorry
## https://www.youtube.com/watch?v=19EfbO5D_8s
## http://stackoverflow.com/questions/8771808/copy-a-dictionary-into-a-new-varia
ble-without-maintaining-the-link-with-previous
## If you wanna learn more about python, i recommend:
## TREVOR PAYNE:
## https://www.youtube.com/user/TPayneExperience
## SENTDEX:
## https://www.youtube.com/user/sentdex

Você também pode gostar