Product//大数乗算
2262 ワード
#include <stdio.h>
#include <string.h>
#define N 1000
void yiwei(char *ch, int a);
void add(char *ch1, char *ch2);
void rev(char *ch);
int main()
{
char ch1[N], ch2[N], ch3[N], ch4[N];
int i, j, k;
while(scanf("%s %s", ch1, ch2) != -1)
{
rev(ch1);rev(ch2);
memset(ch4, 0, sizeof(ch4));
for( i=0; ch1[i]!=0; i++)
{
memset(ch3, 0, sizeof(ch3));
for( j=0; ch2[j]!=0; j++) //ch3 +1, 1 0 。
{
ch3[j] = (ch1[i]-'0')*(ch2[j]-'0')+1;
}
for( k=0; ch3[k]; k++)
{
if(ch3[k]-1 > 9)
{
if(!ch3[k+1])
{
ch3[k+1] += (ch3[k]-1)/10+1;
ch3[k] = (ch3[k]-1)%10+1;
}
else
{
ch3[k+1] += (ch3[k]-1)/10;
ch3[k] = (ch3[k]-1)%10+1;
}
}
}
k = 0;
while(ch3[k])
{
ch3[k] = ch3[k]-1+'0';
k++;
}
yiwei(ch3, i);
add(ch4, ch3);
}
rev(ch4);
if(ch4[0]=='0')
ch4[1] = 0;
printf("%s
", ch4);
}
return 0;
}
void yiwei(char *ch, int a)
{
int len, i;
len = strlen(ch);
for( i=len+a; i>=a; i--)
{
ch[i] = ch[i-a];
}
for(i=0; i<a; i++)
ch[i] = '0';
}
void add(char *ch1, char *ch2)
{
int i = 0;
while(ch1[i] || ch2[i])
{
if(!ch1[i] || !ch2[i])
ch1[i] = ch1[i]+ch2[i];
else
ch1[i] = ch1[i]+ch2[i]-'0';
i++;
}
i = 0;
while(ch1[i])
{
if(ch1[i] > '9')
{
if(!ch1[i+1])
{
ch1[i+1] += (ch1[i]-'0')/10+'0';
ch1[i] = '0'+(ch1[i]-'0')%10;
}
else
{
ch1[i+1] += (ch1[i]-'0')/10;
ch1[i] = '0'+(ch1[i]-'0')%10;
}
}
i++;
}
}
void rev(char *ch)
{
int len, i;
char ch1[N];
len = strlen(ch);
for(i=0; i<len; i++)
ch1[len-i-1] = ch[i];
for(i=0; i<len; i++)
ch[i] = ch1[i];
}