Advertisement
VladSmirN

длинная арифметика -

Nov 27th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef vector<int> num;
  6. int const base = 1000 * 1000 * 1000;
  7.  
  8.  
  9.  
  10. class Num
  11. {
  12.  
  13. public:
  14.     vector<int>m;
  15.     read()
  16.     {
  17.         num a;
  18.         string s;
  19.         cin >> s;
  20.         int size = s.size();
  21.         for (int i=(int)s.length(); i>0; i-=9)
  22.             if (i < 9)
  23.                 this->m.push_back (atoi (s.substr (0, i).c_str()));
  24.             else
  25.                 this->m.push_back (atoi (s.substr (i-9, 9).c_str()));
  26.         //this->show();
  27.  
  28.     }
  29.     show()
  30.     {
  31.         cout << (this->m.empty() ? 0 : this->m.back());
  32.         int size = this->m.size();
  33.         for (int i = size - 2; i > -1; --i)
  34.         {
  35.             cout << this->m[i];
  36.         }
  37.  
  38.     }
  39.     vector<int> substract(vector<int> a, vector<int> b)
  40.     {
  41.         // a>=b
  42.         int carry = 0;
  43.         for (int i = 0; i < b.size() || carry; ++i)
  44.         {
  45.             a[i] -= carry + (i < b.size() ? b[i] : 0);
  46.             carry = a[i] < 0;
  47.             if (carry){
  48.                     a[i] += base;
  49.  
  50.                     }
  51.         }
  52.         while (a.size() > 1 && a.back() == 0)a.pop_back();
  53.         return a;
  54.     }
  55.  
  56.     Num  operator-(const Num& b)
  57.     {
  58.         Num s;
  59.         if (this->m.size() > b.m.size())
  60.         {
  61.  
  62.             s.m = substract(this->m, b.m);
  63.             return s;
  64.         }
  65.         if (this->m.size() == b.m.size())
  66.         {
  67.             bool f = 0;
  68.             for (int i = this->m.size() - 1; i > -1; --i)
  69.             {
  70.                 if (this->m[i] < b.m[i])
  71.                 {
  72.                     f = 1;
  73.                     break;
  74.                 }
  75.             }
  76.             if (!f)
  77.             {
  78.                 s.m = substract(this->m, b.m);
  79.                 return s;
  80.  
  81.             }
  82.  
  83.             s.m =  substract(b.m, this->m);
  84.             s.m[s.m.size() - 1] *= -1;
  85.             return s;
  86.         }
  87.         if (this->m.size() < b.m.size())
  88.         {
  89.  
  90.             s.m =  substract(b.m, this->m);
  91.             s.m[s.m.size() - 1] *= -1;
  92.             return s;
  93.         }
  94.  
  95.     }
  96.  
  97. };
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. int main()
  106. {
  107.     Num a;
  108.     a.read();
  109.     Num b;
  110.     b.read();
  111.     Num c = a - b;
  112.      c.show();
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement