/*大数相乘
思路: 1.先将字符串倒序并转换为数字
2.逐位相乘,并存入一个数组e[i + j]中
3.处理进位,并消去多余的0
4.转换并把数组e[i]反转输出*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
#define Max 1000
int main()
{
char a[Max] = "12345678", b[Max]="87654321";
int c[Max], d[Max], e[Max];
int a1, a2;
while (cin >> a >> b)
{
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
memset(e, 0, sizeof(e));
a1 = strlen(a);
a2 = strlen(b);
//将字符串倒序,将字符转化为数字才能进行乘法
int x = 0;
for (int i = a1 - 1; i >= 0; i--)
{
c[x] = a[i] - 48;
x++;
}
x = 0;
for (int i = a2 - 1; i >= 0; i--)
{
d[x] = b[i] - 48;
x++;
}
//对两组字符串中的数逐位相乘,存于e[i+j]
for (int i = 0; i < a1; i++)
{
for (int j = 0; j < a2; j++)
{
e[i + j] += (c[i] * d[j]);
}
}
//得到的结果进行进位制处理
for (int j = 0; j < Max; j++)
{
//进制位处理的写法
if (e[j] >= 10)
{
e[j + 1] += e[j] / 10;
e[j] %= 10;
}
}
int i;
//除去多余的0
for (i = Max - 1; i >= 0; i--)
{
if (e[i] != 0)
break;
}
printf("%s * %s = ", a, b);
//反转数组并输出结果
for (; i >= 0; i--)
{
printf("%d", e[i]);
}
printf("\n");
}
return 0;
}