高精度計算合集

陳煜晗發表於2020-10-15

高精度計算

高精度加法

#include<iostream>
#include<cstdio>
#include<cstring>
void sum(char* a1, char* b1);
using namespace std;
int main()
{
 char a1[100], b1[100];
 gets_s(a1);
 gets_s(b1);
 sum(a1, b1);
 return 0;
}
void sum(char* a1,char* b1 )
{
 int   a[100], b[100];
 int c[100], x, lena, lenb, lenc, i;
 memset(a, 0, sizeof(a));
 memset(b, 0, sizeof(b));
 memset(c, 0, sizeof(c));
 lena = strlen(a1);
 lenb = strlen(b1);
 for (i = 0; i <= lena - 1; i++) a[lena - i] = a1[i] - 48;
 for (i = 0; i <= lenb - 1; i++)b[lenb - i] = b1[i] - 48;
 lenc = 1;
 x = 0;
 while (lenc <= lena || lenc <= lenb)
 {
  c[lenc] = a[lenc] + b[lenc] + x;
  x = c[lenc] / 10;
  c[lenc] %= 10;
  lenc++;
 }
 c[lenc] = x;
 if (c[lenc] == 0) lenc--;
 for (int i = lenc; i >= 1; i--) cout << c[i];
 cout << endl;
}

高精度減法

#include<iostream>
#include<cstdio>
#include<cstring>
void sum(char* a1, char* b1);
using namespace std;
int main()
{
 char a1[100], b1[100];
 gets_s(a1);
 gets_s(b1);
 sum(a1, b1);
 return 0;
}
void sum(char* a1,char* b1 )
{
 int   a[100], b[100];
 int c[100], x, lena, lenb, lenc, i;
 memset(a, 0, sizeof(a));
 memset(b, 0, sizeof(b));
 memset(c, 0, sizeof(c));
 lena = strlen(a1);
 lenb = strlen(b1);
 for (i = 0; i <= lena - 1; i++) a[lena - i] = a1[i] - 48;
 for (i = 0; i <= lenb - 1; i++)b[lenb - i] = b1[i] - 48;
 lenc = 1;
 x = 0;
 while (lenc <= lena || lenc <= lenb)
 {
  c[lenc] = a[lenc] + b[lenc] + x;
  x = c[lenc] / 10;
  c[lenc] %= 10;
  lenc++;
 }
 c[lenc] = x;
 if (c[lenc] == 0) lenc--;
 for (int i = lenc; i >= 1; i--) cout << c[i];
 cout << endl;
}

高精度乘法

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
 char a1[1000], b1[1000];
 int a[1000], b[1000], c[1000], lena, lenb, lenc, i, j, x;
 memset(a, 0, sizeof(a));
 memset(b, 0, sizeof(b));
 memset(c, 0, sizeof(c));
 gets_s(a1); gets_s(b1);
 lena = strlen(a1); lenb = strlen(b1);
 for (i = 0; i <= lena - i; i++)a[lena - i] = a1[i] - '0';
 for (i = 0; i <= lenb - i; i++)b[lenb - i] = b1[i] - '0';
 for (i = 1; i <= lena; i++)
 {
  x = 0;
  for (j = 1; j <= lenb; j++)
  {
   c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
   x = c[i + j - 1] / 10;
   c[i + j - 1] %= 10;
  }
  c[i + lenb] = x;
 }
 lenc = lena + lenb;
 while (c[lenc] == 0 && lenc > 1) lenc--;
 for (i = lenc; i >= 1; i--) cout << c[i];
 cout << endl;
}
}

高精度除以低精度

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
 char a1[1000], b1[1000];
 int a[1000], b[1000], c[1000], lena, lenb, lenc, i, j, x;
 memset(a, 0, sizeof(a));
 memset(b, 0, sizeof(b));
 memset(c, 0, sizeof(c));
 gets_s(a1); gets_s(b1);
 lena = strlen(a1); lenb = strlen(b1);
 for (i = 0; i <= lena - i; i++)a[lena - i] = a1[i] - '0';
 for (i = 0; i <= lenb - i; i++)b[lenb - i] = b1[i] - '0';
 for (i = 1; i <= lena; i++)
 {
  x = 0;
  for (j = 1; j <= lenb; j++)
  {
   c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
   x = c[i + j - 1] / 10;
   c[i + j - 1] %= 10;
  }
  c[i + lenb] = x;
 }
 lenc = lena + lenb;
 while (c[lenc] == 0 && lenc > 1) lenc--;
 for (i = lenc; i >= 1; i--) cout << c[i];
 cout << endl;
}

相關文章