编写一个Python程序,将标准SOP(积之和,Sum of Products)形式转换为标准POS(和之积,Product of Sums)形式。
假设:
输入的SOP表达式已经是标准形式。表达式中的变量是连续的,即如果表达式包含变量A,那么它将依次拥有变量B、C等,并且每一个乘积项中的字母都按字母顺序排列(例如ABC,而不是BAC)。
示例:
输入 : ABC‘+A‘BC+ABC+AB‘C
输出 : (A+B+C).(A+B+C‘).(A+B‘+C).(A‘+B+C)
输入 : A‘B+AB‘
输出 : (A+B).(A‘+B‘)
解决思路:
- 首先,将每个乘积项转换为等效的二进制形式(例如:如果是ABC‘,则非补变量(A, B)取1,补变量(C)取0,因此二进制转换为110),最后将其转换为十进制形式(例如:110 = 6),并存储在一个列表中。
- 现在,为了得到POS形式,我们要选取所有不在第一步列表中出现的项,然后将每个项转换为二进制,进而转换为SOP形式。例如:假设5不在列表中,那么
5 ==> 101 (二进制)
现在,将1替换为补变量(A, C)
将0替换为非补变量(B)
并在变量之间使用 ‘+‘
101 ==> A‘+B+C‘
在每个单独的和项之后使用 ‘.‘
为了更清晰,在每个单独的项之间使用括号。
例如: (A‘+B+C‘).(A+B+C‘)
Python 代码
# Python code to convert standard SOP form
# to standard POS form
# function to calculate no. of variables
# used in SOP expression
def count_no_alphabets(SOP):
i = 0
no_var = 0
# As expression is standard so total no.
# of alphabets will be equal
# to alphabets before first ‘+‘ character
while (SOP[i]!=‘+‘):
# checking if character is alphabet
if (SOP[i].isalpha()):
no_var+= 1
i+= 1
return no_var
# function to calculate the min terms in integers
def Cal_Min_terms(Min_terms, SOP):
a =""
i = 0
while (i<len(SOP)):
if (SOP[i]=='+'):
# converting binary to decimal
b = int(a, 2)
# insertion of each min term(integer) into the list
Min_terms.append(b)
# empty the string
a =""
i+= 1
else:
# checking whether variable is complemented or not
if(i + 1 != len(SOP) and SOP[i + 1]=="'"):
# concatenating the string with '0'
a+='0'
# incrementing by 2 because 1 for alphabet and
# another for "'"
i+= 2
else:
# concatenating the string with '1'
a+='1'
i+= 1
# insertion of last min term(integer) into the list
Min_terms.append(int(a, 2))
# function to calculate the max terms in binary then
# calculate POS form of SOP
def Cal_Max_terms(Min_terms, no_var, start_alphabet):
# declaration of the list
Max_terms =[]
# calculation of total no. of terms that can be
# formed by no_var variables
max = 2**no_var
for i in range(0, max):
# checking whether the term is not
# present in the min terms
if (Min_terms.count(i)== 0):
# converting integer to binary and then
# taking the value from 2nd index as 1st
# two index contains '0b'
b = bin(i)[2:]
# loop used for inserting 0's before the
# binary value so that its length will be
# equal to no. of variables present in
# each product term
while(len(b)!= no_var):
b ='0'+b
# appending the max terms(integer) in the list
Max_terms.append(b)
POS =""
# loop till there are max terms
for i in Max_terms:
# before every sum term append POS by '('
POS = POS+"("
# acquire the starting variable came from
# main function in every sum term
value = start_alphabet
# loop till there are 0's or 1's in each max term
for j in i:
# checking for complement variable to be used
if (j =='1'):
# concatenating value, ' and + in string POS
POS = POS + value+"'+"
# checking for uncomplement variable to be used
else:
# concatenating value and + in string POS
POS = POS + value+"+"
# increment the alphabet by 1
value = chr(ord(value)+1)
# for discarding the extra '+' in the last
POS = POS[:-1]
# appending the POS string by ')'
POS = POS+")"
return POS
# Driver Code
SOP ="ABC'+A'BC+ABC+AB'C"
Min_terms =[]
# to count no. of variables in the SOP expression
no_var = count_no_alphabets(SOP)
# function to calculate min terms(integer) from the SOP expression
Cal_Min_terms(Min_terms, SOP)
# we are taking A as a start_alphabet, we can ignore
# this by passing this value through function
start_alphabet = 'A'
# conversion of SOP form to POS form
POS = Cal_Max_terms(Min_terms, no_var, start_alphabet)
print(POS)
输出:
(A+B+C).(A+B+C‘).(A+B‘+C).(A‘+B+C)