C + + High-precision implementation of 10,000-digit multiplication and division method

Source: Internet
Author: User

Description: To do a calculator, is to upload the entire project, but suddenly found that can not upload resources, paste out the algorithm part of the code, we have a lot of advice ah, oh, more difficult to read, and then upload the entire project, hehe

conversion, for multiplication part call
int turn (char s[], int num[])
{
int i = 0, Len, J;
while (1)
{
if (s[i] = = 0) break;
else if (s[i] = = '-' | | s[i] = = ' 0 ') i++;
else break;
//i point to the first non 0 number
for (j = 0; S[j]!= 0; j + +);
j--; J Point to last digit
for (len = 0; J >= I; J--, len++)
{
Num[len] = (int) s[j]-48;
}
return (LEN);
}


Conversion, for division part call
int turn2 (char s[], int num[])
{
int i = 0, Len, J;
while (1)
{
if (s[i] = = 0) break;
else if (s[i] = = '-' | | s[i] = = ' 0 ') i++;
else break;
//i point to the first non 0 number
for (j = 0; S[j]!= 0; j + +);
j--; J Point to last digit
for (len = 0; J >= i; i++, len++)
{
Num[len] = (int) s[i]-48;
}
return (LEN);
}

void Mul (int *a, int *b, int *c, int len1, int len2)
{
int I, J;
for (i = 0; i < len1; i++)
{
for (j = 0; J < Len2; J + +)
{
C[i + j] + = a[i] * B[j];
if (C[i+j] >= 10)
{
C[i+j+1] + = c[i+j]/10;
C[I+J] = c[i+j]% 10;
}
}
}
}


Compare size, called by division function
int cmp (int *a, int *b, int len1, int len2)
{
int I, len, result = 0, J;
for (i = 0; a[i] = = 0; i++);
len = len1-i;
if (Len < len2) result =-1;
else if (len > len2) result = 1;
Else
{
for (j = 0; I < Len1, J < Len2; i++, J. J + +)
{
if (A[i] > B[j])
{
result = 1;
Break
}
if (A[i] < b[j])
{
result =-1;
Break
}
}
}
return (result);
}


Divide by two numbers, called by the Division function
void Div (int *a, int *b1, int *c, int len1, int len2)
{
int I, j, M, N, p, Q, b[101], chb[101] = {0}, Chlen, result_of_cmp;
for (i = 1; I <= len1; i++)
{
for (j = 1;; j + +)
{
for (m = 0; m < len2; m++) b[m] = b1[m];
for (M = len2-1 m >= 0; m--) b[m] *= J;
for (M = len2-1 m > 0; m--)
{
if (B[m] >= 10)
{
B[m-1] + = b[m]/10;
B[M] = b[m]% 10;
}
}
if (B[0] >= 10)
{
Chlen = len2 + 1;
Chb[0] = b[0]/10;
CHB[1] = b[0]% 10;
for (n = 2; n < Chlen n++) chb[n] = b[n-1];
}
Else
{
Chlen = Len2;
for (n = 0; n < chlen n++) chb[n] = B[n];
}
RESULT_OF_CMP = CMP (A, ChB, I, Chlen);
if (result_of_cmp = = 0 | | | result_of_cmp = = 1) break;
}
if (result_of_cmp = = 0) c[i-1] = j;
else c[i-1] = j-1;
for (p = i-1, q = len2-1 p >= 0 && q >= 0 P--, q--)
{
A[P] = a[p]-b1[q] * c[i-1];
while (A[p] < 0)
{
a[p-1]--;
A[P] = a[p] + 10;
}
}
}
}


Multiplication section

Highprecdll_api void Multiply (char* a,char* b,char* c)
{
int I, j, num1[101] = {0}, num2[101] = {0}, tmp[10001] = {0}, len1, Len2;
if ((a[0] = = ' 0 ' && a[1] = = 0) | | (b[0] = = ' 0 ' && b[1] = = 0)) {c[0] = ' 0 '; c[1] = 0; return;}
if ((a[0] = = '-' && a[1] = = 0) | | (b[0] = = '-' && b[1] = = 0)) {c[0] = ' 0 '; c[1] = 0; return;}
if ((a[0] = = '-' && a[1] = = ' 0 ' && a[2] = = 0) | | (b[0] = = '-' && b[1] = = ' 0 ' | | | b[2] = = 0)) {c[0] = ' 0 '; c[1] = 0; return;}

Len1 = Turn (A, NUM1);
Len2 = Turn (b, num2);

Mul (NUM1, NUM2, TMP, LEN1, LEN2);

for (i = 10000; Tmp[i] = = 0; i--);
if ((a[0] = = '-' && b[0]!= '-') | | (A[0]!= '-' && b[0] = = ')
{
C[0] = '-';
for (j = 1; I >= 0; I--, j + +) C[j] = ' 0 ' + tmp[i];
C[J] = 0;
}
Else
{
for (j = 0; I >= 0; I--, j + +) C[j] = ' 0 ' + tmp[i];
C[J] = 0;
}
}


Division section

HIGHPRECDLL_API void Division (char* a,char* b,char* c)
{
int I, j, num1[101] = {0}, num2[101] = {0}, tmp[101] = {0}, Len1, Len2, Mark;
if (strcmp (b, "0") = = 0 | | strcmp (b, "-0") = = 0) {strcpy (c, "Error");

Len1 = Turn2 (A, NUM1);
Len2 = turn2 (b, num2);
Mark = CMP (NUM1, num2, Len1, len2);
if (Mark = = 1) {strcpy (C, "0");
Div (NUM1, num2, TMP, LEN1, LEN2);

for (i = 0; tmp[i] = = 0; i++);
if ((a[0] = = '-' && b[0]!= '-') | | (A[0]!= '-' && b[0] = = ')
{
C[0] = '-';
for (j = 1; i < len1 i++, J + +) C[j] = tmp[i] + ' 0 ';
C[J] = 0;
}
Else
{
for (j = 0; I < len1 j + + i++) c[j] = tmp[i] + ' 0 ';
C[J] = 0;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.