Advertisement
Toliak

lab8

Nov 23rd, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <cassert>
  5.  
  6. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  7.  
  8. struct BigUInt192
  9. {
  10.     using baseType = uint16_t;
  11.     using oversizedType = uint32_t;
  12.  
  13.     BigUInt192()
  14.     {
  15.         parts = new baseType[arraySize];
  16.         this->erase();
  17.     }
  18.     void erase()
  19.     {
  20.         std::memset(parts, 0, sizeof(baseType) * arraySize);
  21.     }
  22.  
  23.     ~BigUInt192()
  24.     {
  25.         delete[] parts;
  26.     }
  27.  
  28.     static const size_t bytes = 24;
  29.     static const size_t arraySize = bytes / sizeof(baseType) + (bytes % sizeof(baseType) > 0);
  30.  
  31.     baseType *parts;
  32. };
  33.  
  34. BigUInt192 operator+(const BigUInt192 &left, const BigUInt192 &right)
  35. {
  36.     BigUInt192 result;
  37.     BigUInt192::oversizedType last = 0;
  38.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  39.         BigUInt192::oversizedType partSum = left.parts[i] + right.parts[i] + last;
  40.         if (partSum >> 16 != 0) {
  41.             last = 1;           // Переносим бит из последнего разряда (17й)
  42.         } else {
  43.             last = 0;
  44.         }
  45.         result.parts[i] = static_cast<BigUInt192::baseType>(partSum);
  46.     }
  47.     return result;
  48. }
  49.  
  50. BigUInt192 operator-(const BigUInt192 &left, const BigUInt192 &right)
  51. {
  52.     BigUInt192 result;
  53.     bool last = false;
  54.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  55.         const BigUInt192::oversizedType toSub = right.parts[i] + last;
  56.         BigUInt192::oversizedType partSub;
  57.         if (left.parts[i] >= toSub) {
  58.             partSub = left.parts[i] - toSub;
  59.             last = false;
  60.         } else {
  61.             partSub = (1 << 8 * sizeof(BigUInt192::baseType)) + left.parts[i];
  62.             partSub -= right.parts[i];
  63.             last = true;
  64.         }
  65.         result.parts[i] = static_cast<BigUInt192::baseType>(partSub);
  66.     }
  67.     return result;
  68. }
  69.  
  70. bool operator>(const BigUInt192 &left, const BigUInt192 &right)
  71. {
  72.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  73.         const size_t index = BigUInt192::arraySize - i - 1;
  74.         if (left.parts[index] == right.parts[index])
  75.             continue;
  76.         return left.parts[index] > right.parts[index];
  77.     }
  78.     return false;
  79. }
  80.  
  81. bool operator==(const BigUInt192 &left, const BigUInt192 &right)
  82. {
  83.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  84.         if (left.parts[i] != right.parts[i])
  85.             return false;
  86.     }
  87.     return true;
  88. }
  89.  
  90. bool operator!=(const BigUInt192 &left, const BigUInt192 &right)
  91. {
  92.     return !(left == right);
  93. }
  94.  
  95. bool operator<(const BigUInt192 &left, const BigUInt192 &right)
  96. {
  97.     return !(left > right) && left != right;
  98. }
  99.  
  100. bool operator<=(const BigUInt192 &left, const BigUInt192 &right)
  101. {
  102.     return left < right || left == right;
  103. }
  104.  
  105. bool operator>=(const BigUInt192 &left, const BigUInt192 &right)
  106. {
  107.     return left > right || left == right;
  108. }
  109.  
  110. BigUInt192 operator&(const BigUInt192 &left, const BigUInt192 &right)
  111. {
  112.     BigUInt192 result;
  113.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  114.         result.parts[i] = left.parts[i] & right.parts[i];
  115.     }
  116.     return result;
  117. }
  118.  
  119. BigUInt192 operator|(const BigUInt192 &left, const BigUInt192 &right)
  120. {
  121.     BigUInt192 result;
  122.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  123.         result.parts[i] = left.parts[i] | right.parts[i];
  124.     }
  125.     return result;
  126. }
  127.  
  128. BigUInt192 operator^(const BigUInt192 &left, const BigUInt192 &right)
  129. {
  130.     BigUInt192 result;
  131.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  132.         result.parts[i] = left.parts[i] ^ right.parts[i];
  133.     }
  134.     return result;
  135. }
  136.  
  137. BigUInt192 operator~(const BigUInt192 &left)
  138. {
  139.     BigUInt192 result;
  140.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  141.         result.parts[i] = ~left.parts[i];
  142.     }
  143.     return result;
  144. }
  145.  
  146. BigUInt192 operator<<(const BigUInt192 &left, size_t shift)
  147. {
  148.     BigUInt192::baseType mask = 0x0;
  149.     for (size_t i = 0; i < shift; i++) {
  150.         mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
  151.     }
  152.  
  153.     BigUInt192 result;
  154.     BigUInt192::baseType last = 0;
  155.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  156.         result.parts[i] = (left.parts[i] << shift) | last;
  157.         last = (left.parts[i] >> (8 * sizeof(BigUInt192::baseType) - shift)) & mask;
  158.     }
  159.     return result;
  160. }
  161.  
  162. BigUInt192 operator>>(const BigUInt192 &left, size_t shift)
  163. {
  164.     BigUInt192::baseType mask = 0x0;
  165.     for (size_t i = 0; i < shift; i++) {
  166.         mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
  167.     }
  168.  
  169.     BigUInt192 result;
  170.     BigUInt192::baseType last = 0;
  171.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  172.         const size_t index = BigUInt192::arraySize - i - 1;
  173.         result.parts[index] = (left.parts[index] >> shift);
  174.         result.parts[index] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
  175.         last = left.parts[index] & mask;
  176.     }
  177.     return result;
  178. }
  179.  
  180. BigUInt192 circledShiftL(const BigUInt192 &left, size_t shift)
  181. {
  182.     BigUInt192::baseType mask = 0x0;
  183.     for (size_t i = 0; i < shift; i++) {
  184.         mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
  185.     }
  186.  
  187.     BigUInt192 result;
  188.     BigUInt192::baseType last = 0;
  189.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  190.         result.parts[i] = (left.parts[i] << shift) | last;
  191.         last = (left.parts[i] >> (8 * sizeof(BigUInt192::baseType) - shift)) & mask;
  192.     }
  193.     result.parts[0] |= last;
  194.     return result;
  195. }
  196.  
  197. BigUInt192 circledShiftR(const BigUInt192 &left, size_t shift)
  198. {
  199.     BigUInt192::baseType mask = 0x0;
  200.     for (size_t i = 0; i < shift; i++) {
  201.         mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
  202.     }
  203.  
  204.     BigUInt192 result;
  205.     BigUInt192::baseType last = 0;
  206.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  207.         const size_t index = BigUInt192::arraySize - i - 1;
  208.         result.parts[index] = (left.parts[index] >> shift);
  209.         result.parts[index] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
  210.         last = left.parts[index] & mask;
  211.     }
  212.     result.parts[BigUInt192::arraySize - 1] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
  213.     return result;
  214. }
  215.  
  216. std::istream &operator>>(std::istream &istream, BigUInt192 &value)
  217. {
  218.     std::string s;
  219.     istream >> s;
  220.     value.erase();
  221.  
  222.     for (auto it = s.crbegin(); it != s.crend(); it++) {
  223.         const size_t i = it - s.crbegin();
  224.         assert(*it == '0' || *it == '1');
  225.         if (*it == '0')
  226.             continue;
  227.  
  228.         const size_t index = i / (sizeof(BigUInt192::baseType) * 8);
  229.         const size_t shift = i % (sizeof(BigUInt192::baseType) * 8);
  230.         value.parts[index] |= (*it - '0') << shift;
  231.     }
  232.  
  233.     return istream;
  234. }
  235.  
  236. std::ostream &operator<<(std::ostream &ostream, const BigUInt192 &value)
  237. {
  238.     bool numberStarted = false;
  239.     for (size_t i = 0; i < BigUInt192::arraySize; i++) {
  240.         const size_t index = BigUInt192::arraySize - i - 1;
  241.         const size_t bits = sizeof(BigUInt192::baseType) * 8;
  242.         for (int j = 0; j < bits; j++) {
  243.             const size_t shift = bits - j - 1;
  244.             auto numeral = (value.parts[index] >> shift) & 0x1;
  245.             if ((numeral == 0 && numberStarted) || numeral == 1)
  246.                 ostream << numeral;
  247.             if (numeral == 1 && !numberStarted)
  248.                 numberStarted = true;
  249.         }
  250.     }
  251.  
  252.     return ostream;
  253. }
  254.  
  255. int main()
  256. {
  257.  
  258.     BigUInt192 b;
  259.  
  260.     std::cout << (uint16_t)(12-32) << std::endl;
  261.  
  262.     BigUInt192 v;
  263.     //std::cout << "Enter BIN big value: ";
  264.     std::cin >> v;
  265.     std::cin >> b;
  266.     std::cout << circledShiftR(v, 5) << std::endl;
  267.     std::cout << circledShiftL(v, 5) << std::endl;
  268.     std::cout << v + b << std::endl;
  269.     std::cout << v - b << std::endl;
  270.     std::cout << (v & b) << std::endl;
  271.     std::cout << (v | b) << std::endl;
  272.     std::cout << (v > b) << std::endl;
  273.     std::cout << "Hello, World!" << std::endl;
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement