Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3821Б1ПМ3 Заботин Максим #202

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 205 additions & 98 deletions src/tbitfield.cpp
Original file line number Diff line number Diff line change
@@ -1,98 +1,205 @@
// ННГУ, ВМК, Курс "Методы программирования-2", С++, ООП
//
// tbitfield.cpp - Copyright (c) Гергель В.П. 07.05.2001
// Переработано для Microsoft Visual Studio 2008 Сысоевым А.В. (19.04.2015)
//
// Битовое поле

#include "tbitfield.h"

// Fake variables used as placeholders in tests
static const int FAKE_INT = -1;
static TBitField FAKE_BITFIELD(1);

TBitField::TBitField(int len)
{
}

TBitField::TBitField(const TBitField &bf) // конструктор копирования
{
}

TBitField::~TBitField()
{
}

int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n
{
return FAKE_INT;
}

TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n
{
return FAKE_INT;
}

// доступ к битам битового поля

int TBitField::GetLength(void) const // получить длину (к-во битов)
{
return FAKE_INT;
}

void TBitField::SetBit(const int n) // установить бит
{
}

void TBitField::ClrBit(const int n) // очистить бит
{
}

int TBitField::GetBit(const int n) const // получить значение бита
{
return FAKE_INT;
}

// битовые операции

TBitField& TBitField::operator=(const TBitField &bf) // присваивание
{
return FAKE_BITFIELD;
}

int TBitField::operator==(const TBitField &bf) const // сравнение
{
return FAKE_INT;
}

int TBitField::operator!=(const TBitField &bf) const // сравнение
{
return FAKE_INT;
}

TBitField TBitField::operator|(const TBitField &bf) // операция "или"
{
return FAKE_BITFIELD;
}

TBitField TBitField::operator&(const TBitField &bf) // операция "и"
{
return FAKE_BITFIELD;
}

TBitField TBitField::operator~(void) // отрицание
{
return FAKE_BITFIELD;
}

// ввод/вывод

istream &operator>>(istream &istr, TBitField &bf) // ввод
{
return istr;
}

ostream &operator<<(ostream &ostr, const TBitField &bf) // вывод
{
return ostr;
}

#include "tbitfield.h"

// Fake variables used as placeholders in tests
static const int FAKE_INT = -1;
static TBitField FAKE_BITFIELD(1);

TBitField::TBitField(int len)
{
if (len > 0)
{
memLen = (len + 31) >> sizeof(TELEM);
pMem = new TELEM[memLen];
bitLen = len;
for (int i = 0; i < memLen; i++)
{
pMem[i] = 0;
}
}
else
throw "error";
}

TBitField::TBitField(const TBitField &bf) // конструктор копирования
{
if (bf.bitLen >= 0)
{
memLen = bf.memLen;
pMem = new TELEM[memLen];
bitLen = bf.bitLen;
for (int i = 0; i < memLen; i++)
{
pMem[i] = bf.pMem[i];
}
}
else
throw "error";

}

TBitField::~TBitField()
{
if (pMem != nullptr)
{
delete[] pMem;
pMem = 0;
}
}

int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n
{
if ((n < 0) || (n > itLen))
{
throw "error";
}
else
return n>>5;
}

TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n
{
if (n < 0 && n < bitLen)
throw "error";
return 1 << (n & 31);
}

// доступ к битам битового поля

int TBitField::GetLength(void) const // получить длину (к-во битов)
{
return bitLen;
}

void TBitField::SetBit(const int n) // установить бит
{
if ((n<0) || (n>bitLen))
throw out_of_range("Error");
else
{
int i = GetMemIndex(n);
int m = GetMemMask(n);
pMem[i] = pMem[i] | m;
}
}

void TBitField::ClrBit(const int n) // очистить бит
{
if ((n<0) || (n>bitLen))
throw out_of_range("Error");
else
{
int i = GetMemIndex(n);
int m = GetMemMask(n);
pMem[i] = pMem[i] & ~m;
}
}

int TBitField::GetBit(const int n) const // получить значение бита
{
if ((n<0) || (n>bitLen))
throw out_of_range("Error");
else
{
int i = GetMemIndex(n);
int m = GetMemMask(n);
return (pMem[i] & m);
}
}

// битовые операции

TBitField& TBitField::operator=(const TBitField &bf) // присваивание
{
if (this == &bf)
return *this;
else
{
delete[] pMem;
memLen = bf.memLen;
bitLen = bf.bitLen;
pMem = new TELEM[memLen];
for (int i = 0; i < memLen; i++)
pMem[i] = bf.pMem[i];
return *this;
}

}

int TBitField::operator==(const TBitField &bf) const // сравнение
{
if (memLen != bf.memLen || bitLen != bf.bitLen)
return 0;
else
for (int i = 0; i < memLen; i++)
if (pMem[i] != bf.pMem[i])
return 0;
return 1;
}

int TBitField::operator!=(const TBitField &bf) const // сравнение
{
return !(*this==bf);
}

TBitField TBitField::operator|(const TBitField &bf) // операция "или"
{
const int length = max(bitLen, bf.bitLen);
TBitField res(length);
int i = 0;
for (i; i < memLen; i++)
res.pMem[i] = pMem[i];
for (i=0; i < bf.memLen; i++)
res.pMem[i] = res.pMem[i] | bf.pMem[i];
return res;
}

TBitField TBitField::operator&(const TBitField &bf) // операция "и"
{
const int length = max(bitLen, bf.bitLen);
TBitField res(length);
int i = 0;
for (i; i < memLen; i++)
res.pMem[i] = pMem[i];
for (i = 0; i < bf.memLen; i++)
res.pMem[i] = res.pMem[i] & bf.pMem[i];
return res;
}

TBitField TBitField::operator~(void) // отрицание
{
TBitField res(*this);
for (int i = 0; i < res.bitLen; i++)
{
if (res.GetBit(i))
res.ClrBit(i);
else
res.SetBit(i);
}
return res;
}

// ввод/вывод

istream &operator>>(istream &istr, TBitField &bf) // ввод
{
int length;
istr >> length;
TBitField res(length);
char* mas=new char[length];
istr >> mas;
for (int i = 0; i < bf.bitLen; i++)
if (mas[i] == 0)
bf.ClrBit(i);
else if (mas[i] == 1)
bf.SetBit(i);
else throw "Special Symvol";
return istr;
}

ostream &operator<<(ostream &ostr, const TBitField &bf) // вывод
{
for (int i = 0; i < bf.bitLen; i++)
ostr << bf.GetBit(i);
return ostr;
}
Loading