nim 任意长度整数相加

#编程实现任意长度的两个正整数相加
import stacks
type
    BigInt =  string

var
    a = "123456789323999999999999999999999999999999999922222222222222222222222222212345678932399999999999999999999999999999999992222222222222222222222222222222"
    b = "4567125456312323333333333333333333333333333333335555555555555512"
proc `$`(num:BigInt): string =
    result = num.string

proc `+`(num1,num2:BigInt):BigInt =
    var
        r = Stack[char]()
        a,b,s,c:int
        x = num1.len - 1
        y = num2.len - 1
        zero = ord('0')
    while x >= 0 or y >= 0:
        a = if x >= 0: ord(num1[x]) - zero else : 0
        b = if y >= 0: ord(num2[y]) - zero else : 0
        s = (a + b + c) mod 10
        c = (a + b + c) div 10 
        r.push(chr(s + zero ))
        x.dec
        y.dec
    while not r.empty:
        result.add(r.pop)
    
echo a + b

 

posted @ 2019-10-20 20:19  beasu  阅读(80)  评论(0)    收藏  举报