Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Первый файл #include "BitArray2.h"
- int main() {
- setlocale (LC_ALL, "Russian");
- char c = 0;
- // BitArray1 c1();
- BitArray2 c2(33);
- c2.setBit(10);
- c2.getBit(10);
- BitArray2 c1(c2);
- c1.setBit(3);
- c = c2.getBit(10);
- printf("\nЭлемент с2= %d", c);
- c1.setBit(3);
- printf("\nЭлемент с2= %d", c);
- return 0;
- }
- Второй файл (заголовки)
- #pragma once
- #include <math.h>
- #include <cstdio>
- #include <locale.h>
- #include <iostream>
- //#include "perevodfrom10to2.cpp"
- class BitArray2 {
- private:
- char N;
- unsigned int* bit;
- public:
- BitArray2();
- BitArray2(int N);
- ~BitArray2();
- BitArray2(BitArray2& a);// консруктур копий
- void setBit(int el);
- char getBit(int el);
- BitArray2& operator =(BitArray2& a);
- };
- файл 3 протоколы
- #include "BitArray2.h"
- BitArray2::BitArray2() //конструктор 1
- {
- bit = NULL;
- unsigned int* bit = new unsigned int[1]();
- if (bit == NULL) {
- printf("Ошибка:не выделилась память под массив!");
- exit(-2);
- }
- N = 31;
- printf("N=%d", N);
- // printfBitarray(bit);
- }
- BitArray2::BitArray2(int N) //конструктор 2
- {
- bit = NULL;
- this->N = N - 1;
- int f = N / 32; //
- unsigned int* bit = new unsigned int[f + 1]();
- if (bit == NULL) {
- printf("Ошибка:не выделилась память под массив!");
- exit(-2);
- }
- this->N = N;
- printf("N=%d\n", N);
- //printfBitarray(bit);
- }
- BitArray2::~BitArray2()
- {
- printf("Деструктор");
- delete[] bit;
- }
- BitArray2::BitArray2(BitArray2& a) //конструктор копии
- {
- int f = a.N / 32;
- N = a.N;
- bit = new unsigned int[f + 1];
- for (size_t i = 0; i < f + 1; i++)
- bit[i] = a.bit[i];
- printf("Конструктр копий");
- }
- BitArray2& BitArray2 :: operator =(BitArray2& a) {// оператор копий
- if (&a == this) {
- return *this;
- }
- int f = a.N / 32;
- delete[] bit;
- N = a.N;
- bit = new unsigned int[f + 1];
- for (size_t i = 0; i < f + 1; i++) {
- bit[i] = a.bit[i];
- }
- printf("оператор копий");
- return *this;
- }
- void BitArray2::setBit(int el) {//
- { //установки значения одного элемента массива (бита) по номеру
- int t = 0;
- int c = 0;
- int f = 0;
- if (el < 0 || el > N)
- {
- printf("3) N=%d\n", N);
- printf("Вы вышли за границу массива3.Выберите снова номер элемента ");
- while (1) //защита от дурака
- {
- if ((scanf_s("%d", &el) == 1) && (el >= 0 && el < N))
- {
- if (getchar() == '\n')
- break;
- }
- while (getchar() != '\n');
- printf("Ошибка:введите число.\n");
- }
- };
- printf("Введите 1 либо 0\n");
- while (1) //защита от дурака
- {
- if ((scanf_s("%d", &c) == 1) && (c == 0 || c == 1))
- {
- if (getchar() == '\n')
- break;
- }
- while (getchar() != '\n');
- printf("Ошибка:введите 1 либо 0.\n");
- }
- f = el / 32; // нужен для выбора нужного индекса
- while (el > 31) {
- el = el - f * 32; //формула нужна,чтобы вставить элемент на нужную позицию
- }
- t = pow(2, el);
- if (c == 1) { //вставили единицу
- bit[f] = bit[f] | t; //здесь ошибка
- }
- else
- {
- bit[f] = bit[f] & (~t);
- }
- }
- }
- char BitArray2::getBit(int el) { //забрать бит (1 либо 0)
- int t = 0;
- char c = 0;
- int f = 0;
- if (el < 0 || el > N)
- {// -1 так надо для массива
- printf("Вы вышли за границу массива4.Выберите снова номер элемента ");
- while (1) //защита от дурака
- {
- if ((scanf_s("%d", &el) == 1) && (el >= 0 && el < N))
- {
- if (getchar() == '\n')
- break;
- }
- while (getchar() != '\n');
- printf("Ошибка:введите число.\n");
- }
- };
- f = el / 32;
- while (el > 31) {
- el = el - f * 32;
- }
- t = pow(2, el);
- if ((bit[f] & t) != 0)
- c = 1;
- return c;//исправить
- }
Advertisement
Add Comment
Please, Sign In to add comment