#include
#include
#include
#define SIZE 65535
void main(void)
{char a[SIZE];
char b[SIZE];
int al, bl;
int i, j, k=0;
int af=0;
int fench;
int temp1, temp2, end1;
int all, yu; //分别保存2数的乘积和mod10的结果
char filename[20]="in.txt";
FILE *fpin,*fpout;
fpout=fopen("result.txt","wb");
char c=0;
char input;
memset(a,0,SIZE);
memset(b,0,SIZE);
if((fpin=fopen(filename,"rb"))==NULL)
{ printf("open file %s failed!\n",filename);
exit(0);
}
i=0;
j=0;
while(input=fgetc(fpin),!feof(fpin))//从文件读取运算的数字
{scanf:if(input==32||input==10||input==13)
{input=fgetc(fpin);
af=1;
goto scanf;
}
if(af==0)
{a=input;
i++;
}
else
{b[j]=input;
j++;
}
}
al=strlen(a);
bl=strlen(b);
all=al+bl;
printf("数据读入完成! 数a的长度: %d , 数b的长度: %d .\n",al,bl);
char* result=(char*)malloc(all);
memset(result,48,all);
printf("result malloc ok!\n");
printf("\n计算中...");
fench=0;
for(i=bl-1;i>=0;i--)
{
temp1=b-48;
for(j=al-1;j>=0;j--)
{
temp2=a[j]-48;
end1=temp1*temp2+k+(result[all-al+j-fench]-48);//计算2个数的乘积并加上进位,初始进位为0
k=end1/10; //计算进位值
yu=end1%10; //计算输出结果
c=yu+48; //将结果换成字符形式
result[all-al+j-fench]=c;
if((j==0)&&(end1>=10))
result[all-al+j-1-fench]=k+48;//判断乘数已到最高位后有没有进位,有则进
}
k=0;
fench++;
}
printf("\n--------------------------------------------------------------------------------\n");
for(i=0;i);//输出结果
fputc(result,fpout);//把结果写入文件
}
printf("\nok!\n");
free(result);//释放内存
result=NULL;
} |