python應用之求主析取范式,主合取范式


任意輸入一個命題公式,計算並輸出其真值表以及主析取范式,主合取范式

思路:大概就是將蘊含,等價,異或進行轉化,然后使用eval()計算

#! /usr/bin/env python3
# -*- coding:utf-8 -*-

sInput = '' #輸入的命題公式字符串
sParse = '' #化簡后的sInput
variable = [] #保存公式中的變量
ornl = [] #主析取范式最小項
andnl = [] #主合取范式最大項
fore = '' #符號前面的部分
back = '' #符號后面的部分

def myinput():
	global sInput
	print("請輸入一個任意命題公式(原子命題用字母表示,'~'表示非 '&'表示合取 '|'表示析取 '>'表示蘊含 ':'表示等價 '@'表示異或,可用括號'()'):")
	sInput = input()

def getVariale():
    global sInput,variable
    for c in sInput:
        if c >= 'A' and c <= 'Z' or c >= 'a' and c <= 'z' :
            if c not in variable:
                variable.append(c)
        elif c!='~' and c!='&' and c!='|' and c!='(' and c!=')' and c!='>' and c!=':' and c!='@':
            print('輸入有誤!!')
    variable = sorted(variable)

def getFB(c):
    global sInput,sParse,fore,back
    slen = len(sParse)
    for i in range(0,slen):  #遍歷sParse中所有字符
        if sParse[i] is c:
            if sParse[i-1] is not ')': #找到fore
                fore = sParse[i-1]
            else:
                flag = 1
                j = i-2
                while flag is not 0:
                    if sParse[j] is '~':
                        j-=1
                    if sParse[j] is '(':
                        flag-=1
                    if sParse[j] is ')':
                        flag+=1
                    j-=1
                fore = sParse[j+1:i]
            if sParse[i+1] is not '(': #找到back
                back = sParse[i+1]
            else:
                flag = 1
                j = i+2
                while flag is not 0:
                    if sParse[j] is '~':
                        j+=1
                    if sParse[j] is ')':
                        flag-=1
                    if sParse[j] is '(':
                        flag+=1
                    j+=1
                back = sParse[i+1:j]
            if c is '>':
                sParse = sParse.replace(fore+'>'+back,'('+'~'+fore+'|'+back+')')
            elif c is ':':
                sParse = sParse.replace(fore+':'+back,'('+fore+'&'+back+')|(~'+fore+'&~'+back+')')
            elif c is '@':
                sParse = sParse.replace(fore+'@'+back,'~('+'('+fore+'&'+back+')|(~'+fore+'&~'+back+')'+')')

def parseInput():
    global sInput,sParse
    sParse = sInput
    getFB('>')
    getFB(':')
    getFB('@')

def cal():
    global sInput,sParse,variable,ornl,andnl,orResult,andResult
    vlen = len(variable) #變量個數
    n = 2**vlen   #所有情況個數
    print('真值表如下:')
    print(variable,sInput+'即',sParse)
    for nl in range(0,n):      #獲取真值表
        value = []    #數值
        j = nl   #真值表當前行
        for i in range(0,vlen):
            value.append(0)
        i = 0
        while j!=0:
            value[i]=j%2
            j=j//2
            i+=1
        value.reverse()
        value = list(map(str,value))
        s = sParse
        for x in range(0,vlen):
            s = s.replace(variable[x],value[x])
        result = eval(s)&1
        if result is 1:
            ornl.append(nl)
        else:
            andnl.append(nl)
        print(value,result)

def outprint():
    print('主析取范式:')
    print('∑',ornl,sep='')
    print('主合取范式:')
    print('∏',andnl,sep='')

def main():
    myinput()
    getVariale()
    parseInput()
    cal()
    outprint()

if __name__=='__main__':
	main()

運行結果:

result


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM