High precision algorithm data structure and common function implementation (C ++)

xiaoxiao2021-03-30  195

High-precision algorithms are common in various programming questions and practical applications, here is given the most common ideas and a set of functions.

Structural HP describes this number, member LEN represents the number length, S [] records the number on each bit, in fact, only 10 numbers, use int, some specific occasions can be defined with CHAR definition S [], just corresponding The program part must also make some changes.

The results are then calculated using the most primitive approach.

Of course, this group of functions is only demonstrated, but it is not possible to carry out complex mathematical operations, but this is not difficult to write more complex operations on this basis.

High-precision common function implementation (C ) #define maxSize 100

Struct HP

{

Int Len;

INT S [MaxSize 1];

}

Void Input (HP & A, String STR)

{

INT I;

While (Str [0] == '0' && str.size ()! = 1)

Str.rase (0,1);

a.len = (int) str.size ();

For (i = 1; i <= a.len; i)

A.S [I] = STR [A.LEN-I] -48;

For (i = a.len 1; i <= maxsize; i)

A.s [i] = 0;

}

Void Print (const HP & Y)

{

INT I;

For (i = Y.len; i> = 1; I -)

Cout << Y.S ​​[i];

Cout << Endl;

}

Void Plus (Const HP & A, Const HP & B, HP & C) // High Accuracy Add C = A B

{

INT I, LEN;

For (i = 1; i <= maxsize; i ) C.S [i] = 0;

IF (a.len> b.len) len = a.len;

Else len = b.1;

For (i = 1; i <= le; i )

{

C.S [i] = a.s [i] b.s [i];

IF (C.S [I]> = 10)

{

C.S [I] - = 10;

C.S [i 1] ;

}

}

IF (C.S [LEN 1]> 0) Len ;

C.1 = len;

}

Void Subtract (Const HP & A, Const HP & B, HP & C) // High-precision subtraction c = a-b

{

INT I, LEN;

For (i = 1; i <= maxsize; i ) C.S [i] = 0;

IF (a.len> b.len) len = a.len;

Else len = b.1;

For (i = 1; i <= le; i )

{

C.S [i] = a.s [i] -b.s [i];

IF (C.S [i] <0)

{

C.S [i] = 10;

C.S [i 1] -;

}

}

While (Len> 1 && C.s [LEN] == 0) LEN -;

C.1 = len;

}

Int Compare (Const HP & A, Const HP & B)

{

Int Len;

IF (a.len> b.len) len = a.len;

Else len = b.1;

While (LEN> 0 && A.s [LEN] == B.S [LEN]) LEN -; if (len == 0) Return 0;

Else Return A.s [len] -b.s [len];

}

Void Multiply (Const HP & A, INT B, HP & C) // High Accuracy * Single Joint

{

INT I, LEN;

For (i = 1; i <= maxsize; i ) C.S [i] = 0;

Len = a.len;

For (i = 1; i <= le; i )

{

C.S [i] = a.s [i] * b;

C.S [i 1] = C.S [i] / 10;

C.S [I]% = 10;

}

Len ;

While (c.s [len]> = 10)

{

C.S [LEN 1] = C.s [len] / 10;

C.S [len]% = 10;

Len ;

}

While (Len> 1 && C.s [LEN] == 0) LEN -;

C.1 = len;

}

Void Multiplyh (Const HP & A, Const HP & B, HP & C) // High Accuracy * High Precision

{

INT I, J, LEN;

For (i = 1; i <= maxsize; i ) C.S [i] = 0;

For (i = 1; i <= a.len; i )

For (j = 1; j <= b.len; j )

{

C.S [i j-1] = a.s [i] * b.s [j];

C.S [i j] = C.s [i j-1] / 10;

C.S [i J-1]% = 10;

}

Len = a.len b.len 1;

While (Len> 1 && C.s [LEN] == 0) LEN -;

C.1 = len;

}

Void Divide (Const HP & A, INT B, HP & C, INT & D) // High Accuracy / Single Joint {D is the remainder}

{

INT I, LEN;

For (i = 1; i <= maxsize; i ) C.S [i] = 0;

Len = a.len;

D = 0;

For (i = le; i> = 1; I -)

{

D = D * 10 A.s [i];

C.S [I] = D / B;

D% = B;

}

While (Len> 1 && C.s [LEN] == 0) LEN -;

C.1 = len;

}

Void Multiply10 (HP & A) // High Accuracy * 10

{

INT I;

For (i = a.len; i> = 1; i -)

A.S [i 1] = a.s [i];

A.S [1] = 0;

a.len ;

While (a.len> 1 && a.s [a.len] == 0) a.1--;

}

Void Divideh (Const HP & A, Const HP & B, HP & C, HP & D) // High Accuracy / High Accuracy {D is the remainder}

{

HP E;

INT I, LEN;

For (i = 1; i <= maxsize; i )

{

C.S [I] = 0;

D.S [i] = 0;}

Len = a.len;

D.1 = 1;

For (i = le; i> = 1; I -)

{

Multiply10 (d);

D.s [1] = a.s [i];

While (Compare (d, b)> = 0)

{

Subtract (D, B, E);

D = E;

C.S [i] ;

}

}

While (Len> 1 && C.s [LEN] == 0) LEN -;

C.1 = len;

}

转载请注明原文地址:https://www.9cbs.com/read-130666.html

New Post(0)