Fixed a bug that didn't allow you to negate variables used multiple times

This commit is contained in:
TiagoRG 2023-02-22 12:01:24 +00:00
parent 2d83e2c862
commit 96f27e2b01
1 changed files with 10 additions and 4 deletions

View File

@ -47,10 +47,14 @@ def getTable(variables, function):
if index == len(tempFunction): if index == len(tempFunction):
break break
if var in "01" and tempFunction[index - 1] == '~': if var in "01" and tempFunction[index - 1] == '~':
tempFunction = tempFunction.replace('~' + var, str(1 - int(var))) tempFunction = tempFunction.replace('~' + var, str(1 - int(var) + " "))
if var in variables: try:
tempFunction = tempFunction.replace(tempFunction[index], binary[variables.index(var)]) if tempFunction[index - 1] != '~' else tempFunction.replace(f"~{tempFunction[index]}", str(int(not int(binary[variables.index(tempFunction[index])])))+" ") if var in variables:
index += 1 tempFunction = tempFunction[:index] + binary[variables.index(var)] + tempFunction[index+1:] if tempFunction[index - 1] != '~' else tempFunction[:index-1] + str(int(not bool(int(binary[variables.index(tempFunction[index])]))))+" " + tempFunction[index+1:]
index += 1
except ValueError:
index += 1
continue
try: try:
truthTable[binary] = eval(tempFunction) truthTable[binary] = eval(tempFunction)
except: except:
@ -58,6 +62,8 @@ def getTable(variables, function):
return return
if truthTable[binary] > 1: if truthTable[binary] > 1:
truthTable[binary] = 1 truthTable[binary] = 1
if truthTable[binary] < 0:
truthTable[binary] = 0
return truthTable return truthTable