输入:两个大整数,以空格分开
输出:大整数的乘积
程序如下:
#include <stdio.h>
#include <string.h>
typedef struct{
int nums[100];
int length;
}bigNumber;
bigNumber create(char arr[]){
bigNumber A;
int i;
A.length=strlen(arr);
for(i=0;i<A.length;i++)
A.nums[A.length-1-i]=arr[i]-48;
return A;
}
bigNumber multiply(bigNumber A,bigNumber B){
bigNumber C;
int i,j;
memset(C.nums,0,sizeof(C.nums));
for(i=0;i<A.length;i++)
for(j=0;j<B.length;j++)
C.nums[i+j]+=A.nums[i]*B.nums[j];
i=0;
while(i<A.length+B.length-1||C.nums[i]){//这个判断很重要,两个位数分别为a、b的整数相乘,
//结果只能是a+b-1位或者a+b位
C.nums[i+1]+=C.nums[i]/10;
C.nums[i]=C.nums[i]%10;
i++;
}
C.length=i;
return C;
}
void print(bigNumber C){
int i;
for(i=C.length-1;i>=0;i--)
printf("%d",C.nums[i]);
putchar('\n');
}
int main(){
char strA[100],strB[100];
bigNumber numA,numB,numC;
while(scanf("%s%s",strA,strB)!=EOF){
numA=create(strA);
numB=create(strB);
numC=multiply(numA,numB);
print(numC);
}
return 0;
}