//任意长度的两个字符串数列相乘的算法实现
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
unsigned arr1[MAX_LEN];
unsigned arr2[MAX_LEN];
unsigned result[MAX_LEN * 2 + 1];
char str1[MAX_LEN];
char str2[MAX_LEN];
int main(int argc, char *argv[])
{
int len1 = 0, len2 = 0, len3 = 0;
int i, j;
memset(arr1, 0, sizeof(arr1));
memset(arr2, 0, sizeof(arr2));
memset(result, 0, sizeof(result));
//输入字符串数列
scanf("%s", &str1);
scanf("%s", &str2);
//字符串数列长度
len1 = strlen(str1);
len2 = strlen(str2);
len3 = len1 + len2 + 1;
//把输入的字符串数列反向装入数组,即个位在数组下标为[0]个位置
for (i = len1 - 1, j = 0; i >= 0; i--)
arr1[j++] = str1[i] - '0';
for (i = len2 - 1, j = 0; i >= 0; i--)
arr2[j++] = str2[i] - '0';
//保存不进位相乘的积
for (i = 0; i < len1; i++)
{
for (j = 0; j < len2; j++)
{
result[i + j] += arr1[i] * arr2[j];
}
}
//对结果进行进位操作
for (i = 0; i < len3; i++)
{
if (result[i] >= 10)
{
result[i] %= 10;
result[i + 1] += result[i] / 10;
}
}
//去掉结果中开始的无效数字0
for (i = len3 - 1; i >= 0; i--)
{
if (result[i] == 0)
continue;
else
break;
}
//打印结果
for ( ; i >= 0; i--)
{
printf("%d", result[i]);
}
printf("\n");
return 0;
}