#include
#include
#include
using namespace std;
struct HighAcc
{
char str[100];
int length;
int minflag;
};
void reverse_str(HighAcc &num)
{
char* p = &num.str[0];
char* q = &num.str[num.length - 1];
char tmp = 0;
while (p <= q)
{
tmp = *p;
*p = *q;
q = tmp;
p++;
q–;
}
}
void Ture_num(HighAcc &num)
{
char p = &num.str[0];
while (*p)
{
*p = *p - ‘0’;
p++;
}
}
HighAcc mul(HighAcc num1, HighAcc num2)
{
Ture_num(num1);
Ture_num(num2);
reverse_str(num1);
reverse_str(num2);
HighAcc res = {0};
if ((num1.length == 1 && num1.str[0] == 0) || (num2.length == 1 && num2.str[0] == 0))
{
res.length = 1;
return res;
}
int i=0, j=0;
int tmp;
for (i = 0;i < num1.length;i++)
{
for (j = 0;j < num2.length;j++)
{
tmp = num1.str[i] * num2.str[j] + res.str[i+j];
res.str[i + j] = tmp % 10;
res.str[i + j + 1] += tmp / 10;
}
}
res.length = i + j - !res.str[i + j -1];
return res;
}
void Printf(HighAcc tmp)
{
for (int i = tmp.length - 1;i >= 0;i–)
{
printf("%d", tmp.str[i]);
}
}
int main()
{
HighAcc num1 = {0};
cout << "请输入第一个数字: ";
cin >> num1.str;
cin.clear();
num1.length = strlen(num1.str);
HighAcc num2 = {0};
cout << "请输入第二个数字: ";
cin >> num2.str;
cin.clear();
num2.length = strlen(num2.str);
HighAcc res = mul(num1, num2);
Printf(res);
return 0;
}
来源:CSDN
作者:ldbite
链接:https://blog.csdn.net/ldbite/article/details/104115033