实现任意长的整数进行加法运算的演示程序
时间: 2023-05-13 22:05:03 浏览: 81
以下是一个实现任意长整数加法的演示程序:
```python
def add(num1, num2):
# 将两个数字转换为字符串并反转
num1, num2 = str(num1)[::-1], str(num2)[::-1]
# 确定两个数字中较长的那个
if len(num1) < len(num2):
num1, num2 = num2, num1
# 补齐较短的数字
num2 += '0' * (len(num1) - len(num2))
# 进位标志
carry = 0
# 存储结果的列表
result = []
# 逐位相加
for i in range(len(num1)):
# 将字符转换为数字
n1, n2 = int(num1[i]), int(num2[i])
# 计算当前位的和
s = n1 + n2 + carry
# 判断是否需要进位
if s >= 10:
carry = 1
s -= 10
else:
carry = 0
# 将当前位的和添加到结果列表中
result.append(str(s))
# 如果最高位有进位,需要将进位加到结果中
if carry:
result.append(str(carry))
# 反转结果并转换为数字
return int(''.join(result[::-1]))
```
该程序可以处理任意长度的整数加法,例如:
```python
>>> add(123456789, 987654321)
1111111110
>>> add(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
阅读全文