hozer

[OOP]Lab4

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