0

这是我的代码:

from sympy import symbols, Equivalent
from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z


def eq(a, b):
    return fnc(Equivalent(a, b), True)

inr = str(input('Enter formula: '))
x = ' '
for i in inr:
     #print(inr[inr.index(i-2)])
     if i == '>':
         i = '>>'
         x += i
     elif i == '=':

         print(i)
         i = 'Equivalent'
         x += i

     else:
         x += i

print(fnc(x, True))

而不是input =(a,b)打电话Equivalent function我想输入(a=b)。我怎样才能做到这一点?我试图控制元素之前和下一个,=并在您输入(a = b)时将其添加到Equivalent功能中,输出应该是(a|~b)&(b|~a)但它不起作用。

i= sympy.Equivalent(symbols((inr[inr.index(i) - 1])), symbols(inr[inr.index(i) + 1]))
4

1 回答 1

0

因此,根据您的示例 I/O,它是:

IN:所以当您输入 (a=b) 时,输出应该是什么?– 用户5173426

OUT : 应该是 (a | ~b) & (b | ~a) – Yacine Benatia

我对您现有的代码进行了一些更改:

from sympy import symbols, Equivalent
from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z


def eq(a, b):
    return fnc(Equivalent(a, b), True)

inr = str(input('Enter formula: '))
x = ' '
# get the part of the formula/string before =
y = inr.rsplit('=', 1)[0]
# get the part of the formula/string after =
z = inr.rsplit('=', 1)[1]
for i in inr:
     if i == '>':
         i = '>>'
         x += i
     elif i == '=':
         i = 'Equivalent'
         x = i
     elif i != ')':
         x += y
         x += ','
         x +=  z
     # print(x)

print(fnc(x, True))

输出:

出去

于 2018-12-19T14:31:40.593 回答