将公式转换为cnf python

时间:2018-12-15 17:43:05

标签: python python-3.x logical-operators boolean-logic cnf

我想将公式转换为CNF。有图书馆可以这样做吗?这是我的代码。我创建了许多函数来将任何(a或b)转换为CNF格式。

但是,如果有很多命题,那么很难做到(a> b)&(c&d),或者不是(f)..

operator="&|>=~"

def isOperand(c):
    return c >= 'a' and c <= 'z'

operators ="&|>=~"
def isOperator(c): #it cheak if the the given c is on the operators
 return c in operators
def dblimplique(a):
    if(a=='a = b' ):
       a='(a > b) & (b > a)'
    return a
def limplique(a):
    if(a=='a > b' ):
       a='~a | b'
    return a
def nonAouB(a):
    if(a=='~(a | b)' ):
       a='~a & ~b'
    return a
def nonAetB(a):
    if (a == '~(a & b)'):
        a = '~a | ~b'
    return a
def doublenon(a):
    if (a == '~~a'):
        a = 'a'
    return a
def doublenon(a):
    if (a == '~~a'):
        a = 'a'
    return a
def AetBouc(a):
    if (a == 'a & (b | c)'):
        a = '(a & b) | (a & c)'
    return a


def AouBetc(a):
    if (a == 'a | (b & c)'):
        a = '(a | b) & (a | c)'
    return a


def FNC():
    input_string = input("Entrer votre formule:")
    if(input_string == 'a = b'):
           return dblimplique(input_string)

    elif(input_string=='a > b'):
            return limplique(input_string)

    elif(input_string=='~(a | b)'):
            return  nonAouB(input_string)

    elif(input_string == '~(a & b)'):
            return nonAetB(input_string)

    elif(input_string =='~~a'):
            return  doublenon(input_string)
    elif(input_string =='a & (b | c)'):
        return AetBouc(input_string)
    elif(input_string=='a | (b & c)'):
        return AouBetc(input_string)
    elif(input_string=='a | b' or 'a | b | c' or 'a | b | c | d' or 'a | b | c | d'):
        return input_string
    else:
      for i in input_string:
          if(i == 'a > b' or 'c > d' or 'e > f'):
              return limplique(i)


print(FNC())

1 个答案:

答案 0 :(得分:0)

使用此模块<option value="">YY</option>

<option>YY</option>

这是一个很好的库:https://docs.sympy.org/latest/modules/logic.html

这是两个代码: https://github.com/ldkrsi/cnf_py

https://github.com/omkarkarande/CNF-Converter