Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. const int ksize = 1000;
  6.  
  7. class Huge{
  8. public:
  9.     int a[ksize];
  10.  
  11.     void Reset(){
  12.         memset(a,0,sizeof(a));
  13.         a[0]=1;
  14.     }
  15.     Huge(){
  16.         Reset();
  17.     }
  18.  
  19.     void setValue(int x){
  20.         Reset();
  21.         a[0]=0;
  22.         do{
  23.             a[0]++;
  24.             a[a[0]] = x % 10;
  25.             x/=10;
  26.         }while(x);
  27.     }
  28.     void operator +=(const Huge &other){
  29.         int i, t= 0;
  30.         for(i = 1; i <= a[0] || i <= other.a[0] || t != 0;i++, t/=10)
  31.         {
  32.             a[i] = (t+= a[i] + other.a[i]) % 10;
  33.         }
  34.         a[0] = i - 1;
  35.     }
  36.  
  37.     void operator -=(const Huge &other){
  38.         int i, t =0;
  39.         for(i=1;i<= a[0];i++)
  40.         {
  41.             a[i] = (a[i] - t) - other.a[i];
  42.  
  43.             if(a[i] < 0)
  44.                 t = 1;
  45.             else
  46.                 t = 0;
  47.             a[i] += t * 10;
  48.  
  49.         }
  50.         while(a[0] > 1 && (a[a[0]] == 0))
  51.             a[0]--;
  52.     }
  53.  
  54.     void operator *=(const int x){
  55.         int i, t = 0;
  56.         for(i = 1; i <= a[0]; i++, t/=10)
  57.         {
  58.             a[i] = (t+= a[i] * x) % 10;
  59.         }
  60.         a[0] = i -1;
  61.         while(t)
  62.         {
  63.             a[++a[0]] = t % 10;
  64.             t/=10;
  65.         }
  66.     }
  67.  
  68.     void operator *=(const Huge &other)
  69.     {
  70.         int i,j,t=0;
  71.         Huge aux;
  72.         for(i=1; i<=a[0] ; i++)
  73.             for(j = 1, t = 0; j <= other.a[0] || t ; j++, t/=10)
  74.                 aux.a[i+j-1] = (t += aux.a[i+j-1] + a[i] * other.a[j]) % 10;
  75.  
  76.         if(i + j - 3 > aux.a[0])
  77.             aux.a[0] = i + j - 3;
  78.  
  79.         Reset();
  80.         for(i = 1; i<= aux.a[0]; i++)
  81.             a[i] = aux.a[i];
  82.         a[0] = aux.a[0];
  83.     }
  84.  
  85.  
  86.     void addInteger(int x)
  87.     {
  88.         int i, t= 0;
  89.         for(i = 1; i<= a[0] || x!=0 || t!= 0; i++, t/=10, x/=10)
  90.         {
  91.             a[i] = (t += a[i] + x % 10) % 10;
  92.         }
  93.         a[0] = i - 1;
  94.     }
  95.  
  96.     void printNumber()
  97.     {
  98.         int i;
  99.         for(i = a[0]; i >= 1; i--)
  100.             printf("%d",a[i]);
  101.     }
  102.  
  103.  
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement