您的位置:首页 > 编程语言 > C语言/C++

c语言大数相加

2013-11-05 00:00 148 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void add(char str1[], char str2[], char str3[])
{
int ns1[100] = {0};
int ns2[100] = {0};
int len1 = strlen(str1);
int len2 = strlen(str2);
int i=0, j=0, k=0, t=0;
if (len1 > len2)
{
while (i < len1)
{
ns1[i+1] = str1[i] - '0';
++i;
}
i = 0;
while (i < len2)
{
ns2[i+len1-len2+1] = str2[i] - '0';
++i;
}
} else {
while (i < len1)
{
ns1[i+len1-len2+1] = str1[i] - '0';
++i;
}
i = 0;
while (i < len2)
{
ns2[i+1] = str2[i] - '0';
++i;
}
}
k = i = len1>len2?len1:len2;
while (i>0)
{
t = (ns1[i]+ns2[i])/10;
ns1[i] = (ns1[i]+ns2[i])%10;
--i;
ns1[i] += t;
}
if (ns1[0] != 0)
while (i<=k)
str3[i] = ns1[i]+'0', ++i;
else
while (i+1<=k)
str3[i] = ns1[i+1]+'0', ++i;
str3[i] = '\0';
}

int main()
{
char s1[100] = {0};
char s2[100] = {0};
char s3[101] = {0};
int i;
scanf("%s%s", s1, s2);
add(s1, s2, s3);
printf("%s\n", s3);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  大数相加