@[TOC]
Without further ado, let’s get right to the code.
1. Large number addition
string getCountAdd(string a, string b)
{
string c = "";
int bit = - 1; // Check whether -1 is no, and other bits are carried
int i = a.length(a)- 1; // Get the length of a string
int j = b.length(a)- 1; // Get the length of b string
// Do both in the first case
while(i ! =- 1&& j ! =- 1)
{
int t1 = a[i] - 48;
int t2 = b[j] - 48;
// There is no carry
if (bit == - 1)
{
if (t1 + t2 >= 10)
{
int d = (t1 + t2) % 10;
c.insert(0.1, d + 48);
bit = (t1 + t2) / 10;
}
else
{
c.insert(0.1, t1 + t2 + 48); }}// Carry exists
else
{
if (t1 + t2 + bit >= 10)
{
int d = (t1 + t2 + bit) % 10;
c.insert(0.1, d + 48);
bit = (t1 + t2 + bit) / 10;
}
else
{
c.insert(0.1, t1 + t2 + bit + 48);
bit = - 1;
}
}
i--;
j--;
}
// In the second case, the former is done
while (i == - 1&& j ! =- 1)
{
int t2 = b[j] - 48;
if (bit == - 1)
{
c.insert(0.1, b[j]);
}
else
{
if (t2 + bit >= 10)
{
int d = (t2 + bit) % 10;
c.insert(0.1, d + 48);
bit = (t2 + bit) / 10;
}
else
{
c.insert(0.1, t2 + bit + 48);
bit = - 1;
}
}
j--;
}
// The third case is done with the latter
while(i ! =- 1 && j == - 1)
{
int t1 = a[i] - 48;
if (bit == - 1)
{
c.insert(0.1, a[i]);
}
else
{
if (t1 + bit >= 10)
{
int d = (t1 + bit) % 10;
c.insert(0.1, d + 48);
bit = (t1 + bit) / 10;
}
else
{
c.insert(0.1, t1 + bit + 48);
bit = - 1;
}
}
i--;
}
// Check whether there is a carry
if(bit ! =- 1)
{
c.insert(0.1, bit + 48);
}
bit = - 1;
return c;
}
Copy the code
2. Power operation of large numbers
string getCountExp(int a, int b)
{
string a1 = to_string(a);
int i = a1.length(a)- 1;// The last subscript of a
// The number of m digits x n digits cannot exceed m+ N digits
string temp = a1; // Temp is always changing
string temp_2 = "0";
int bitcount = 0; // Determine the current digit
int bit = - 1;// Determine whether there is a carry
string * arr = new string[a1.length()];// Save each count
int arr_i = 0;
for (int x = 1; x < b; x++)// The power will loop several times
{
while(i ! =- 1)// The number of digits in the multiplier
{
//temp * a1
int t1 = a1[i] - 48;
int j = temp.length() - 1;// The last subscript of temp
for (int z = 0; z < bitcount; z++)
{
arr[arr_i].insert(0.1.'0');
}
while(j ! =- 1)/ / temp digits
{
int t2 = temp[j] - 48;
if (bit == - 1)// Check if there is a carry
{
if (t1*t2 >= 10)
{
int d = (t1*t2) % 10;
arr[arr_i].insert(0.1, d + 48);
int d_2 = (t1*t2) / 10;
bit = d_2;
}
else
{
int d = t1*t2;
arr[arr_i].insert(0.1, d + 48); }}else
{
if ((t1*t2)+bit >= 10)
{
int d = ((t1*t2) + bit) % 10;
arr[arr_i].insert(0.1, d + 48);
int d_2 = ((t1*t2) + bit) / 10;
bit = d_2;
}
else
{
int d = (t1*t2) + bit;
arr[arr_i].insert(0.1, d + 48);
bit = - 1;
}
}
j--;
}
if(bit ! =- 1)
{
arr[arr_i].insert(0.1, bit + 48);
bit = - 1;
}
// Finish the walk
// Count each digit and add them up
//temp_2=temp_2+arr[arr_i];
temp_2 = getCountAdd(temp_2, arr[arr_i]);
bitcount++;
arr_i++;
i--;
}
bitcount = 0;
temp = temp_2;
temp_2 = "0";
//temp_2 = "0";
for (int z = 0; z < arr_i; z++)
{
arr[z] = "";
}
arr_i = 0;
i = a1.length() - 1;// The last subscript of a
}
return temp;
}
Copy the code
3. Mod large numbers
int getCountMod(string a, int b)
{
int bit = - 1; // Determine whether carry is required
/ / such as 4255%
int i = 0;
while (i < a.length())
{
int t1 = a[i] - 48;
if (bit == - 1)
{
if (t1%b > 0) { bit = t1%b; }}else
{
if (((bit * 10) + t1) % b>=0)
{
bit = ((bit * 10) + t1) % b;
}
}
i++;
}
if(bit ! =- 1)
{
return bit;
}
else
{
return 0;
}
return 0;
}
Copy the code