您好,欢迎来到划驼旅游。
搜索
您的当前位置:首页整数大数乘法以及小数大数乘法实现

整数大数乘法以及小数大数乘法实现

来源:划驼旅游


声明:本算法可以实现整数乘以整数,小数乘以小数功能。但是小数只能是小数点前不为0

的小数。比如0.1之类的不适用。

#include

#include

using namespace std;

void multiply(const char *a, const char *b)

{

int length1 = strlen(a);

int length2 = strlen(b);

int *p = new int[length1 + length2];

for (int i = 0; i < length1 + length2; i++)

{

p[i] = 0;

}

for (int i = 0; i < length1; i++)

{

for (int j = 0; j < length2; j++)

{

p[i + j + 1] += (a[i] - '0')*(b[j] - '0');

}

}

for (int i = length1 + length2 - 1; i >= 0; i--)

{

if (p[i] >= 10)

{

p[i - 1] += p[i] / 10;

p[i] = p[i] % 10;

}

}

char *pp = new char[length1 + length2 + 1];

int count = 0;

while (p[count] == 0)

{

count++;

}

int i1;

for (i1 = 0; count < length1 + length2; i1++, count++)

{

pp[i1] = (p[count] + '0');

}

pp[i1] = '\\0';

cout << pp << endl;

delete[]p;

delete[]pp;

}

void dianmultiply(const char *a, const char *b)

{

int place1 = 0;

int place2 = 0;

char *newp1 = new char[strlen(a)];

char *newp2 = new char[strlen(b)];

int k1 = 0;

int k2 = 0;

for (int i = 0; i < strlen(a); i++)

{

if (a[i] != '.')

{

newp1[k1] = a[i];

k1++;

}

else

{

place1 = i;

}

}

newp1[k1] = '\\0';

/*cout << newp1 << endl;*/

for (int i = 0; i < strlen(b); i++)

{

if (b[i] != '.')

{

newp2[k2] = b[i];

k2++;

}

else

{

place2 = i;

}

}

newp2[k2] = '\\0';

/*cout << newp2 << endl;*/

int length1 = strlen(newp1);

int length2 = strlen(newp2);

//cout << length1 << \" \" << length2 << endl;

//cout << place1 << \" \" << place2 << endl;

int *p = new int[length1 + length2];

for (int i = 0; i < length1 + length2; i++)

{

p[i] = 0;

}

for (int i = 0; i < length1; i++)

{

for (int j = 0; j < length2; j++)

{

p[i + j + 1] += (newp1[i] - '0')*(newp2[j] - '0');

}

}

for (int i = length1 + length2 - 1; i >= 0; i--)

{

if (p[i] >= 10)

{

p[i - 1] += p[i] / 10;

p[i] = p[i] % 10;

}

}

//cout << p << endl;

char *pp = new char[length1 + length2 + 2];

int count = 0;

while (p[count] == 0)

{

count++;

}

int weizhi = 0;

if (place1 != 0)

{

place1 = strlen(a) - place1 - 1;

weizhi += place1;

}

if (place2 != 0)

{

place2 = strlen(b) - place2 - 1;

weizhi += place2;

}

int i1;

/*cout << count << endl;*/

int length = length1 + length2 - count;

int flag =0;

int tem = count;

for (i1 = 0; count < length1 + length2; i1++, count++)

{

if (tem == 0)

{

if (count == length - weizhi && flag == 0)

{

pp[i1] = '.';

count--;

flag++;

}

else

{

pp[i1] = (p[count] + '0');

}

}

else

{

if (count == length - weizhi + 1 && flag == 0)

{

pp[i1] = '.';

count--;

flag++;

}

else

{

pp[i1] = (p[count] + '0');

}

}

}

pp[i1] = '\\0';

cout << pp << endl;

delete []p;

delete []pp;

}

bool judge(const char *p, const char *q)

{

for (int i = 0; i < strlen(p); i++)

{

if (p[i] == '.')

{

return true;

}

}

for (int i = 0; i < strlen(q); i++)

{

if (q[i] == '.')

{

return true;

}

}

return false;

}

int main()

{

string dashu1;

string dashu2;

cin >> dashu1;

cin >> dashu2;

const char *p1 = dashu1.c_str();

const char *p2 = dashu2.c_str();

cout << p1 << \"*\" << p2 << \"=\";

if (judge(p1, p2))

{

dianmultiply(p1, p2);

}

else

{

multiply(p1, p2);

}

system(\"pause\");

return 0;

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo6.com 版权所有 湘ICP备2023023988号-11

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务