大数加法

/*************************************************************************
    > File Name: big_sum.c
    > Author: 傻李
    > Mail: hellojukay@gmail.com 
    > Created Time: 2014年11月15日 星期六 11时36分29秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#define MAX 1000
int result[MAX];

void add(char *str1,char *str2)
{
 int a[MAX],b[MAX];
 memset(result,0,MAX * sizeof(int));
 memset(a,0,MAX * sizeof(int));
 memset(b,0,MAX * sizeof(int));
 int c;//进位表示
 int sum;
 int i,j;
 for(i = strlen(str1) - 1, j = 0; i >=0; --i,++j)
 {
 a[j] = str1[i] - '0';
 }
 for( i = strlen(str2) - 1, j = 0; i >=0; --i,++j)
 {
 b[j] = str2[i] - '0';
 }
 c = 0;
 for(i = 0 ; i < MAX; ++i)
 {
 sum = a[i] + b[i] + c;
 result[i] = sum % 10;
 c = sum / 10;
 }

}

int main()
{
 int i;
 char a[MAX],b[MAX];
 memset(result,0,MAX);
 scanf("%s",a);
 getchar();
 scanf("%s",b);
 add(a,b);
 for(i = MAX -1; i >=0; --i)
 {
 if(result[i] !=0)
 {
 for(;i>=0; --i)
 printf("%d",result[i]);
 break;
 }
 }
 getchar();
 return 0;
}
=-=-=-=-=
Powered by Blogilo

 

posted @ 2014-11-15 16:09  水墨沙场  阅读(118)  评论(0编辑  收藏  举报