C ++ knowledge point precision

A high-precision adder

1.1 precision adder

        The basic operation is the high-precision arithmetic add and subtract. And subtraction arithmetic rules, like vertical simulation calculation, considering the carry misalignment calculation process.

#include <cstdio>
#include <cstring>
int main()
{
char a[202]={0}, b[202]={0};
scanf("%s%s", a, b);
int alen = strlen(a), blen = strlen(b), t = 0, i;
int a1[202]={0}, b1[202]={0};
for (i = 0; i < alen; i++)    a1[i] = a[alen-1-i]-'0';
for (i = 0; i < blen; i++)    b1[i] = b[blen-1-i]-'0';
alen = (alen > blen) ? alen : blen;
for (i = 0; i <= alen; i++)
t = a1[i]+b1[i], a1[i] = t%10, a1[i+1] += t/10;
while (!a1[i] && i) i--;
for(; i >= 0; i--) printf("%d", a1[i]);
return 0;
}

1.2 precision adder (pressure level) (Qing North school results)

      int type 9 digits can be stored, and said code to each element of the array saved only one digit (0-9), can be said to waste a lot of space, and the computer calculates and 4 + 5 + 4444 with time 3333 it is the same, so we sometimes use pressure level to save space and time. The principle is as follows:

      Reading from the keyboard into a large integer and stored in the character array

      From the front of each eight-bit number stored in an array element of an int

      Corresponding elements of two arrays of addition or subtraction of a carry forward to the last output

#include <iostream>
#include <cstring>
#include <cstdio> 
using namespace std;
const int INF = 1E8;
struct Data{
int u[50], l;
Data(){
memset(u, 0, sizeof(u)), l = 0;
}
void change(string a){
int len = a.size(), k = len / 8, i = 0;
l = k + (len%8 > 0);
for (len; len > 8; len -= 8)
sscanf(a.substr(len-8, 8).c_str(), "%d", &u[i++]);
if (len > 0) sscanf(a.substr(0, len).c_str(), "%d", &u[i]);
}
void print(){
int k = l-1;
printf("%d", u[k--]);
while (k >= 0) printf("%8.8d", u[k--]);
printf("\n");
}
}a, b;
int main(){
string aa, bb, ac;
cin >> aa >> bb;
int ka = 0, kb = 0, i;
a.change(aa), b.change(bb);
for (i = 0; i < 50; i++)
a.u[i] += b.u[i], a.u[i+1] += a.u[i] / INF, a.u[i] %= INF;
for (i = 49; a.u[i]==0 && i>0; i--);
a.l = i + 1;
a.print();
return 0;
}

II. Precision subtraction
2.1 subtraction precision

      Principles and additions as needed, but consider not carry, but borrow.

      code show as below:

#include <cstdio>
#include <cstring>
int main()
{
char a[202]={0}, b[202]={0};
scanf("%s%s", a, b);
int alen = strlen(a), blen = strlen(b), t = 0, i;
int a1[202]={0}, b1[202]={0};
for (i = 0; i < alen; i++)    a1[i] = a[alen-1-i]-'0';
for (i = 0; i < blen; i++)    b1[i] = b[blen-1-i]-'0';
alen = (alen > blen) ? alen : blen;
for (i = 0; i <= alen; i++)
t = a1[i]-b1[i], t<0?(t+=10,a1[i+1]--):t, a1[i] = t;
while (!a1[i] && i) i--;
for(; i >= 0; i--) printf("%d", a1[i]);
return 0;
}

2.2 high-precision subtraction (pressure level) (also a clear north outcomes)

      Subtraction and addition similar, if you will the addition, then subtraction but also to fear.

#include <iostream>
#include <cstring>
#include <cstdio> 
using namespace std;
const int INF = 1E8;
struct Data{
int u[50], l;
Data(){
memset(u, 0, sizeof(u)), l = 0;
}
void change(string a){
int len = a.size(), k = len / 8, i = 0;
l = k + (len%8 > 0);
for (len; len > 8; len -= 8)
sscanf(a.substr(len-8, 8).c_str(), "%d", &u[i++]);
if (len > 0) sscanf(a.substr(0, len).c_str(), "%d", &u[i]);
}
void print(){
int k = l-1;
printf("%d", u[k--]);
while (k >= 0) printf("%8.8d", u[k--]);
printf("\n");
}
}a, b;
int main(){
string aa, bb, ac;
cin >> aa >> bb;
int ka = 0, kb = 0, i,t;
a.change(aa), b.change(bb);
for (i = 0; i < 50; i++)
t = a.u[i] - b.u[i],(t < 0)?(t+=INF,a.u[i+1]--):t,a.u[i] = t; 
for (i = 49; a.u[i]==0 && i>0; i--);
a.l = i + 1;
a.print();
return 0;
}

Prerequisites code is a> = b, so the front need a judgment as error also need a, b flipping

III. Precision multiplication

    The basic principle is as follows

                        3210--> array a, b subscript

                        3 4 5 6 i -> array a []

                    * 1 2 7 8 j -> B array []

               ————————————

                 2    7    6    4    8 

            2    4    1    9    2

            6    9    1    2

      3    4    5    6

——————————————————

      4416768--> Array C []

      6 5 4 3 2 1 0 i + j -> array subscript c

These are both a vertical digit multiplication calculation method. As can be seen, the number of right alignment, is calculated from the low-order, after the end of a calculation result of the addition is the answer. Then the number of right to left two labeled 0,1,2 ... n, the result of each column is the first number of the subscript i is a number with a second number of index j, the number of the multiplication result, which is stored in the column of the i + j. The end result is the sum of each column, i + j is the number of all columns are added. It can be c [i + j] + = a [i] * b [j].

for(int i = 0;i < LA;i++)
for(int j = 0;j < LB;j++)
c[i+j] += a[i]*b[j];
for(int i = 0;i < LA+LB;i++)
if(c[i] >= 10)
{
c[i+1] += c[i]/10;
c[i] %= 10; 
}


Code temporary cool back off, more than a few paragraphs back Gengshuang (--QAQ--)

Guess you like

Origin www.cnblogs.com/Jiangxingchen/p/12469373.html
Recommended