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

Lab is done (Руяткин Илья ПМИ3) #216

Open
wants to merge 3 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
209 changes: 173 additions & 36 deletions src/tbitfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,86 +13,223 @@ static TBitField FAKE_BITFIELD(1);

TBitField::TBitField(int len)
{
}

TBitField::TBitField(const TBitField &bf) // конструктор копирования
{
if (len > -1)
{
BitLen = len;
MemLen = len * sizeof(TELEM) * 8;
pMem = new TELEM[MemLen];
if (pMem != NULL)
{
for (int i = 0; i < MemLen; i++)
pMem[i] = 0;
}
}
else
{
throw "error";
}
}

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

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

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

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

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

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

void TBitField::SetBit(const int n) // установить бит
{
if ((n < 0) || (n > BitLen))
{
throw "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 "error";
}
else
{
int i = GetMemIndex(n);
int m = GetMemMask(n);
pMem[i] = pMem[i] & ~m;
}
}

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

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

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=(const TBitField& bf) // присваивание
{
if (this == &bf)
return *this;
else
{
delete[] pMem;
MemLen = bf.MemLen;
pMem = new TELEM[MemLen];
BitLen = bf.BitLen;
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 // сравнение
{
if (MemLen != bf.MemLen || BitLen != bf.BitLen)
return 1;
else
{
for (int i = 0; i < MemLen; i++)
{
if (pMem[i] != bf.pMem[i])
return 1;
}
return 0;
}
}

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

TBitField TBitField::operator&(const TBitField& bf) // операция "и"
{
if (bf.BitLen > BitLen)
BitLen = bf.BitLen;
TBitField res = (BitLen);
for (int i = 0; i < MemLen; i++)
res.pMem[i] = pMem[i];
for (int i = 0; i < bf.MemLen; i++)
res.pMem[i] &= bf.pMem[i];
return res;
}

TBitField TBitField::operator~(void) // отрицание
{
return FAKE_BITFIELD;
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) // ввод
istream& operator>>(istream& istr, TBitField& bf) // ввод
{
return istr;
int tmp;
for (int i = 0; i < bf.GetLength(); i++)
{
cin >> tmp;
if (tmp == 1)
bf.SetBit(i);
else
if (tmp == 0)
bf.ClrBit(i);
}
return istr;
}

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

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