HomoCivicus

TUSUR 29.05.2019 (2)

May 29th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. // Класс для выполнения операций с 64-битовыми строками
  7. class BitString {
  8. private:
  9.     string bitString;
  10.     unsigned long firstPart = 0;
  11.     unsigned long secondPart = 0;
  12.     int stringLength = 64;
  13.  
  14.     // Перевод битовой 64-строки в два числа типа unsigned long
  15.     void translateStringToNumbers() {
  16.         for (int i = 0; i < stringLength; i++) {
  17.             if (i < stringLength / 2) {
  18.                 firstPart += (bitString[i] - '0') * pow(2, stringLength / 2 - 1 - i);
  19.             }
  20.             else {
  21.                 secondPart += (bitString[i] - '0') * pow(2, stringLength - 1 - i);
  22.             }
  23.         }
  24.     }
  25.  
  26. public:
  27.     // Ввод с клавиатуры
  28.     void read() {
  29.         do {
  30.             cout << "Ввод битовой строки: ";
  31.             cin >> bitString;
  32.         } while (bitString.size() != stringLength);
  33.         cout << "\n";
  34.  
  35.         translateStringToNumbers();
  36.     }
  37.  
  38.     // Операция "И"
  39.     BitString _and(BitString other) {
  40.         BitString newBitString;
  41.         unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
  42.         tmpFirstThis = firstPart;
  43.         tmpSecondThis = secondPart;
  44.         tmpFirstOther = other.firstPart;
  45.         tmpSecondOther = other.secondPart;
  46.  
  47.         for (int i = 0; i < stringLength; i++) {
  48.             if (i < stringLength / 2) {
  49.                 newBitString.firstPart += (tmpFirstThis % 2) * (tmpFirstOther % 2)
  50.                     * pow(2, i);
  51.                 tmpFirstThis /= 2;
  52.                 tmpFirstOther /= 2;
  53.             }
  54.             else {
  55.                 newBitString.secondPart += (tmpSecondThis % 2) * (tmpSecondOther % 2)
  56.                     * pow(2, i - stringLength / 2);
  57.                 tmpSecondThis /= 2;
  58.                 tmpSecondOther /= 2;
  59.             }
  60.         }
  61.  
  62.         return newBitString;
  63.     }
  64.  
  65.     // Операция "ИЛИ"
  66.     BitString _or(BitString other) {
  67.         BitString newBitString;
  68.         unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
  69.         tmpFirstThis = firstPart;
  70.         tmpSecondThis = secondPart;
  71.         tmpFirstOther = other.firstPart;
  72.         tmpSecondOther = other.secondPart;
  73.  
  74.         for (int i = 0; i < stringLength; i++) {
  75.             if (i < stringLength / 2) {
  76.                 newBitString.firstPart +=
  77.                     min((int)((tmpFirstThis % 2) + (tmpFirstOther % 2)), 1)
  78.                     * pow(2, i);
  79.  
  80.                 tmpFirstThis /= 2;
  81.                 tmpFirstOther /= 2;
  82.             }
  83.             else {
  84.                 newBitString.secondPart +=
  85.                     min((int)((tmpSecondThis % 2) + (tmpSecondOther % 2)), 1)
  86.                     * pow(2, i - stringLength / 2);
  87.  
  88.                 tmpSecondThis /= 2;
  89.                 tmpSecondOther /= 2;
  90.             }
  91.         }
  92.  
  93.         return newBitString;
  94.     }
  95.  
  96.     // Операция "Исключающее ИЛИ"
  97.     BitString _xor(BitString other) {
  98.         BitString newBitString;
  99.         unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
  100.         tmpFirstThis = firstPart;
  101.         tmpSecondThis = secondPart;
  102.         tmpFirstOther = other.firstPart;
  103.         tmpSecondOther = other.secondPart;
  104.  
  105.         for (int i = 0; i < stringLength; i++) {
  106.             if (i < stringLength / 2) {
  107.                 newBitString.firstPart +=
  108.                     (abs((int)((tmpFirstThis % 2) - (tmpFirstOther % 2))))
  109.                     * pow(2, i);
  110.  
  111.                 tmpFirstThis /= 2;
  112.                 tmpFirstOther /= 2;
  113.             }
  114.             else {
  115.                 newBitString.secondPart +=
  116.                     (abs((int)((tmpSecondThis % 2) - (tmpSecondOther % 2))))
  117.                     * pow(2, i - stringLength / 2);
  118.  
  119.                 tmpSecondThis /= 2;
  120.                 tmpSecondOther /= 2;
  121.             }
  122.         }
  123.  
  124.         return newBitString;
  125.     }
  126.  
  127.     // Инверсия
  128.     void _not() {
  129.         unsigned long tmpFirst, tmpSecond;
  130.         tmpFirst = firstPart;
  131.         tmpSecond = secondPart;
  132.         firstPart = 0;
  133.         secondPart = 0;
  134.  
  135.         for (int i = 0; i < stringLength; i++) {
  136.             if (i < stringLength / 2) {
  137.                 if (tmpFirst % 2 == 0) {
  138.                     firstPart += pow(2, i);
  139.                 }
  140.  
  141.                 tmpFirst /= 2;
  142.             }
  143.             else {
  144.                 if (tmpSecond % 2 == 0) {
  145.                     secondPart += pow(2, i - stringLength / 2);
  146.                 }
  147.  
  148.                 tmpSecond /= 2;
  149.             }
  150.         }
  151.     }
  152.  
  153.     // Сдвиг влево
  154.     void shiftLeft() {
  155.         unsigned long tmpFirst, tmpSecond;
  156.         tmpFirst = firstPart;
  157.         tmpSecond = secondPart;
  158.         firstPart = 0;
  159.         secondPart = 0;
  160.  
  161.         for (int i = 0; i < stringLength; i++) {
  162.             if (i < stringLength / 2) {
  163.                 if (i != stringLength / 2 - 1) {
  164.                     firstPart += (tmpFirst % 2) * pow(2, i + 1);
  165.                 }
  166.  
  167.                 tmpFirst /= 2;
  168.             }
  169.             else {
  170.                 if (i != stringLength - 1) {
  171.                     secondPart += (tmpSecond % 2) * pow(2, i - stringLength / 2 + 1);
  172.                 }
  173.                 else {
  174.                     firstPart += (tmpSecond % 2);
  175.                 }
  176.                
  177.                 tmpSecond /= 2;
  178.             }
  179.         }
  180.     }
  181.  
  182.     // Сдвиг вправо
  183.     void shiftRight() {
  184.         unsigned long tmpFirst, tmpSecond;
  185.         tmpFirst = firstPart;
  186.         tmpSecond = secondPart;
  187.         firstPart = 0;
  188.         secondPart = 0;
  189.  
  190.         for (int i = 0; i < stringLength; i++) {
  191.             if (i < stringLength / 2) {
  192.                 if (i != 0) {
  193.                     firstPart += (tmpFirst % 2) * pow(2, i - 1);
  194.                 }
  195.                 else {
  196.                     secondPart += (tmpFirst % 2) * pow(2, stringLength / 2 - 1);
  197.                 }
  198.  
  199.                 tmpFirst /= 2;
  200.             }
  201.             else {
  202.                 if (i != stringLength) {
  203.                     secondPart += (tmpSecond % 2) * pow(2, i - stringLength / 2 - 1);
  204.                 }
  205.  
  206.                 tmpSecond /= 2;
  207.             }
  208.         }
  209.     }
  210.  
  211.     // Вывод на экран
  212.     void display() {
  213.         bitString = "";
  214.         unsigned long tmpFirst, tmpSecond;
  215.         tmpFirst = firstPart;
  216.         tmpSecond = secondPart;
  217.  
  218.         // Формирование битовой строки
  219.         for (int i = stringLength - 1; i >= 0; i--) {
  220.             if (i < stringLength / 2) {
  221.                 bitString = to_string(tmpFirst % 2) + bitString;
  222.                 tmpFirst /= 2;
  223.             }
  224.             else {
  225.                 bitString = to_string(tmpSecond % 2) + bitString;
  226.                 tmpSecond /= 2;
  227.             }
  228.         }
  229.  
  230.         cout << "Вывод битовой строки: " << bitString << "\n\n";
  231.     }
  232. };
  233.  
  234. int main() {
  235.     // Пример работы с объектами класса
  236.  
  237.     BitString str1, str2, str3;
  238.    
  239.     str1.read();
  240.     str2.read();
  241.  
  242.     str3 = str1._and(str2);
  243.     str3.display();
  244.  
  245.     str3 = str1._or(str2);
  246.     str3.display();
  247.  
  248.     str3 = str1._xor(str2);
  249.     str3.display();
  250.  
  251.     str3._not();
  252.     str3.display();
  253.  
  254.     str3.shiftLeft();
  255.     str3.display();
  256.  
  257.     str3.shiftRight();
  258.     str3.display();
  259.  
  260.     return 0;
  261. }
Add Comment
Please, Sign In to add comment