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 Shushpanov N.M. Lab1 #228

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions sln/vc10/bitfield/bitfield.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 2 additions & 0 deletions sln/vc10/gtest/gtest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 2 additions & 0 deletions sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 2 additions & 0 deletions sln/vc10/test_bitfield/test_bitfield.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
140 changes: 128 additions & 12 deletions src/tbitfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,91 +8,207 @@
#include "tbitfield.h"

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

TBitField::TBitField(int len)
{
if (len >= 0)
{
BitLen = len;
MemLen = BitLen / (sizeof(TELEM) * 8);
if (BitLen % (sizeof(TELEM) * 8))
MemLen++;
pMem = new TELEM[MemLen];
for (int i = 0; i < MemLen; i++)
pMem[i] = 0;
}
else
throw "Error.";
}

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

TBitField::~TBitField()
{
delete[] pMem;
}

int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n
{
return FAKE_INT;
int index;
if (n < 0 || n > BitLen)
throw "Error.";
index = n / (sizeof(TELEM) * 8);
return index;
//return FAKE_INT;
}

TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n
{
return FAKE_INT;
if (n < 0 || n > BitLen)
throw "Error.";
TELEM mask = 1 << (n % (sizeof(TELEM) * 8));
return mask;
//return FAKE_INT;
}

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

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

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

void TBitField::ClrBit(const int n) // очистить бит
{
TELEM mask = 1;
int index;
if (n < 0 || n > BitLen)
throw "Error.";
mask = GetMemMask(n);
index = GetMemIndex(n);
pMem[index] &= ~mask;
}

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

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

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

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

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

TBitField TBitField::operator|(const TBitField &bf) // операция "или"
{
return FAKE_BITFIELD;
int minMemLen, len, temp = 1;
if (BitLen < bf.BitLen)
temp = 0;
if (temp == 1)
{
len = BitLen;
minMemLen = bf.MemLen;
}
else
{
len = bf.BitLen;
minMemLen = MemLen;
}
TBitField result(len);
for (int i = 0; i < minMemLen; i++)
result.pMem[i] = pMem[i] | bf.pMem[i];
if (temp == 1)
for (int i = minMemLen; i < result.MemLen; i++)
result.pMem[i] = pMem[i];
else
for (int i = minMemLen; i < result.MemLen; i++)
result.pMem[i] = bf.pMem[i];
return result;
//return FAKE_BITFIELD;
}

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

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

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

istream &operator>>(istream &istr, TBitField &bf) // ввод
{
int temp;
cin >> temp;
while (temp > -1)
{
bf.SetBit(temp);
cin >> temp;
}
return istr;
}

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