Nomenclature fix

This commit is contained in:
TiagoRG 2022-11-17 12:08:42 +00:00
parent dfcf80c183
commit 753c7f28e6
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
4 changed files with 41 additions and 41 deletions

View File

@ -1,15 +0,0 @@
# Karnaugh Map Builder
### Script to build the karnaugh map for a given function.
---
## Installing
#### 1. Head over to [Release](https://github.com/TiagoRG/uaveiro-leci/releases/tag/kmb-v1) and download the python file.
#### 2. Save it on any directory and then open the directory.
#### 3. Inside the directory, right click and choose "Open in terminal"
![- Unable to load image -](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/karnaugh-map-builder/openInTerminal.png)
#### 4. Run the following command:
`python3 karnaugh.py bash`
#### 5. Relaunch the terminal and you will have the `karnaugh` command
---
#### Use `karnaugh usage` to know better how to use this tool.
#### If you want to change the location of the `karnaugh.py` file, move it to another directory, open that directory in terminal and run
`python3 karnaugh.py bash --reset`

View File

@ -0,0 +1,15 @@
# Truth Table Builder
### Script to build the truth table for a given function.
---
## Installing
#### 1. Head over to [Release](https://github.com/TiagoRG/uaveiro-leci/releases/tag/ttb) and download the python file.
#### 2. Save it on any directory and then open the directory.
#### 3. Inside the directory, right click and choose "Open in terminal"
![- Unable to load image -](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/truthtable/openInTerminal.png)
#### 4. Run the following command:
`python3 truthtable.py bash`
#### 5. Relaunch the terminal and you will have the `truthtable` command
---
#### Use `truthtable usage` to know better how to use this tool.
#### If you want to change the location of the `truthtable.py` file, move it to another directory, open that directory in terminal and run
`python3 truthtable.py bash --reset`

View File

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 47 KiB

View File

@ -8,19 +8,19 @@ def main():
if len(sys.argv) == 1 or sys.argv[1] == 'usage':
print("""Usage:
karnaugh '<function>':
Prints the karnaugh map of the function.
truthtable '<function>':
Prints the truth table map of the function.
Valid characters:
> Variables: a-z
> Operators: +, *, ~, (, ); You can't use ~ before ()
karnaugh bash:
Adds the command karnaugh to bash, so you can use it from anywhere.
truthtable bash:
Adds the command truthtable to bash, so you can use it from anywhere.
karnaugh bash --reset:
Resets the command karnaugh in bash in case it's broken, so you can use it from anywhere.
truthtable bash --reset:
Resets the command truthtable in bash in case it's broken, so you can use it from anywhere.
karnaugh usage:
truthtable usage:
Prints this message.""")
return
if sys.argv[1] == "bash":
@ -28,11 +28,11 @@ karnaugh usage:
if len(sys.argv) == 2:
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
for line in bashrc:
if line.startswith('alias karnaugh='):
print('Command already exists, try using, after opening a new terminal, karnaugh <function>')
if line.startswith('alias truthtable='):
print('Command already exists, try using, after opening a new terminal, truthtable \'<function>\'')
return
os.system(f'echo "\n\nalias karnaugh=\'python3 {pathlib.Path(__file__).parent.absolute()}/karnaugh.py\'\n" >> \'/home/{username}/.bashrc\'')
print('Command added to bash, try using karnaugh <function>.\nIf it doesn\'t work, do karnaugh bash --reset.')
os.system(f'echo "\n\nalias truthtable=\'python3 {pathlib.Path(__file__).parent.absolute()}/truthtable.py\'\n" >> \'/home/{username}/.bashrc\'')
print('Command added to bash, try using truthtable <function>.\nIf it doesn\'t work, do truthtable bash --reset.')
elif sys.argv[2] == '--reset':
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
lines = bashrc.readlines()
@ -42,16 +42,16 @@ karnaugh usage:
linesDict[index] = line
index += 1
for key in linesDict:
if linesDict[key].startswith('alias karnaugh='):
if linesDict[key].startswith('alias truthtable='):
del linesDict[key]
break
string = ''
for key in linesDict:
string += linesDict[key]
string += f'\n\nalias karnaugh=\'python3 {pathlib.Path(__file__).parent.absolute()}/karnaugh.py\'\n'
string += f'\n\nalias truthtable=\'python3 {pathlib.Path(__file__).parent.absolute()}/truthtable.py\'\n'
with open(f'/home/{username}/.bashrc', 'w') as bashrc:
bashrc.write(string)
print('Command readded to bash, try using, after opening a new terminal, karnaugh <function>.')
print('Command readded to bash, try using, after opening a new terminal, truthtable <function>.')
else:
function = sys.argv[1]
@ -60,19 +60,19 @@ karnaugh usage:
for char in function:
if not (re.match(validVariables, char) or char in validOperators):
print("Invalid function, use 'karnaugh usage' to see the valid characters.")
print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
variables = re.findall(validVariables, function)
variables = list(dict.fromkeys(variables))
variables.sort()
karnaughMap = getMap(variables, function)
printMap(variables, karnaughMap)
truthTable = getTable(variables, function)
printTable(variables, truthTable)
def getMap(variables, function):
karnaughMap = {}
def getTable(variables, function):
truthTable = {}
for n in range(2 ** len(variables)):
tempFunction = function
binary = bin(n)[2:]
@ -87,17 +87,17 @@ def getMap(variables, function):
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])])))+" ")
index += 1
try:
karnaughMap[binary] = eval(tempFunction)
truthTable[binary] = eval(tempFunction)
except:
print("Invalid function, use 'karnaugh usage' to see the valid characters.")
print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
if karnaughMap[binary] > 1:
karnaughMap[binary] = 1
if truthTable[binary] > 1:
truthTable[binary] = 1
return karnaughMap
return truthTable
def printMap(variables, karnaughMap):
def printTable(variables, truthTable):
for var in variables:
print("|---", end='')
print('|-----|')
@ -107,7 +107,7 @@ def printMap(variables, karnaughMap):
for var in variables:
print("|---", end='')
print('|-----|')
for key, value in karnaughMap.items():
for key, value in truthTable.items():
for char in key:
print("| " + char, end=' ')
print(f"| {value} |")