If you are looking for high accuracy (large integers) class, proceeds to
https://blog.csdn.net/tomjobs/article/details/99988413 or
https://blog.csdn.net/tomjobs/article/details/101856803
here just simple array simulation, of consolidating the foundation.
Precision adder: a direct one added like, greater than 10 proceeds to the next
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char a1[1005],b1[1005];
char a[1005],b[1005];
int c[1005];
int main()
{
scanf("%s%s",a1 + 1,b1 + 1);
int len1 = (int)strlen(a1 + 1),len2 = (int)strlen(b1 + 1);
for(int i = 1;i <= len1;i++)
{
a[i] = a1[len1 - i + 1] - '0';
}
for(int i = 1;i <= len2;i++)
{
b[i] = b1[len2 - i + 1] - '0';
}
int len = max(len1,len2);
for(int i = 1;i <= len;i++)
{
c[i] += a[i] + b[i];
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
if(c[len + 1])len++;
for(int i = len;i >= 1;i--)printf("%d",c[i]);
return 0;
}
Precision subtraction: first compares the size (the number of bits, same bit position is relatively the same size)
if less than subtrahend minuend exchanged, and outputs a negative sign.
The actual operation performed when a borrow, if a [i] <b [i ], then a [i] plus 10, next minus one.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
char s1[maxn],s2[maxn];
int a1[maxn],a2[maxn];
bool cmp(int a[],int b[])
{
if(a[0] > b[0])return 1;
if(a[0] < b[0])return 0;
for(int i = 1;i <= a[0];i++)
{
if(a[i] < b[i])return 0;
if(a[i] > b[i])return 1;
}
return 1;
}
int main()
{
scanf("%s%s",s1 + 1,s2 + 1);
int len1 = (int)strlen(s1 + 1),len2 = (int)strlen(s2 + 1);
for(int i = 1;i <= len1;i++)
{
a1[i] = s1[len1 - i + 1] - '0';
}
for(int i = 1;i <= len2;i++)
{
a2[i] = s2[len2 - i + 1] - '0';
}
a1[0] = len1;a2[0] = len2;
int flag = 0;
if(!cmp(a1,a2))
{
flag = 1;
swap(a1,a2);
}
for(int i = 1;i <= len1;i++)
{
if(a1[i] < a2[i])
{
a1[i] += 10;
a1[i + 1]--;
}
a1[i] -= a2[i];
}
while(a1[a1[0]] == 0 && a1[0] > 1)
{
a1[0]--;
}
if(flag)printf("-");
for(int i = a1[0];i >= 1;i--)printf("%d",a1[i]);
}
Precision multiplication: by one bit, and assigning to the second i + j - 1 bits.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
char s1[maxn],s2[maxn];
int a1[maxn],a2[maxn],a[maxn];
int main()
{
scanf("%s%s",s1 + 1,s2 + 1);
int len1 = (int)strlen(s1 + 1),len2 = (int)strlen(s2 + 1);
for(int i = 1;i <= len1;i++)
{
a1[i] = s1[len1 - i + 1] - '0';
}
for(int i = 1;i <= len2;i++)
{
a2[i] = s2[len2 - i + 1] - '0';
}
a1[0] = len1;a2[0] = len2;
int len = len1 + len2;
for(int i = 1;i <= len1;i++)
{
for(int j = 1;j <= len2;j++)
{
int m = i + j - 1;
a[m] += a1[i] * a2[j];
a[m + 1] += a[m] / 10;
a[m] %= 10;
}
}
while(len > 1 && a[len] == 0)len--;
for(int i = len;i >= 1;i--)printf("%d",a[i]);
return 0;
}
Precision division: in essence, is an analog subtractor.
A waste of a cut too, reducing the number of possible binary processing.
It can be similar to the "double", the 10 th power minus b, first a - k1 * b * 1000,
then a - k2 * b * 100, a - k3 * b * 10 ...
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
char s1[maxn],s2[maxn];
int a1[maxn],a2[maxn],a3[maxn],a4[maxn];
bool cmp(int a[],int b[])
{
if(a[0] > b[0])return 1;
if(a[0] < b[0])return 0;
for(int i = a[0];i >= 1;i--)
{
if(a[i] > b[i])return 1;
if(a[i] < b[i])return 0;
}
return 1;
}
void minu(int a[],int b[])
{
for(int i = 1;i <= a[0];i++)
{
if(a[i] < b[i])
{
a[i] += 10;
a[i + 1]--;
}
a[i] -= b[i];
}
while(a[a[0]] == 0 && a[0] > 1)
{
a[0]--;
}
}
int main()
{
scanf("%s%s",s1 + 1,s2 + 1);
int len1 = (int)strlen(s1 + 1),len2 = (int)strlen(s2 + 1);
for(int i = 1;i <= len1;i++)a1[i] = s1[len1 - i + 1] - '0';a1[0] = len1;
for(int i = 1;i <= len2;i++)a2[i] = s2[len2 - i + 1] - '0';a2[0] = len2;
a4[0] = len1 - len2 + 1;
for(int i = a4[0];i >= 1;i--)
{
memset(a3,0,sizeof(a3));
for(int j = 1;j <= a2[0];j++)a3[j + i - 1] = a2[j];
a3[0] = a2[0] + i - 1;
while(cmp(a1,a3))
{
a4[i]++;
minu(a1,a3);
}
}
while(a4[a4[0]] == 0 && a4[0] > 1)
{
a4[0]--;
}
for(int i = a4[0];i >= 1;i--)printf("%d",a4[i]);
return 0;
}
Factorial and precision
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int sum[maxn],fac[maxn];
int main()
{
fac[1] = 1;fac[0] = 1;
int n;scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
int carry = 0;
for(int j = 1;j <= fac[0];j++)
{
fac[j] *= i;fac[j] += carry;
carry = fac[j] / 10;
fac[j] %= 10;
}
if(carry)fac[++fac[0]] = carry;
for(int j = 1;j <= fac[0];j++)
{
sum[j] += fac[j];
sum[j + 1] += sum[j] / 10;
sum[j] %= 10;
}
if(sum[sum[0] + 1])sum[0]++;
}
for(int i = sum[0];i >= 1;i--)printf("%d",sum[i]);
return 0;
}