大数相乘,用字符串实现。 #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> void multiply(char *a, char *b, char **result) { int i, j, ca, cb, *s; ca = strlen(a); cb = strlen(b); s = (int*)malloc(sizeof(int)*(ca+cb)); for (i=0; i<ca+cb; i++) s[i]=0; printf("The s is :%d and %d/n", ca, cb); for (i=0; i<ca; i++) for (j=0; j<cb; j++) s[i+j+1] += (a[i]-'0')*(b[j]-'0'); for (i=ca+cb-1; i>=0; i--){ if (s[i] >= 10){ s[i-1] += s[i]/10; s[i]%=10; } } i=0; while(s[i] == 0) i++; (*result) = (char*)malloc( ca + cb + 1 - i); for ( j=0; i < 8; i++,j++){ (*result)[j] = s[i] + '0'; } result[j] = '/0'; free(s); } int main() { char *a = "1234"; char *b = "2345"; char *result; multiply(a, b, &result); printf("The number %s multiply %s result in %s./n", a, b, result); return 0;