-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
66 lines (53 loc) · 1.71 KB
/
calculator.py
File metadata and controls
66 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
def limpar_terminal():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
def inserir_binarios():
numero_str1 = str(input("Digite o primeiro binário: "))
numero_str2 = str(input("Digite o segundo binário: "))
print("")
return numero_str1, numero_str2
def igualar_tamanhos(numero_str1, numero_str2):
tamanho_maximo = max(len(numero_str1), len(numero_str2))
numero_str1_ajustado = numero_str1.zfill(tamanho_maximo)
numero_str2_ajustado = numero_str2.zfill(tamanho_maximo)
return numero_str1_ajustado, numero_str2_ajustado
def somar_binarios(binary1, binary2):
carry = 0
resultado = []
for i in range(len(binary1) -1, -1, -1):
soma = int(binary1[i]) + int(binary2[i]) + carry
if soma == 2:
soma = 0
carry = 1
elif soma == 3:
soma = 1
carry = 1
elif soma == 0:
soma = 0
carry = 0
elif soma == 1:
soma = 1
carry = 0
resultado.insert(0, soma)
if carry == 1:
resultado.insert(0, carry)
return resultado
def exibir_resultado(resultado):
print("".join(map(str, resultado)))
def main():
limpar_terminal()
numero_str1, numero_str2 = inserir_binarios()
binary1 = []
binary2 = []
numero_str1_ajustado, numero_str2_ajustado = igualar_tamanhos(numero_str1, numero_str2)
for i in range(len(numero_str1_ajustado)):
digito1 = numero_str1_ajustado[i]
binary1.append(digito1)
for i in range(len(numero_str2_ajustado)):
digito2 = numero_str2_ajustado[i]
binary2.append(digito2)
exibir_resultado(somar_binarios(binary1, binary2))
main()