大整数乘法.模拟小学生算数.
#include <stdio.h>
#include <string.h>
#define size 10000+2
#define truesize size-1-1
int ans[size*size*100]={0};
int multiply(char a[size],int sa,char b[size],int sb){
int i,j,k,pa,pb,ec=0;
long last=0;
for(i=0;(pa=truesize-i)>=sa;i++){
long cp=0;
for(j=0;(pb=truesize-j)>=sb;j++){
int na=a[pa]-'0';
int nb=b[pb]-'0';
cp=cp+na*nb;
ans[ec=truesize-i-j]+=cp;
cp/=10;
}
while(cp&&ec-1){
ans[--ec]+=cp;
cp/=10;
}
}
ec=truesize;
last=0;
while(ec>=0){
last+=ans[ec];
ans[ec--]=last;
last/=10;
}
for(i=0;i<=truesize;i++)
if(ans[i])break;
return i;
}
int read(char c[size]){
int l=0,i;
char t[size]="";
c[truesize+1]='\0';
while(t[l]=getchar()){
if(t[l]=='\n'||t[l]==' '){
l--;
break;
}
l++;
}
for(i=0;i<=l;i++)
c[truesize-(l-i)]=t[i];
return truesize-l;
}
void show(int i){
if(i<=truesize)
for(;i<=truesize;i++)
printf("%d",ans[i]);
else
printf("%d",0);
printf("\n");
}
int main(){
char a[size]="";
char b[size]="";
int sa=read(a);
int sb=read(b);
int sc=multiply(a,sa,b,sb);
show(sc);
return 0;
}