forked from nguyenktrong/thuat-toan-attt-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.2 sum-subtract.py
68 lines (66 loc) · 1.65 KB
/
1.2 sum-subtract.py
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
67
68
# Thuật toán cộng - trừ chính xác bội
from math import *
def convert_array(a,w,t):
A = []
for i in range(t):
A.append(a//(2**((t-i-1)*w)))
a = a % (2**((t-i-1)*w))
return A
def sum(a,b,w,t):
C = []
e = 0
for i in range(t-1,-1,-1):
c = A[i] + B[i] + e
if 0 <= c < 2**w:
e = 0
else:
e = 1
C.append(c%(2**w))
return e,C[::-1]
def subtraction(a,b,w,t):
C = []
e = 0
for i in range(t-1,-1,-1):
c = A[i] - B[i] - e
if 0 <= c < 2**w:
e = 0
else:
e = 1
C.append(c%(2**w))
return e,C[::-1]
# phép cộng trừ số nguyên lớn
w = int(input("Nhap W = "))
p = 2147483647
m = ceil(log(p,2))
t = ceil(m/w)
print("Choose 1 to sum")
print("Choose 2 to subtract")
key = input("Enter: ")
if key == "1":
print("Choose a to sum two arrays")
print("Choose b to sum two numbers")
newkey = input("Enter: ")
if newkey == "a":
A = [int(a) for a in input("Nhap A = ").split()]
B = [int(b) for b in input("Nhap B = ").split()]
print(sum(A,B,w,t))
elif newkey == "b":
a = int(input("Nhap a = "))
b = int(input("Nhap b = "))
A = convert_array(a,w,t)
B = convert_array(b,w,t)
print(sum(A,B,w,t))
elif key == "2":
print("Choose a to subtract two arrays")
print("Choose b to subtract two numbers")
newkey = input("Enter: ")
if newkey == "a":
A = [int(a) for a in input("Nhap A = ").split()]
B = [int(b) for b in input("Nhap B = ").split()]
print(subtraction(A,B,w,t))
elif newkey == "b":
a = int(input("Nhap a = "))
b = int(input("Nhap b = "))
A = convert_array(a,w,t)
B = convert_array(b,w,t)
print(subtraction(A,B,w,t))