#include<stdio.h>
int *multi1=NULL,*multi2=NULL,*result=NULL;
int c=0,length1,length2;
int i,j,k,m,n,p,q,suffix;
main()
{
printf("Please input the length of multiplicand:");
scanf("%d",&length1);/*输入被乘数的位数*/
multi1=(int *)malloc(length1*sizeof(int));
if(multi1==NULL)
{
printf("No enough memory!");
exit(0);
}
printf("Now enter the multiplicand:");
for(i=0;i<length1;i++)
scanf("%1d",multi1+i);
printf("Then please input the length of multiplier:");
scanf("%d",&length2);/*输入乘数的位数*/
multi2=(int *)malloc(length2*sizeof(int));
if(multi2==NULL)
{
printf("No enough memory!");
exit(0);
}
printf("Now enter the multiplier:");
for(j=0;j<length2;j++)
scanf("%1d",multi2+j);
result=(int *)malloc(length1*length2*sizeof(int));
if(result==NULL)
{
printf("No enough memory!");
exit(0);
}
for(k=0;k<length1*length2;k++)
*(result+k)=0;
for(m=i-1;m>=0;m--)
for(n=j-1;n>=0;n--)
{
c=(*(multi1+m))*(*(multi2+n));
suffix=k-(i-m)-(j-n)+1;
*(result+suffix)+=c%10;
if(*(result+suffix)>=10)
{
*(result+suffix)=*(result+suffix)%10;
*(result+suffix-1)+=1;
}
*(result+suffix-1)+=c/10;
if(*(result+suffix-1)>=10)
{
*(result+suffix-1)=*(result+suffix-1)%10;
*(result+suffix-2)+=1;
}
}
for(p=0;p<=k;p++)
{
if(*(result+p)!=0)
break;
}
for(q=p;q<=k;q++)
printf("%1d",*(result+q));
printf("\n");
free(multi1);
free(multi2);
free(result);
getch();
}
俺是只菜鸟,m*n位相乘结果虽然可以出来,但总带些后缀(比如输入99*99,结果就是
980130065),晕死了,麻烦哪个高手帮俺看一下,找一下错误,谢谢!!