【关键在于怎么把大数读入数组(用一个char型变量做辅助)】
#include <stdio.h>
#include <string.h>
#define M 10
#define N 1000
void add(int *a,int *b,int *c)//加法
{
int i,carry=0;
for (i=N-1;i>=0;i--)
{
c[i]=a[i]+b[i]+carry;
if (c[i]>=M)
{
c[i]-=M;
carry = 1;
}
else
{
carry = 0;
}
}
}
void con(int *a,int *c)//转换
{
int i,j;
i=0;
while (c[i]==0)
i++;
j=999;
while (i<=999)
a[j--]=c[i++];
}
void main()
{
int a[1000],b[1000],c[1000],i;
char ch;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
i=999;
memset(c,0,sizeof(c));
while ((ch=getchar())!='\n')
c[i--]=ch-48;
con(a,c);
i=999;
memset(c,0,sizeof(c));
while ((ch=getchar())!='\n')
c[i--]=ch-48;
con(b,c);
memset(c,0,sizeof(c));
add(a,b,c);
i=0;
while (c[i]==0)
i++;
while (i<=999)
printf("%d",c[i++]);
printf("\n");
}
本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/04/06/2435352.html,如需转载请自行联系原作者