Advertisement
Toliak

ded ded ded ded ded

Dec 20th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. #define MAXDIGITS   1000        /* maximum length bignum */
  7. #define PLUS        1       /* positive sign bit */
  8. #define MINUS       -1      /* negative sign bit */
  9.  
  10. struct BigInt
  11. {
  12.     char digits[MAXDIGITS];         /* represent the number */
  13.     int signbit;            /* 1 if positive, -1 if negative */
  14.     int lastdigit;          /* index of high-order digit */
  15.  
  16.     BigInt operator+(BigInt& b) {
  17.         BigInt c;
  18.         add_bignum(this, &b, &c);
  19.         return c;
  20.     }
  21.  
  22.     BigInt operator -(BigInt& b) {
  23.         BigInt c;
  24.         subtract_bignum(this, &b, &c);
  25.         return c;
  26.     }
  27.     BigInt operator << (BigInt& b) {}
  28.     BigInt operator >>(BigInt& b) {}
  29.  
  30.     bool operator < (BigInt& b)
  31.     {
  32.         return compare_bignum(this, &b) > 0;
  33.     }
  34.     bool operator > (BigInt b)
  35.     {
  36.         return compare_bignum(this, &b) < 0;
  37.     }
  38.     bool operator == (BigInt b)
  39.     {
  40.         return compare_bignum(this, &b) == 0;
  41.     }
  42.     bool operator != (BigInt b) {
  43.         return !(*this == b);
  44.     }
  45.  
  46.     int max(int a, int b)
  47.     {
  48.         if (a > b) return(a);
  49.         else return(b);
  50.     }
  51.  
  52.     void add_bignum(BigInt *a, BigInt *b, BigInt *c)
  53.     {
  54.         int carry;          /* carry digit */
  55.         int i;              /* counter */
  56.  
  57.         initialize_bignum(c);
  58.  
  59.         if (a->signbit == b->signbit) c->signbit = a->signbit;
  60.         else {
  61.             if (a->signbit == MINUS) {
  62.                 a->signbit = PLUS;
  63.                 subtract_bignum(b, a, c);
  64.                 a->signbit = MINUS;
  65.             }
  66.             else {
  67.                 b->signbit = PLUS;
  68.                 subtract_bignum(a, b, c);
  69.                 b->signbit = MINUS;
  70.             }
  71.             return;
  72.         }
  73.  
  74.         c->lastdigit = max(a->lastdigit, b->lastdigit) + 1;
  75.         carry = 0;
  76.  
  77.         for (i = 0; i <= (c->lastdigit); i++) {
  78.             c->digits[i] = (char)(carry + a->digits[i] + b->digits[i]) % 10;
  79.             carry = (carry + a->digits[i] + b->digits[i]) / 10;
  80.         }
  81.  
  82.         zero_justify(c);
  83.     }
  84.  
  85.     void int_to_bignum(int s, BigInt *n)
  86.     {
  87.         int i;              /* counter */
  88.         int t;              /* int to work with */
  89.  
  90.         if (s >= 0) n->signbit = PLUS;
  91.         else n->signbit = MINUS;
  92.  
  93.         for (i = 0; i < MAXDIGITS; i++) n->digits[i] = (char)0;
  94.  
  95.         n->lastdigit = -1;
  96.  
  97.         t = abs(s);
  98.  
  99.         while (t > 0) {
  100.             n->lastdigit++;
  101.             n->digits[n->lastdigit] = (t % 10);
  102.             t = t / 10;
  103.         }
  104.  
  105.         if (s == 0) n->lastdigit = 0;
  106.     }
  107.  
  108.     void int_to_bignum(int s)
  109.     {
  110.         int_to_bignum(s, this);
  111.     }
  112.  
  113.     void str_to_bignum(string num)
  114.     {
  115.         for (int i = 0;i < MAXDIGITS; i++) {
  116.             digits[i] = 0;
  117.         }
  118.         if (num[0] != '-') {
  119.             for (auto it = num.crbegin(); it != num.crend(); it++) {
  120.                 digits[it - num.crbegin()] = *it - '0';
  121.             }
  122.             lastdigit = num.size() - 1;
  123.             signbit = PLUS;
  124.         }
  125.         else {
  126.             auto end = num.crend();
  127.             end--;
  128.             for (auto it = num.crbegin(); it != end; it++) {
  129.                 digits[it - num.crbegin()] = *it - '0';
  130.             }
  131.             lastdigit = num.size() - 2;
  132.             signbit = MINUS;
  133.         }
  134.  
  135.     }
  136.  
  137.     void initialize_bignum(BigInt *n)
  138.     {
  139.         int_to_bignum(0, n);
  140.     }
  141.  
  142.     void subtract_bignum(BigInt *a, BigInt *b, BigInt *c)
  143.     {
  144.         int borrow;         /* has anything been borrowed? */
  145.         int v;              /* placeholder digit */
  146.         int i;              /* counter */
  147.  
  148.         initialize_bignum(c);
  149.  
  150.         if ((a->signbit == MINUS) || (b->signbit == MINUS)) {
  151.             b->signbit = -1 * b->signbit;
  152.             add_bignum(a, b, c);
  153.             b->signbit = -1 * b->signbit;
  154.             return;
  155.         }
  156.  
  157.         if (compare_bignum(a, b) == PLUS) {
  158.             subtract_bignum(b, a, c);
  159.             c->signbit = MINUS;
  160.             return;
  161.         }
  162.  
  163.         c->lastdigit = max(a->lastdigit, b->lastdigit);
  164.         borrow = 0;
  165.  
  166.         for (i = 0; i <= (c->lastdigit); i++) {
  167.             v = (a->digits[i] - borrow - b->digits[i]);
  168.             if (a->digits[i] > 0)
  169.                 borrow = 0;
  170.             if (v < 0) {
  171.                 v = v + 10;
  172.                 borrow = 1;
  173.             }
  174.  
  175.             c->digits[i] = (char)v % 10;
  176.         }
  177.  
  178.         zero_justify(c);
  179.     }
  180.  
  181.     int compare_bignum(BigInt *a, BigInt *b)
  182.     {
  183.         int i;
  184.  
  185.         if ((a->signbit == MINUS) && (b->signbit == PLUS)) return(PLUS);
  186.         if ((a->signbit == PLUS) && (b->signbit == MINUS)) return(MINUS);
  187.  
  188.         if (b->lastdigit > a->lastdigit) return (PLUS * a->signbit);
  189.         if (a->lastdigit > b->lastdigit) return (MINUS * a->signbit);
  190.  
  191.         for (i = a->lastdigit; i >= 0; i--) {
  192.             if (a->digits[i] > b->digits[i]) return(MINUS * a->signbit);
  193.             if (b->digits[i] > a->digits[i]) return(PLUS * a->signbit);
  194.         }
  195.  
  196.         return 0;
  197.     }
  198.  
  199.     void zero_justify(BigInt *n)
  200.     {
  201.         while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0))
  202.             n->lastdigit--;
  203.  
  204.         if ((n->lastdigit == 0) && (n->digits[0] == 0))
  205.             n->signbit = PLUS;  /* hack to avoid -0 */
  206.     }
  207.  
  208.     void print_bignum()
  209.     {
  210.         int i;
  211.  
  212.         if (signbit == MINUS) printf("- ");
  213.         for (i = lastdigit; i >= 0; i--)
  214.             printf("%c", '0' + digits[i]);
  215.  
  216.         printf("\n");
  217.     }
  218.  
  219. };
  220.  
  221.  
  222. int main()
  223. {
  224.     BigInt test1;
  225.     test1.str_to_bignum("123456789012345678901234567890");
  226.     test1.print_bignum();
  227.     BigInt test2;
  228.     test2.str_to_bignum("-123456789012345678901234567890");
  229.     test2.print_bignum();
  230.  
  231.     test2 = test2 + test1;
  232.     test2.print_bignum();
  233.  
  234.     setlocale(LC_ALL, "Russian");
  235.     int a, b;
  236.     cout << "Enter a:"; cin >> a;
  237.     cout << "Enter b:"; cin >> b;
  238.     BigInt n1, n2, n3, zero;
  239.  
  240.  
  241.  
  242.     n1.int_to_bignum(a);
  243.     n2.int_to_bignum(b);
  244.  
  245.     n3 = n1 + n2;
  246.     printf("Сочетание= ");
  247.     n3.print_bignum();
  248.  
  249.     n3 = n1 - n2;
  250.     cout << "Вычитание= ";
  251.     n3.print_bignum();
  252.  
  253.     cout << "б меньше а? Ответ: " << (n2 < n1) << endl;
  254.  
  255.     cout << "б больше а? Ответ: " << (n2 > n1) << endl;
  256.  
  257.     cout << "б равно а? Ответ: " << (n2 == n1) << endl;
  258.  
  259.     cout << "Не равно ли б переменной а? Ответ: " << (n2 != n1) << endl;
  260.  
  261.     system("pause");
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement