栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用python实现主范式的转换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用python实现主范式的转换

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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号