python 兩個無窮大的數相加

ac = '11223344556677889911111111111216541342132134563573457435744444444444.444444444444444444444444444444444444444444444444444444444444444444444444444444444444444'
bc = '9988776655443322111111111111135686796789678324135512353214561111111111.111111111111111111111111111122222222222222233333333333333333333333455555555555555566666'




def str2list(param):
    return_list = []
    for x in range(len(param)):
        try:
            return_list.append(int(param[x]))
        except:
            return_list.append(param[x])
    return return_list


def summary(a, b):
    final_list = []
    if '.' in a and '.' not in b:
        split_a = a.split('.')[-1]
        b += '.'
        for x in range(len(split_a)):
            b += '0'
        
    elif '.' in b and '.' not in a:
        a += '.'
        split_b = b.split('.')[-1]
        for x in range(len(split_b)):
            a += '0'
    elif '.' in a and '.' in b:
        split_a = a.split('.')[-1]
        split_b = b.split('.')[-1]
        if len(split_a) > len(split_b):
            for x in range((len(split_a) - len(split_b))):
                b += '0'
        elif len(split_a) < len(split_b):
            for x in range((len(split_b) - len(split_a))):
                a += '0'
    else:
        pass
    list_a = str2list(a)
    list_b = str2list(b)

    if len(list_a) > len(list_b):
        increase = 0
        for x in range(len(list_a)):
            try:
                tmp_a = list_a.pop()
            except:
                tmp_a = 0
            try:
                tmp_b = list_b.pop()
            except:
                tmp_b = 0
            if tmp_a == '.' and tmp_b == '.':
                final_list.append('.')
            else:
                tmp_sum = tmp_a + tmp_b
                tmp_list = tmp_sum % 10
                if tmp_list + increase >= 10:
                    final_list.append((tmp_list + increase) % 10)
                    tmp_increase = int((tmp_list + increase) / 10)
                    increase = int(tmp_sum / 10) + tmp_increase
                else:
                    final_list.append(tmp_list + increase)
                    increase = int(tmp_sum / 10)
        if increase > 0:
            final_list.append(increase)
    else:
        increase = 0
        for x in range(len(list_b)):
            try:
                tmp_a = list_a.pop()
            except:
                tmp_a = 0
            try:
                tmp_b = list_b.pop()
            except:
                tmp_b = 0
            if tmp_a == '.' and tmp_b == '.':
                final_list.append('.')
            else:
                tmp_sum = tmp_a + tmp_b
                tmp_list = tmp_sum % 10
                if tmp_list + increase >= 10:
                    final_list.append((tmp_list + increase) % 10)
                    tmp_increase = int((tmp_list + increase) / 10)
                    increase = int(tmp_sum / 10) + tmp_increase
                else:
                    final_list.append(tmp_list + increase)
                    increase = int(tmp_sum / 10)
        if increase > 0:
            final_list.append(increase)

    return final_list[::-1]

if __name__ == '__main__':
    # a = '11223344556677889911111111111216541342132134563573457435744444444444'
    # b = '9988776655443322111111111111135686796789678324135512353214561111111111'
    a = '11223344556677889911111111111216541342132134563573457435744444444444.444444444444444444444444444444444444444444444444444444444444444444444444444444444444444'
    b = '9988776655443322111111111111135686796789678324135512353214561111111111.111111111111111111111111111122222222222222233333333333333333333333455555555555555566666'
    c = 11223344556677889911111111111216541342132134563573457435744444444444
    d = 9988776655443322111111111111135686796789678324135512353214561111111111
    ss = summary(a, b)
    print(''.join(map(str, ss)))
    print(c + d)


上面是代碼塊,源數據暫時使用的是字符串,後面考慮下源數據是整型和浮點型的如何進行處理,這裏只是進行了兩個正數進行相加。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章