用 Python 編寫一個程序,將標準 POS(sum of products)形式轉換為標準 SOP(sum of products)形式。
假設:輸入的 POS 表達式是標準的。 POS 表達式中的變量是連續的,即如果表達式包含變量 A,那麽它將分別具有變量 B、C,並且每個 Sum 項包含按排序順序排列的字母,即 A + B + C(不像 B+A+C)。
例子:
Input: (A + B + C).(A + B + C').(A + B' + C).(A' + B + C) Output: A'BC + AB'C + ABC' + ABC Input: (A + B).(A' + B') Output: A'B + AB'
方法:
- 首先將每個總和項轉換為其等效的二進製形式。例如,如果 (A+B+C') 則取 0 表示非補變量 (A, B) 取 1 表示補變量 (C) 所以二進製轉換為 011) 最後等價於其十進製形式(例如: 001 = 1) 並存儲在列表中。
- 現在對於 SOP 形式,取所有在步驟 1 中形成的列表中不存在的術語,然後將每個術語轉換為二進製,從而更改為 POS 形式。例如 -
假設 4 不在列表中,那麽 5==> 101(二進製)
現在,用補碼變量替換 0(B)
用非補變量(A,C)替換 1
101 ==> AB'C
在每個單獨的總和項之後使用“+”
例如:AB'C+AB'C'
以下是上述方法的 Python 實現:
# Python code to convert standard POS form
# to standard SOP form
# Function to calculate no. of variables
# used in POS expression
def count_no_alphabets(POS):
i = 0
no_var = 0
# As expression is standard so total no.
# of alphabets will be equal
# to alphabets before first '.' character
while (POS[i]!='.'):
# checking if character is alphabet
if (POS[i].isalpha()):
no_var+= 1
i+= 1
return no_var
# Function to calculate the max terms in integers
def Cal_Max_terms(Max_terms, POS):
a = ""
i = 0
while (i<len(POS)):
if (POS[i]=='.'):
# converting binary to decimal
b = int(a, 2)
# insertion of each min term(integer) into the list
Max_terms.append(b)
# empty the string
a =""
i+= 1
elif(POS[i].isalpha()):
# checking whether variable is complemented or not
if(i + 1 != len(POS) and POS[i + 1]=="'"):
# concatenating the string with '0'
a += '1'
# incrementing by 2 because 1 for alphabet and
# another for "'"
i += 2
else:
# concatenating the string with '1'
a += '0'
i += 1
else:
i+= 1
# insertion of last min term(integer) into the list
Max_terms.append(int(a, 2))
# Function to calculate the min terms in binary then
# calculate SOP form of POS
def Cal_Min_terms(Max_terms, no_var, start_alphabet):
# declaration of the list
Min_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 max terms
if (Max_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
Min_terms.append(b)
SOP = ""
# loop till there are min terms
for i in Min_terms:
# acquire the starting variable came from
# main function in every product term
value = start_alphabet
# loop till there are 0's or 1's in each min term
for j in i:
# checking for complement variable to be used
if (j =='0'):
# concatenating value, ' and + in string POS
SOP = SOP + value+"'"
# checking for uncomplement variable to be used
else:
# concatenating value and + in string POS
SOP = SOP + value
# increment the alphabet by 1
value = chr(ord(value)+1)
# appending the SOP string by '+" after
# every product term
SOP = SOP+ "+"
# for discarding the extra '+' in the last
SOP = SOP[:-1]
return SOP
# main function
def main():
# input1
POS_expr ="(A+B+C).(A+B+C').(A+B'+C).(A'+B + C)"
Max_terms = []
no_var = count_no_alphabets(POS_expr)
Cal_Max_terms(Max_terms, POS_expr)
SOP_expr = Cal_Min_terms(Max_terms, no_var, POS_expr[1])
print("Standard SOP form of " + POS_expr + " ==> " + SOP_expr)
# input2
POS_expr ="(A + B).(A'+B')"
Max_terms = []
no_var = count_no_alphabets(POS_expr)
Cal_Max_terms(Max_terms, POS_expr)
SOP_expr = Cal_Min_terms(Max_terms, no_var, POS_expr[1])
print ("Standard SOP form of " + POS_expr + " ==> " + SOP_expr)
# Driver code
if __name__=="__main__":
main()
輸出:
Standard SOP form of (A+B+C).(A+B+C').(A+B'+C).(A'+B + C) ==> A'BC+AB'C+ABC'+ABC Standard SOP form of (A + B).(A'+B') ==> A'B+AB'
相關用法
- Python turtle.pos()用法及代碼示例
- Python XML轉Dictionary用法及代碼示例
- Python list轉string用法及代碼示例
- Python Celsius轉Fahrenheit用法及代碼示例
注:本文由純淨天空篩選整理自AashutoshChauhan大神的英文原創作品 Python program to convert POS to SOP。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。