Skip to content

Commit

Permalink
Merge pull request #1 from kmuradoff/mine
Browse files Browse the repository at this point in the history
Первая Лабораторная работа
  • Loading branch information
kmuradoff authored Oct 26, 2023
2 parents 4123da5 + b115b5b commit 851265c
Showing 7 changed files with 191 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.8)

set(PROJECT_NAME set)
set(PROJECT_NAME kmuradoffLab)
project(${PROJECT_NAME})

# TODO(Korniakov): not sure if these lines are needed
3 changes: 3 additions & 0 deletions sln/vc10/bitfield/bitfield.vcxproj
Original file line number Diff line number Diff line change
@@ -20,10 +20,13 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>

</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
2 changes: 2 additions & 0 deletions sln/vc10/gtest/gtest.vcxproj
Original file line number Diff line number Diff line change
@@ -20,10 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
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
@@ -20,10 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
2 changes: 2 additions & 0 deletions sln/vc10/test_bitfield/test_bitfield.vcxproj
Original file line number Diff line number Diff line change
@@ -20,10 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
139 changes: 122 additions & 17 deletions src/tbitfield.cpp
Original file line number Diff line number Diff line change
@@ -7,92 +7,197 @@

#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(int len): BitLen(len)
{
if (len < 0)
throw ("Invalid len parameter");
BitLen = len;
if (len % (sizeof(TELEM) * 8) == 0)
MemLen = len / (sizeof(TELEM) * 8);
else
MemLen = (len / (sizeof(TELEM) * 8)) + 1;
pMem = new TELEM[MemLen];
for (int i = 0; i < MemLen; i++)
pMem[i] = 0;
}

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

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

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

TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n
{
return FAKE_INT;
if ((n <= -1) || (n > BitLen))
throw "Не могу установить бит";
return 1 << (n % (sizeof(TELEM) * 8));
}

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

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

void TBitField::SetBit(const int n) // установить бит
{
if ((n <= -1) || (n > BitLen))
throw "Не могу установить бит";
pMem[GetMemIndex(n)] |= GetMemMask(n);
}

void TBitField::ClrBit(const int n) // очистить бит
{
if ((n <= -1) || (n > BitLen))
throw "Не могу установить бит";
pMem[GetMemIndex(n)] &= ~GetMemMask(n);
}

int TBitField::GetBit(const int n) const // получить значение бита
{
return FAKE_INT;
if ((n <= -1) || (n > BitLen))
throw "Не могу установить бит";
return (pMem[GetMemIndex(n)] & GetMemMask(n));
}

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

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

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

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

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

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

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

for (int i = 0; i < temp.BitLen; i++) {
if (temp.GetBit(i))
temp.ClrBit(i);
else
temp.SetBit(i);
}
return temp;
}

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

istream &operator>>(istream &istr, TBitField &bf) // ввод
{
return istr;
int i = 0;
char ch;
do {
istr >> ch;
} while (ch != ' ');
while (1) {
istr >> ch;
if (ch == '0')
bf.ClrBit(i++);
else if (ch == '1')
bf.SetBit(i++);
else break;
}
return istr;
}

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

0 comments on commit 851265c

Please sign in to comment.