The addition, multiplication, and factorial operations of large numbers may cause overflow of results. You can convert them into strings before performing operations. Note that, traditionally, the addition and multiplication operations start from the low position. The first bit is calculated, and the second bit is carried to the high position until the highest bit. A string represents a number such as "3476". Its low number is at the maximum subscript. To be consistent with the operation in practice, you can first reverse the string, after finding the result, you can reverse the result. The next addition operation uses the inverse method, and the multiplication operation can also use similar inversion, but the multiplication operation in this article does not use inversion. The calculated result can be saved in a string or an integer array. The next addition operation stores the result in a string, multiplication and factorial Save the result in an integer array. When performing a factorial operation, each digit is stored in an integer variable, and a loop is used to multiply each digit by 2, 3, 4, 5, 6 ..., And process carry, so that the factorial of the number can be obtained.
# Include <iostream> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <cassert> using namespace STD; const int n = 110; void numadd (int A, int B) // Add {assert (a> 0 & B> 0); char * T1 = new char [100]; char * t2 = new char [100]; sprintf (T1, "% d", a); sprintf (t2, "% d", B); string oper1 (T1 ); string oper2 (T2); reverse (oper1.begin (), oper1.end (); // reverse string reverse (oper2.begin (), oper2.end (); In T I = 0, j = 0; int carry = 0; // carry string res (50, '0'); int K = 0; while (k <oper1.size () & K <oper2.size () {res [k] = oper1 [k] + oper2 [k] + carry-'0 '; carry = (RES [k]-'0')/10; If (RES [k]-'9'> 0) RES [k]-= 10; + + k;} while (k <oper1.size () {res [k] = oper1 [k] + carry; carry = (RES [k]-'0 ') /10; If (RES [k]-'9'> 0) RES [k]-= 10; ++ K;} while (k <oper2.size ()) {res [k] = oper2 [k] + carry; carry = (RES [k]-'0')/10; if (RES [k]-'9'> 0) RES [k]-= 10; ++ K;} If (carry) {res [k ++] = '1 ';} For (I = res. Size ()-1; I> = 0; -- I) if (RES [I]! = '0') break; Res = res. substr (0, I + 1); reverse (res. begin (), Res. end (); // reverse the result to cout <res <Endl;} void nummulti (char a [], char B []) // multiply {if (a = NULL | B = NULL) return; int Lena = strlen (a); int lenb = strlen (B ); if (Lena = 0 | lenb = 0) return; int Len = Lena + lenb; int C [N] = {0}; int I, J; int flag = 0; for (I = lena-1; I> = 0; I --) for (j = lenb-1; j> = 0; j --) {c [I + J + 1] + = (a [I]-'0') * (B [J]-'0 '); if (C [I + J + 1]> = 10) {C [I + J] + = C [I + J + 1]/10; c [I + 1 + J] = C [I + J + 1] % 10 ;}} I = 0; while (C [I] = 0) I ++; // process for (; I <Len; I ++) {printf ("% d", C [I]) when the highest bit is 0;} void numfactorial (int n) // factorial {If (n <= 0) return; int res [1000]; int cur = 0; Res [0] = 1; int I = 2, J; int Pos = 1; int carry = 0; for (I = 2; I <= N; ++ I) {carry = 0; // carry for (j = 1; j <= Pos; ++ J) {int TMP = I * res [J-1] + carry; Res [J-1] = TMP % 10; carry = tmp/10;} while (carry) {res [+ pos-1] = carry % 10; carry/= 10;} for (I = pos-1; i> = 0; -- I) cout <res [I]; cout <Endl;} int main () {numfactorial (6); int n = 1000; int A = 3556390; int B = 2478389; numadd (a, B); char * T1 = new char [50]; char * t2 = new char [50]; sprintf (T1, "% d", a); sprintf (t2, "% d", B); nummulti (T1, T2); Delete [] T1; delete [] T2; System ("pause"); Return 0 ;}
Multiplication of large numbers and factorial