diff --git a/sln/vc10/bitfield/bitfield.vcxproj b/sln/vc10/bitfield/bitfield.vcxproj
index f4b10eb18..c57b0e3be 100644
--- a/sln/vc10/bitfield/bitfield.vcxproj
+++ b/sln/vc10/bitfield/bitfield.vcxproj
@@ -20,10 +20,12 @@
StaticLibrary
Unicode
true
+ v143
StaticLibrary
Unicode
+ v143
diff --git a/sln/vc10/gtest/gtest.vcxproj b/sln/vc10/gtest/gtest.vcxproj
index 518510409..5667fe05e 100644
--- a/sln/vc10/gtest/gtest.vcxproj
+++ b/sln/vc10/gtest/gtest.vcxproj
@@ -20,10 +20,12 @@
StaticLibrary
Unicode
true
+ v143
StaticLibrary
Unicode
+ v143
diff --git a/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj b/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj
index 981eb64a1..c057a3163 100644
--- a/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj
+++ b/sln/vc10/sample_prime_numbers/sample_prime_numbers.vcxproj
@@ -20,10 +20,12 @@
Application
Unicode
true
+ v143
Application
Unicode
+ v143
diff --git a/sln/vc10/test_bitfield/test_bitfield.vcxproj b/sln/vc10/test_bitfield/test_bitfield.vcxproj
index 5b72614b8..9d59323c9 100644
--- a/sln/vc10/test_bitfield/test_bitfield.vcxproj
+++ b/sln/vc10/test_bitfield/test_bitfield.vcxproj
@@ -20,10 +20,12 @@
Application
Unicode
true
+ v143
Application
Unicode
+ v143
diff --git a/src/tbitfield.cpp b/src/tbitfield.cpp
index 3bfa48182..c9d53c3f2 100644
--- a/src/tbitfield.cpp
+++ b/src/tbitfield.cpp
@@ -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;
}
diff --git a/src/tset.cpp b/src/tset.cpp
index e70a76e58..b6e80db60 100644
--- a/src/tset.cpp
+++ b/src/tset.cpp
@@ -8,97 +8,131 @@
#include "tset.h"
// Fake variables used as placeholders in tests
-static const int FAKE_INT = -1;
-static TBitField FAKE_BITFIELD(1);
-static TSet FAKE_SET(1);
+//static const int FAKE_INT = -1;
+//static TBitField FAKE_BITFIELD(1);
+//static TSet FAKE_SET(1);
-TSet::TSet(int mp) : BitField(-1)
+TSet::TSet(int mp) : BitField(mp), MaxPower(mp)
{
}
// конструктор копирования
-TSet::TSet(const TSet &s) : BitField(-1)
+TSet::TSet(const TSet& s) : BitField(s.BitField), MaxPower(s.MaxPower)
{
}
// конструктор преобразования типа
-TSet::TSet(const TBitField &bf) : BitField(-1)
+TSet::TSet(const TBitField& bf) : BitField(bf), MaxPower(bf.GetLength())
{
}
TSet::operator TBitField()
{
- return FAKE_BITFIELD;
+ return BitField;
}
int TSet::GetMaxPower(void) const // получить макс. к-во эл-тов
{
- return FAKE_INT;
+ return MaxPower;
}
int TSet::IsMember(const int Elem) const // элемент множества?
{
- return FAKE_INT;
+ return BitField.GetBit(Elem);
}
void TSet::InsElem(const int Elem) // включение элемента множества
{
+ BitField.SetBit(Elem);
}
void TSet::DelElem(const int Elem) // исключение элемента множества
{
+ BitField.ClrBit(Elem);
}
// теоретико-множественные операции
-TSet& TSet::operator=(const TSet &s) // присваивание
+TSet& TSet::operator=(const TSet& s) // присваивание
{
- return FAKE_SET;
+ MaxPower = s.MaxPower;
+ BitField = s.BitField;
+ return *this;
}
-int TSet::operator==(const TSet &s) const // сравнение
+int TSet::operator==(const TSet& s) const // сравнение
{
- return FAKE_INT;
+ if (BitField == s.BitField)
+ return 1;
+ else
+ return 0;
}
-int TSet::operator!=(const TSet &s) const // сравнение
+int TSet::operator!=(const TSet& s) const // сравнение
{
- return FAKE_INT;
+ if (BitField != s.BitField)
+ return 1;
+ else
+ return 0;
}
-TSet TSet::operator+(const TSet &s) // объединение
+TSet TSet::operator+(const TSet& s) // объединение
{
- return FAKE_SET;
+ TSet result(BitField | s.BitField);
+ return result;
}
TSet TSet::operator+(const int Elem) // объединение с элементом
{
- return FAKE_SET;
+ if (Elem > 0 || Elem < MaxPower)
+ {
+ TSet result = *this;
+ result.InsElem(Elem);
+ return result;
+ }
+ else
+ throw "Error.";
}
TSet TSet::operator-(const int Elem) // разность с элементом
{
- return FAKE_SET;
+ if (Elem > 0 || Elem < MaxPower)
+ {
+ TSet result = *this;
+ result.DelElem(Elem);
+ return result;
+ }
+ else
+ throw "Error.";
}
-TSet TSet::operator*(const TSet &s) // пересечение
+TSet TSet::operator*(const TSet& s) // пересечение
{
- return FAKE_SET;
+ TSet result(BitField & s.BitField);
+ return result;
}
TSet TSet::operator~(void) // дополнение
{
- return FAKE_SET;
+ TSet result = *this;
+ result.BitField = ~BitField;
+ return result;
}
// перегрузка ввода/вывода
-istream &operator>>(istream &istr, TSet &s) // ввод
+istream& operator>>(istream& istr, TSet& s) // ввод
{
+ int i;
+ cin >> i;
+ for (; i > -1; cin >> i)
+ s.BitField.SetBit(i);
return istr;
}
-ostream& operator<<(ostream &ostr, const TSet &s) // вывод
+ostream& operator<<(ostream& ostr, const TSet& s) // вывод
{
+ for (int i = 0; i < s.MaxPower; i++)
+ cout << s.BitField.GetBit(i);
return ostr;
-}
+}
\ No newline at end of file