special_forces

Битовый массив

May 16th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. Первый файл #include "BitArray2.h"
  2. int main() {
  3.     setlocale (LC_ALL, "Russian");
  4.     char c = 0;
  5.     //  BitArray1 c1();
  6.     BitArray2 c2(33);
  7.     c2.setBit(10);
  8.     c2.getBit(10);
  9.     BitArray2 c1(c2);
  10.     c1.setBit(3);
  11.     c = c2.getBit(10);
  12.     printf("\nЭлемент с2= %d", c);
  13.     c1.setBit(3);
  14.     printf("\nЭлемент с2= %d", c);
  15.     return 0;
  16. }
  17.  
  18. Второй файл (заголовки)  
  19. #pragma once
  20. #include <math.h>
  21. #include <cstdio>
  22. #include <locale.h>
  23. #include <iostream>
  24. //#include "perevodfrom10to2.cpp"
  25. class BitArray2 {
  26. private:
  27.     char N;
  28.     unsigned int* bit;
  29. public:
  30.     BitArray2();
  31.     BitArray2(int N);
  32.     ~BitArray2();
  33.     BitArray2(BitArray2& a);// консруктур копий
  34.     void setBit(int el);
  35.     char getBit(int el);
  36.     BitArray2& operator =(BitArray2& a);
  37. };
  38. файл 3 протоколы
  39. #include "BitArray2.h"
  40. BitArray2::BitArray2() //конструктор 1
  41. {
  42.     bit = NULL;
  43.  
  44.     unsigned int* bit = new unsigned int[1]();
  45.     if (bit == NULL) {
  46.         printf("Ошибка:не выделилась память под массив!");
  47.         exit(-2);
  48.     }
  49.     N = 31;
  50.  
  51.     printf("N=%d", N);
  52.     //  printfBitarray(bit);
  53. }
  54. BitArray2::BitArray2(int N) //конструктор 2
  55. {
  56.     bit = NULL;
  57.     this->N = N - 1;
  58.     int f = N / 32; //
  59.     unsigned int* bit = new unsigned int[f + 1]();
  60.     if (bit == NULL) {
  61.         printf("Ошибка:не выделилась память под массив!");
  62.         exit(-2);
  63.     }
  64.  
  65.     this->N = N;
  66.  
  67.     printf("N=%d\n", N);
  68.     //printfBitarray(bit);
  69. }
  70.  
  71. BitArray2::~BitArray2()
  72. {
  73.     printf("Деструктор");
  74.     delete[] bit;
  75. }
  76. BitArray2::BitArray2(BitArray2& a) //конструктор копии
  77. {
  78.     int f = a.N / 32;
  79.     N = a.N;
  80.     bit = new unsigned int[f + 1];
  81.     for (size_t i = 0; i < f + 1; i++)
  82.         bit[i] = a.bit[i];
  83.     printf("Конструктр копий");
  84. }
  85.  
  86. BitArray2& BitArray2 :: operator =(BitArray2& a) {// оператор копий
  87.     if (&a == this) {
  88.         return *this;
  89.     }
  90.     int f = a.N / 32;
  91.     delete[] bit;
  92.     N = a.N;
  93.     bit = new unsigned int[f + 1];
  94.     for (size_t i = 0; i < f + 1; i++) {
  95.         bit[i] = a.bit[i];
  96.     }
  97.     printf("оператор копий");
  98.     return *this;
  99. }
  100. void BitArray2::setBit(int el) {//
  101.     { //установки значения одного элемента массива (бита) по номеру
  102.         int t = 0;
  103.         int c = 0;
  104.         int f = 0;
  105.         if (el < 0 || el > N)
  106.         {
  107.             printf("3) N=%d\n", N);
  108.             printf("Вы вышли за границу массива3.Выберите снова номер элемента ");
  109.             while (1) //защита от дурака
  110.             {
  111.                 if ((scanf_s("%d", &el) == 1) && (el >= 0 && el < N))
  112.                 {
  113.                     if (getchar() == '\n')
  114.                         break;
  115.                 }
  116.                 while (getchar() != '\n');
  117.                 printf("Ошибка:введите число.\n");
  118.             }
  119.         };
  120.         printf("Введите 1 либо 0\n");
  121.         while (1) //защита от дурака
  122.         {
  123.             if ((scanf_s("%d", &c) == 1) && (c == 0 || c == 1))
  124.             {
  125.                 if (getchar() == '\n')
  126.                     break;
  127.             }
  128.             while (getchar() != '\n');
  129.             printf("Ошибка:введите 1 либо 0.\n");
  130.         }
  131.         f = el / 32; // нужен для выбора нужного индекса
  132.         while (el > 31) {
  133.             el = el - f * 32; //формула нужна,чтобы вставить элемент на нужную позицию
  134.         }
  135.         t = pow(2, el);
  136.  
  137.         if (c == 1) { //вставили единицу
  138.  
  139.             bit[f] = bit[f] | t; //здесь ошибка
  140.         }
  141.         else
  142.         {
  143.             bit[f] = bit[f] & (~t);
  144.         }
  145.     }
  146. }
  147.  
  148. char BitArray2::getBit(int el) { //забрать бит (1 либо 0)
  149.     int t = 0;
  150.     char c = 0;
  151.     int f = 0;
  152.     if (el < 0 || el > N)
  153.     {// -1 так надо для массива
  154.         printf("Вы вышли за границу массива4.Выберите снова номер элемента ");
  155.         while (1) //защита от дурака
  156.         {
  157.             if ((scanf_s("%d", &el) == 1) && (el >= 0 && el < N))
  158.             {
  159.                 if (getchar() == '\n')
  160.                     break;
  161.             }
  162.             while (getchar() != '\n');
  163.             printf("Ошибка:введите число.\n");
  164.         }
  165.     };
  166.  
  167.  
  168.     f = el / 32;
  169.     while (el > 31) {
  170.         el = el - f * 32;
  171.     }
  172.     t = pow(2, el);
  173.     if ((bit[f] & t) != 0)
  174.         c = 1;
  175.     return c;//исправить
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment