#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define BF_SZ 1000 //buffer size
//将整数转换成字符串,并传出长度, 请手动释放返回值
//stdlib中的itoa也能完成类似的功能,
//但stdlib传入的buffer长度未知,一般会设置得比较大,而造成浪费
char* int_to_str(int n, int* length)
{
*length = floor(log10(abs(n!=0?n:1))) + 1;
char *str = (char*)calloc(sizeof(char)*(*length+1));
int i = *length-1;
while(n!=0)
{
str[i] = n%10+48;
n = n/10;
i--;
}
return str;
}
//大数乗法,结果放在r中
void multiply(const char *a,const char *b, char *r)
{
int i, j, *t, length_a, length_b, length_t; // t for temp
length_a = strlen(a);
length_b = strlen(b);
length_t = length_a + length_b;
t=(int*)malloc(sizeof(int)*length_t);
for(i=0; i<length_t; i++)
{
t[i]=0;
}
for (i=0; i<length_a; i++)
{
for (j=0;j<length_b;j++)
{
t[i+j+1] += (a[i]-48)*(b[j]-48);
}
}
// 这里实现进位操作
for (i=length_t-1; i>=0; i--)
{
if(t[i]>=10)
{
t[i-1] += t[i]/10;
t[i] %= 10;
}
}
i=0;
while(t[i]==0)
{
i++; // 跳过数前面的0
}
for (j=0; i<length_t; i++,j++)
{
r[j] = t[i]+48;
}
r[j]='\0';
free(t);
}
int test(int n, char* buffer, char* temp)
{
int i, j, k, a, b, c, p; //p for product
int len;
for(i=2; i<=n; i++)
{
char *str = int_to_str(i, &len);
//printf("%s\t", str);
//做buffer和str的大数乘法
multiply(buffer, str, buffer);
free(str);
}
return 0;
}
int main()
{
char* buffer = (char*)malloc(BF_SZ*sizeof(char));
char* temp = (char*)malloc(BF_SZ* sizeof(char));
memset(buffer, '0', BF_SZ);
buffer[BF_SZ-1] = '1';
memset(temp, '0', BF_SZ);
test(100, buffer, temp);
printf("buffer is %s\n", buffer);
int i, len=strlen(buffer), count=0;
for (i=0; i<len; i++)
{
count += buffer[i]-48;
}
printf("the count is %d\n", count);
free(buffer);
free(temp);
return 0;