Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
Rhinemann | cb19425b8a | |
dymik739 | c0aebf14d2 | |
rhinemann | e6ba434796 | |
rhinemann | dbe283a3d6 |
16
README.md
16
README.md
|
@ -7,14 +7,18 @@ This is a Python language prototype for a binary calculator to be used in Comput
|
|||
The user must have installed:
|
||||
|
||||
- python 3 (for the calculator itself);
|
||||
- git (to clone the repository for installation);
|
||||
|
||||
## Installation
|
||||
To install the calculator just clone the repository locally:
|
||||
|
||||
### Cloning the repository
|
||||
To install the experimental version of the calculator just clone the repository locally:
|
||||
```
|
||||
git clone -b master http://139.162.162.130:3000/Rhinemann/binaryCalculatorPrototype.git
|
||||
```
|
||||
|
||||
### Stable packages
|
||||
To install a stable release of the calculator download the archive from [releases](http://139.162.162.130:3000/Rhinemann/binaryCalculatorPrototype/releases) page and decompress it in a directory of your choice.
|
||||
|
||||
## User instructions
|
||||
Start the calculator using the following command:
|
||||
```
|
||||
|
@ -262,14 +266,18 @@ So that multiple operations are performed on the same operands without the need
|
|||
Користувач мусить мати:
|
||||
|
||||
- python 3 (для роботи калькулятора);
|
||||
- git (щоб встановити калькулятор);
|
||||
|
||||
## Завантаження
|
||||
Щоб встановити калькулятор пропишіть цю команду:
|
||||
|
||||
### Клонування репозиторію
|
||||
Щоб встановити експериментальну версію калькулятора пропишіть цю команду, щоб клонувати репозиторій:
|
||||
```
|
||||
git clone -b master http://139.162.162.130:3000/Rhinemann/binaryCalculatorPrototype.git
|
||||
```
|
||||
|
||||
### Стабільні версії
|
||||
Щоб встановити стабільний реліз калькулятора завантажте архів зі сторінки [релізів](http://139.162.162.130:3000/Rhinemann/binaryCalculatorPrototype/releases) та розархівуйте в обраній директорії.
|
||||
|
||||
## Інструкція користувачам
|
||||
Калькулятор запускається цією командою:
|
||||
```
|
||||
|
|
|
@ -100,6 +100,18 @@ class Counter(BasicRegister):
|
|||
return any(self.memory)
|
||||
|
||||
|
||||
def negated(memory: deque[bool]) -> deque[bool]:
|
||||
"""
|
||||
Returns negated memory chunk.
|
||||
|
||||
:param deque[bool] memory: Memory chunk to be negated.
|
||||
|
||||
:return: Negated memory chunk.
|
||||
:rtype: deque[bool]
|
||||
"""
|
||||
return deque([not value for value in memory])
|
||||
|
||||
|
||||
def get_memory(variable_name: str) -> deque[bool]:
|
||||
"""
|
||||
Reads user input to be used as a memory array.
|
||||
|
@ -472,3 +484,68 @@ def binary_division_method_2(first_term: BasicRegister, second_term: BasicRegist
|
|||
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, f"RG3 := l(RG3).SM[p]\nRG1 := 0.r(RG1)"])))
|
||||
|
||||
return BasicRegister(deque(list(rg3.memory)[1:])), data_table
|
||||
|
||||
def binary_square_root(first_term: BasicRegister) \
|
||||
-> tuple[BasicRegister, list[list[str]]]:
|
||||
"""
|
||||
Extracts the square root of first term.
|
||||
|
||||
:param: BasicRegister first_term: Register for square root extraction.
|
||||
|
||||
:return: Register containing the division result and the state table of a virtual
|
||||
device that extracted the square root of the given term.
|
||||
:rtype: tuple[BasicRegister, list[list[str]]]
|
||||
"""
|
||||
|
||||
n: int = len(first_term)
|
||||
i = 0
|
||||
|
||||
rga = BasicRegister(deque([False]*n))
|
||||
rgb = BasicRegister(deque([False]*(n+2)))
|
||||
rgc = BasicRegister(first_term.memory)
|
||||
ct = Counter(n)
|
||||
|
||||
data_table = [["iter", "RGA", "RGB", "RGC", "CT", "MicroOperations"]]
|
||||
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "-"])))
|
||||
|
||||
# two initial shifts
|
||||
rgb.left_shift(rgc.memory[0])
|
||||
rgc.left_shift()
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := l(RGB).RGC[0]\nRGC := l(RGC).0"])))
|
||||
|
||||
rgb.left_shift(rgc.memory[0])
|
||||
rgc.left_shift()
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := l(RGB).RGC[0]\nRGC := l(RGC).0"])))
|
||||
|
||||
# initial inverted addition
|
||||
rgb = binary_sum(rgb, BasicRegister(negated(rga.memory) + deque([True, True])))
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := RGB + !(RGA).11"])))
|
||||
|
||||
while ct.non_zero():
|
||||
data_table.append([])
|
||||
i += 1
|
||||
|
||||
rga.left_shift(not rgb.memory[0])
|
||||
rgb.left_shift(rgc.memory[0])
|
||||
rgc.left_shift()
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGA := l(RGA).!(RGB[0])\nRGB := l(RGB).RGC[0]\nRGC := l(RGC).0"])))
|
||||
|
||||
rgb.left_shift(rgc.memory[0])
|
||||
rgc.left_shift()
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := l(RGB).RGC[0]\nRGC := l(RGC).0"])))
|
||||
|
||||
if rgb.memory[0]:
|
||||
rgb = binary_sum(rgb, BasicRegister(rga.memory + deque([True, True])))
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := RGB + RGA.11"])))
|
||||
else:
|
||||
rgb = binary_sum(rgb, BasicRegister(negated(rga.memory) + deque([True, True])))
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "RGB := RGB + !(RGA).11"])))
|
||||
|
||||
ct.decrement()
|
||||
data_table[-1].append(list(map(str, [i, rga, rgb, rgc, ct, "CT := CT - 1"])))
|
||||
|
||||
return rga, data_table
|
||||
|
|
10
main.py
10
main.py
|
@ -9,7 +9,7 @@ user_input = []
|
|||
def process_command(symbol):
|
||||
global operation, method
|
||||
|
||||
if symbol.lower() in "asmdq":
|
||||
if symbol.lower() in "asmdrq":
|
||||
operation = symbol
|
||||
elif operation == "m" and symbol in "1234":
|
||||
method = symbol
|
||||
|
@ -65,6 +65,10 @@ def perform_operation(first_register: bu.BasicRegister, second_register: bu.Basi
|
|||
operation, method = "", ""
|
||||
case _:
|
||||
pass
|
||||
case "r":
|
||||
result, data_table = bu.binary_square_root(first_register)
|
||||
print(f"\nSquare root:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
|
||||
operation, method = "", ""
|
||||
case "q":
|
||||
exit(0)
|
||||
|
||||
|
@ -93,10 +97,10 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg
|
|||
|
||||
while True:
|
||||
prompt_text: str = get_prompt_text(operation, method).format(first_register, second_register, operation, method)
|
||||
print()
|
||||
# print()
|
||||
if operation == "":
|
||||
raw_user_input = input("Choose the operation:\n"
|
||||
"[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n" +
|
||||
"[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [r]oot, [q]uit\n" +
|
||||
prompt_text + " > ")
|
||||
elif operation == "m":
|
||||
raw_user_input = input("Choose method to use (1-4):\n" + prompt_text + " > ")
|
||||
|
|
Loading…
Reference in New Issue