Matlab实现两个大数相加

来源:互联网 发布:时时技巧软件 编辑:程序博客网 时间:2024/05/16 11:25

对自己很是无语!

function D = strAdd(str1)    n1 = length(str1);    n2 = length(str2);    flag = 0;    D = [];    while n1 >= 1 & n2 >= 1      a = str2double(str1(n1));      b = str2double(str2(n2));      c = mod(a + b + flag,10);      flag = floor((a + b + flag) / 10);      D = [D,c];      n1 = n1 - 1;      n2 = n2 - 1;    end    while n1 >= 1         a = str2double(str1(n1));        c = mod(a + flag,10);        flag = floor((a + flag) / 10);        D = [D,c];        n1 = n1 - 1;    end    while n2 >= 1         a = str2double(str2(n2));        c = mod(a + flag,10);        flag = floor((a + flag) / 10);        D = [D,c];        n2 = n2 - 1;    end    if flag > 0        D = [D,flag];    end    D = D(end : -1 :1);end


0 0