Some refactoring.

This commit is contained in:
Rhinemann 2023-09-27 23:02:49 +03:00
parent e2ebe2d095
commit ec152c5758
2 changed files with 25 additions and 26 deletions

View File

@ -1,5 +1,5 @@
import copy
from collections import deque
from typing import Tuple, List, Any
from typing_extensions import Self
from lib.prettytable import PrettyTable
@ -236,12 +236,12 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * n))
rg2 = BasicRegister(first_term.memory)
rg3 = BasicRegister(second_term.memory)
rg2 = copy.copy(first_term)
rg3 = copy.copy(second_term)
ct = Counter(n)
data_table = [["iter", "RG1", "RG2", "RG3", "CT", "MicroOperations"]]
@ -278,11 +278,11 @@ def binary_multiplication_method_2(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * (2*n)))
rg2 = BasicRegister(first_term.memory)
rg2 = copy.copy(first_term)
rg3 = BasicRegister(deque([False] * n + list(second_term.memory)))
i = 0
@ -317,7 +317,7 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
data_table = [["iter", "RG2", "RG1", "RG3", "CT", "MicroOperations"]]
@ -361,11 +361,11 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * (2*n+1)))
rg2 = BasicRegister(first_term.memory)
rg2 = copy.copy(first_term)
rg3 = BasicRegister(deque([False]) + second_term.memory + deque([False] * n))
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
@ -389,6 +389,7 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
return BasicRegister(deque(list(rg1.memory)[:-1])), data_table
def binary_division_method_1(first_term: BasicRegister, second_term: BasicRegister) \
-> tuple[BasicRegister, list[list[str]]]:
"""
@ -401,7 +402,6 @@ def binary_division_method_1(first_term: BasicRegister, second_term: BasicRegist
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False, False]) + second_term.memory)
@ -431,6 +431,7 @@ def binary_division_method_1(first_term: BasicRegister, second_term: BasicRegist
return BasicRegister(deque(list(rg3.memory)[1:])), data_table
def binary_division_method_2(first_term: BasicRegister, second_term: BasicRegister) \
-> tuple[BasicRegister, list[list[str]]]:
"""
@ -443,7 +444,6 @@ def binary_division_method_2(first_term: BasicRegister, second_term: BasicRegist
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False]) + second_term.memory + deque([False]*n))

29
main.py
View File

@ -20,6 +20,7 @@ def process_command(symbol):
else:
print(f"Error: unexpected instruction '{symbol}', skipping")
def perform_operation(first_register: bu.BasicRegister, second_register: bu.BasicRegister):
global operation, method
@ -71,15 +72,15 @@ def perform_operation(first_register: bu.BasicRegister, second_register: bu.Basi
pass
def get_prompt_text(first_register: bu.BasicRegister, second_register: bu.BasicRegister) -> str:
global operation, method
def get_prompt_text(operation: any, method: any) -> str:
response = "({} {})"
if not operation:
return f"({first_register} {second_register})"
elif not method:
return f"({first_register} {second_register}) {operation}"
else:
return f"({first_register} {second_register}) {operation}/{method}"
if operation:
response += " {}"
if operation and method:
response += "/{}"
return response
def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicRegister):
@ -91,17 +92,15 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg
print(second_register)
while True:
prompt_text: str = get_prompt_text(operation, method).format(first_register, second_register, operation, method)
print()
if operation == "":
raw_user_input = input(f"Choose the operation:\n" \
f"[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n" \
f"{get_prompt_text(first_register, second_register)} > ")
raw_user_input = input("Choose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, "
"[q]uit\n" + prompt_text + " > ")
elif operation == "m":
raw_user_input = input(f"Choose method to use (1-4):\n" \
f"{get_prompt_text(first_register, second_register)} > ")
raw_user_input = input("Choose method to use (1-4):\n" + prompt_text + " > ")
elif operation == "d":
raw_user_input = input(f"Choose method to use (1-2):\n" \
f"{get_prompt_text(first_register, second_register)} > ")
raw_user_input = input("Choose method to use (1-2):\n" + prompt_text + " > ")
user_input = list(raw_user_input)