#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * BigNumberMulti(const char * a, const char *b);
int main(void)
{
const char * a = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
const char * b = "9999999999999999999999999999999999999999";
char * s = BigNumberMulti(a, b);
if ( s==NULL )
{
printf("结果为NULL\n");
exit(1);
}
else
{
printf("%s × %s = %s\n", a, b, s);
}
//free ( s );
exit(EXIT_SUCCESS);
}
char * BigNumberMulti(const char * a, const char *b)
{
int lengthOfA = strlen(a);
int lengthOfB = strlen(b);
int i = 0;
int j = 0;
int * result = (int *)malloc( sizeof(int) * (lengthOfA+lengthOfB) );
char * char_result = (char * )malloc( sizeof(char) * (lengthOfA+lengthOfB) );
if ( result==NULL || char_result==NULL)
{
printf("分配内存失败,在文件%s中第%d行!!\n", __FILE__, __LINE__);
return NULL;
}
else
{
printf("分配内存成功,在文件%s中第%d行!!\n", __FILE__, __LINE__);
}
memset( result, 0, sizeof(int)*(lengthOfA+lengthOfB) );
memset( char_result, 0, sizeof(char)*(lengthOfA+lengthOfB) );
for (i=0; i<lengthOfA; i++)
{
for (j=0; j<lengthOfB; j++)
{
result[i+j+1] += ( a[i]-'0' ) * ( b[j]-'0' );
//printf("s[%d+%d+1] is %d\n", i, j, result[i+j+1]);
}
}
/*
for (i=0; i<lengthOfA+lengthOfB; i++)
{
printf("%d ", result[i]);
}
*/
for (i=lengthOfA+lengthOfB-1; i>=0; i--)
{
if ( result[i] >= 10 )
{
result[i-1] += result[i] / 10;
result[i] = result[i] % 10;
}
}
i=0;
while( result[i] == 0 )
{
i++;
}
for (j=0; i<lengthOfA+lengthOfB; j++, i++)
{
char_result[j] = result[i]+'0';
}
char_result[j] = '\0';
//printf("char_result is %s\n", char_result);
free( result );
result = NULL;
return char_result;
}