hozer

ooplab1

Oct 28th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. class Bigint
  8. {
  9.     static const int max = 10;
  10.     int *n;
  11.     int s;
  12.     int sign;
  13.  
  14. public:
  15.     Bigint()
  16.     {
  17.         n = new int[max];
  18.         s = 0;
  19.         sign = 1;
  20.         for (int i = 0; i < max; i++)
  21.             n[i] = 0;
  22.     }
  23.  
  24.     Bigint(int size)
  25.     {
  26.         n = new int[max];
  27.         s = size;
  28.         sign = 1;
  29.         for (int i = 0; i < max; i++)
  30.             n[i] = 0;
  31.     }
  32.  
  33.     Bigint(char *str, int size)
  34.     {
  35.         n = new int[max];
  36.         s = size;
  37.         int i;
  38.         sign = 1;
  39.         for (i = 0; i < s; i++)
  40.         {
  41.             n[s - 1 - i] = str[i] - 48;
  42.  
  43.         }
  44.  
  45.         for (; i < max; i++)
  46.             n[i] = 0;
  47.     }
  48.    
  49.     Bigint(const Bigint &other)
  50.     {
  51.         if (this != &other)
  52.         {
  53.             n = new int[max];
  54.             s = other.s;
  55.             sign = other.sign;
  56.             for (int i = 0; i < max; i++)
  57.                 n[i] = other.n[i];
  58.         }
  59.     }
  60.  
  61.     ~Bigint()
  62.     {
  63.         //n = new int[max];
  64.         delete[] n;
  65.     }
  66.  
  67.     Bigint& operator=(const Bigint &other)
  68.     {
  69.         if (this != &other)
  70.         {
  71.             n = new int[max];
  72.             s = other.s;
  73.             sign = other.sign;
  74.             for (int i = 0; i < max; i++)
  75.                 n[i] = other.n[i];
  76.         }
  77.  
  78.         return *this;
  79.     }
  80.  
  81.     friend ostream& operator<<(ostream &output, const Bigint an)
  82.     {
  83.         if (an.sign == -1) output << '-';
  84.         for (int i = an.s - 1; i >= 0; i--)
  85.         {
  86.             output << an.n[i];
  87.         }
  88.         output << endl;
  89.         return output;
  90.     }
  91.  
  92.     friend istream& operator>>(istream& input, Bigint& an)
  93.     {
  94.         char *str = new char[max];
  95.         int i = 0, j = 0;
  96.         input >> str;
  97.         an.s = strlen(str);
  98.                 delete[] an.n;
  99.                 an.n = new int[max];
  100.         if (str[0] == '-') { an.sign = -1; i++; an.s -= 1; }
  101.         for (j = 0; j < an.s; i++, j++)
  102.         {
  103.             an.n[an.s - 1 - j] = str[i] - 48;
  104.         }
  105.  
  106.         for (; j <= max; j++)
  107.             an.n[j] = 0;
  108.  
  109.         //delete [] str;
  110.         return input;
  111.     }
  112.  
  113.     bool operator==(Bigint an)
  114.     {
  115.         if (sign != an.sign || s != an.s) return false;
  116.         for (int i = 0; i < s; i++)
  117.         {
  118.             if (n[i] != an.n[i]) return false;
  119.         }
  120.  
  121.         return true;
  122.     }
  123.  
  124.     inline bool operator!=(Bigint an) { return !(*this == an); }
  125.  
  126.     bool operator>(Bigint an)
  127.     {
  128.         if (sign == 1 && an.sign == -1) return true;
  129.         else if (sign == -1 && an.sign == 1) return false;
  130.         else
  131.         {
  132.             bool rez = false;
  133.             if (s > an.s) return true;
  134.             else if (s < an.s) return false;
  135.  
  136.             for (int i = 0; i < s; i++)
  137.             {
  138.                 if (n[i] > an.n[i])
  139.                 {
  140.                     rez = true;
  141.                     break;
  142.                 }
  143.             }
  144.  
  145.             if (sign == -1) rez = !rez;
  146.             return rez;
  147.         }
  148.         return false;
  149.     }
  150.  
  151.     inline bool operator<(Bigint an) { return an > *this; }
  152.     inline bool operator<=(Bigint an) { return !(*this>an); }
  153.     inline bool operator>=(Bigint an) { return !(*this<an); }
  154.  
  155.     Bigint operator+(Bigint an)
  156.     {
  157.         Bigint rez = Bigint(*this);
  158.         int m = s > an.s ? s : an.s, c = 0;
  159.         // a + b
  160.         if (rez.sign == 1 && an.sign == 1)
  161.         {
  162.             for (int i = 0; i < m; i++)
  163.             {
  164.                 c += rez.n[i] + an.n[i];
  165.                 rez.n[i] = c % 10;
  166.                 c = c / 10;
  167.             }
  168.  
  169.             rez.s = m;
  170.             if (c > 0)
  171.             {
  172.                 rez.n[m] = c;
  173.                 rez.s = m + 1;
  174.             }
  175.         }
  176.         //-a + -b = -(a + b)
  177.         else if (rez.sign == -1 && an.sign == -1)
  178.         {
  179.             an.sign = 1;
  180.             sign = 1;
  181.             rez = an + *this;
  182.             rez.sign = -1;
  183.         }
  184.         else if (rez.sign == 1 && an.sign == -1)
  185.         {
  186.             an.sign = 1;
  187.             rez = rez - an;
  188.         }
  189.         else
  190.         {
  191.             rez.sign = 1;
  192.             rez = an - rez;
  193.         }
  194.  
  195.         return rez;
  196.     }
  197.  
  198.     Bigint operator-(Bigint an)
  199.     {
  200.         Bigint rez = Bigint(*this);
  201.  
  202.         //a - -b = a + b
  203.         if (sign == 1 && an.sign == -1)
  204.         {
  205.             an.sign = 1;
  206.             rez = *this + an;
  207.         }
  208.         //-a - b = -a + -b
  209.         else if (sign == -1 && an.sign == 1)
  210.         {
  211.             an.sign = -1;
  212.             rez = *this + an;
  213.         }
  214.         //a - b
  215.         else if (sign == 1 && an.sign == 1)
  216.         {
  217.             if (rez < an) {
  218.                 rez = an - rez;
  219.                 rez.sign = -1;
  220.             }
  221.             else
  222.             {
  223.                 int c = 0;
  224.                 for (int i = 0; i < rez.s; i++)
  225.                 {
  226.                     c = c + rez.n[i] - an.n[i] + 10;
  227.                     rez.n[i] = c % 10;
  228.                     if (c < 10) c = -1;
  229.                     else c = 0;
  230.                 }
  231.  
  232.                 while (rez.n[rez.s - 1] == 0 && rez.s > 1) rez.s--;
  233.             }
  234.         }
  235.         else
  236.         {
  237.             an.sign = 1;
  238.             rez.sign = 1;
  239.             rez = an - rez;
  240.         }
  241.  
  242.         return rez;
  243.     }
  244.  
  245.     Bigint operator*(Bigint an)
  246.     {
  247.         Bigint rez = Bigint();
  248.         int cr = 0, k;
  249.  
  250.         if (sign != an.sign) rez.sign = -1;
  251.  
  252.         for (int i = 0; i < s; i++)
  253.         {
  254.             for (int j = 0; j < an.s; j++)
  255.             {
  256.                 cr = n[i] * an.n[j];
  257.                 k = i + j;
  258.                 while (cr>0)
  259.                 {
  260.                     cr += rez.n[k];
  261.                     rez.n[k] = cr % 10;
  262.                     cr = cr / 10;
  263.  
  264.                     if (k + 1>rez.s) rez.s = k + 1;
  265.  
  266.                     k += 1;
  267.                 }
  268.             }
  269.         }
  270.  
  271.         return rez;
  272.     }
  273.  
  274.     Bigint operator/(long an)
  275.     {
  276.         Bigint rez = Bigint();
  277.         int ch = 0, k = 0;
  278.         for (int i = 0; i < s; i++)
  279.         {
  280.             ch = ch * 10 + n[s - i - 1];
  281.             if (ch < an && k == 0 && i > 1) continue;
  282.             k = 1;
  283.             if (ch / an != 0) {
  284.                 rez.n[s - i - 1] = ch / an;
  285.                 rez.s++;
  286.             }
  287.             ch = ch % an;
  288.         }
  289.         return rez;
  290.     }
  291.  
  292.     long operator%(long an)
  293.     {
  294.         long r = 0;
  295.         for (int i = 0; i < s; i++)
  296.         {
  297.             r = (r * 10 + n[s-i-1]) % an;
  298.         }
  299.         return r;
  300.     }
  301.  
  302.     bool isZero()
  303.     {
  304.         return (s == 1 && n[0] == 0) ? true : false;
  305.     }
  306.  
  307.     Bigint pow(int n)
  308.     {
  309.         Bigint r = *this;
  310.         if (n == 0)
  311.         {
  312.             Bigint zero = Bigint("1", 1);
  313.             return zero;
  314.         }
  315.  
  316.         for (int i = 1; i < n; i++)
  317.         {
  318.             r = r * *this;
  319.         }
  320.  
  321.         return r;
  322.     }
  323. };
  324.  
  325.  
  326. int main(int argc, char* argv[])
  327. {
  328.     Bigint n1, n2;
  329.     int i1;
  330.     long l1;
  331.     cin >> n1;
  332.     cin >> n2;
  333.     //cout << (n1 < n2 ? "true" : "false") << endl;
  334.     //cout << n1 + n2 << endl;
  335.     /*cout << n1 - n2 << endl;
  336.     cout << n1 * n2 << endl;*/
  337.     /*cin >> n1;
  338.  
  339.     cout << n1.pow(50);*/
  340.  
  341.     /*cin.ignore();
  342.     cin.get();
  343.     cout << n1;
  344.         cout << n2;  */
  345.     return 0;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment