Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. #define mineMax 999999999
  8.  
  9. class bigInt
  10. {
  11.     private:
  12.         vector<int> x;
  13.     public:
  14.         bigInt(int x);
  15.         bigInt(const char* y);
  16.         //vector<int> getVector();
  17.  
  18.  
  19.  
  20.         friend ostream& operator<<(ostream&, bigInt);
  21.  
  22.         friend bigInt operator+(bigInt, int);
  23.         friend void operator+= (bigInt& object, int y);
  24.  
  25.         friend bigInt operator- (bigInt object, int y);
  26.         friend void operator-= (bigInt& object, int y);
  27.  
  28.  
  29.  
  30. };
  31.  
  32. void reverse(string& x);
  33.  
  34. ostream& operator<< (ostream& stream, bigInt object);
  35.  
  36. bigInt operator+ (bigInt object, int y);
  37. void operator+= (bigInt& object, int y);
  38.  
  39.  
  40. bigInt operator- (bigInt object, int y);
  41. void operator-= (bigInt& object, int y);
  42.  
  43.  
  44.  
  45. int main()
  46. {
  47.  
  48.     bigInt a = "999999998";
  49.  
  50.     cout << a << endl;
  51.     a = a + 1;
  52.     cout << a << endl;
  53.     a -= 1;
  54.     cout << a << endl;
  55.     a += 2;
  56.     cout << a << endl;
  57.     a -= 2;
  58.     cout << a << endl;
  59.  /*   a = a + 20;
  60.     cout << a << endl;
  61.     a = a + 300;
  62.     cout << a << endl;
  63.     a = a + 4000;
  64.     cout << a << endl;
  65.     a = a + 50000;
  66.     cout << a << endl;
  67.     a = a + 600000;
  68.     cout << a << endl;
  69.     a = a + 7000000;
  70.     cout << a << endl;
  71.     a = a + 80000000;
  72.     cout << a << endl;
  73.     a = a + 900000000;
  74.     cout << a << endl;
  75.     a = a + 10000000;
  76.     cout << a << endl;*/
  77.  
  78.     return 0;
  79. }
  80.  
  81.  
  82.  
  83. bigInt::bigInt(int x)
  84. {
  85.     this -> x.push_back(x);
  86. }
  87.  
  88. bigInt::bigInt(const char* y)
  89. {
  90.     string bufor;
  91.  
  92.     string x = y;
  93.  
  94.     int i,j;
  95.  
  96.     int eraser = 9;
  97.     if(x.size() > 9)
  98.     {
  99.         do
  100.         {
  101.             for (i = x.size(), j = x.size() - eraser; i > j; i--) {
  102.                 bufor += x[i];
  103.             }
  104.             reverse(bufor);
  105.             x.erase(x.size()-eraser, eraser);
  106.  
  107.             (this -> x).push_back(atoi(bufor.c_str()));
  108.         }while(x.size() > eraser);
  109.  
  110.     }
  111.  
  112.     if(x.size() > 0 ) bufor = x;
  113.  
  114.  
  115.     //cout << "|" << bufor << "|" << endl;
  116.     (this -> x).push_back(atoi(bufor.c_str()));
  117. }
  118.  
  119.  
  120. void reverse(string& x)
  121. {
  122.     for(int i=0; i<x.size()/2; i++)
  123.     {
  124.         swap(x[i],x[x.size()-1-i]);
  125.     }
  126. }
  127.  
  128. ostream& operator<< (ostream& stream, bigInt object)
  129. {
  130.     for(int i=object.x.size()-1; i>=0; i--)
  131.     {
  132.  
  133.         if(i!=object.x.size()-1)
  134.         {
  135.             if(object.x[i] >= 100000000)
  136.             {
  137.                 stream << object.x[i];
  138.             }else if(object.x[i] >= 10000000)
  139.             {
  140.                 stream << "0" << object.x[i];
  141.             }else if(object.x[i] >= 1000000)
  142.             {
  143.                 stream << "00" << object.x[i];
  144.             }else if(object.x[i] >= 100000)
  145.             {
  146.                 stream << "000" << object.x[i];
  147.             }else if(object.x[i] >= 10000)
  148.             {
  149.                 stream << "0000" << object.x[i];
  150.             }else if(object.x[i] >= 1000)
  151.             {
  152.                 stream << "00000" << object.x[i];
  153.             }else if(object.x[i] >= 100)
  154.             {
  155.                 stream << "000000" << object.x[i];
  156.             }else if(object.x[i] >= 10)
  157.             {
  158.                 stream << "0000000" << object.x[i];
  159.             }else
  160.             {
  161.                 stream << "00000000" << object.x[i];
  162.             }
  163.  
  164.  
  165.         }else stream << object.x[i];
  166.  
  167.     }
  168.  
  169.     return stream;
  170. }
  171.  
  172. bigInt operator+(bigInt object, int y)
  173. {
  174.  
  175.     int i = 1;
  176.     int difference = (object.x[0] + y) - mineMax -1 ;
  177.     if(object.x[0] + y > mineMax)
  178.     {
  179.         if(object.x.size() > 1)
  180.         {
  181.             while(object.x[i] + 1 > mineMax || i == object.x.size())
  182.             {
  183.                 i++;
  184.             }
  185.  
  186.             if(i == object.x.size())
  187.             {
  188.  
  189.                 for(int j=1; j<object.x.size(); j++)
  190.                 {
  191.                     object.x[j] = 0;
  192.                 }
  193.                 object.x[0] = difference;
  194.                 object.x.push_back(1);
  195.             }else
  196.             {
  197.                 for(int j=1; j<i-1; j++)
  198.                 {
  199.                     object.x[j] = 0;
  200.                 }
  201.                 object.x[i]++;
  202.                 object.x[0] = difference;
  203.             }
  204.         }else
  205.         {
  206.             object.x[0] = difference;
  207.             object.x.push_back(1);
  208.         }
  209.  
  210.     }else
  211.     {
  212.         object.x[0] += y;
  213.     }
  214.  
  215.  
  216.     return object;
  217.  
  218. }
  219.  
  220. void operator+= (bigInt& object, int y)
  221. {
  222.     object = object + y;
  223. }
  224.  
  225. bigInt operator- (bigInt object, int y)
  226. {
  227.     int i = 1;
  228.     int difference = abs(object.x[0] - y);
  229.  
  230.     if(object.x[0] - y < 0)
  231.     {
  232.         if(object.x.size() > 1)
  233.         {
  234.             while(object.x[i] - 1 < 0 || i == object.x.size())
  235.             {
  236.                 i++;
  237.             }
  238.  
  239.             if(i == object.x.size())
  240.             {
  241.                 for(int j=1; j<object.x.size(); j++)
  242.                 {
  243.                     object.x[j] = 0;
  244.                 }
  245.                 object.x[0] += (mineMax - y);
  246.                 object = object.x[0];
  247.             }else
  248.             {
  249.                 for(int j=1; j<i-1; j++)
  250.                 {
  251.                     object.x[j] = 0;
  252.                 }
  253.                 if(i == object.x.size()-1 && object.x[i]-1 == 0) object.x.erase(object.x.begin());
  254.                 else object.x[i]--;
  255.                 object.x[0] += (mineMax - y);
  256.             }
  257.         }else
  258.         {
  259.             object.x[0] -= y;
  260.             object = object.x[0];
  261.         }
  262.  
  263.     }else
  264.     {
  265.         object.x[0] -= y;
  266.     }
  267.  
  268.  
  269.     return object;
  270.  
  271. }
  272.  
  273. void operator-= (bigInt& object, int y)
  274. {
  275.     object = object - y;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement