From c102a4d30be49ce79e2c4043e85c3ba2d73d533d Mon Sep 17 00:00:00 2001 From: Andrey Karagod Date: Tue, 20 Sep 2022 10:36:05 +0300 Subject: [PATCH 1/3] ) --- src/tbitfield.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 3 deletions(-) diff --git a/src/tbitfield.cpp b/src/tbitfield.cpp index 3bfa48182..4e43c5c66 100644 --- a/src/tbitfield.cpp +++ b/src/tbitfield.cpp @@ -13,23 +13,69 @@ static TBitField FAKE_BITFIELD(1); TBitField::TBitField(int len) { + if (len <= 0) + { + throw "init error"; + } + else + { + BitLen = sizeof(int) * 8 * len; + MemLen = len; + pMem = new TELEM[MemLen]; + for (int i = 0; i < len; 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 != nullptr) + { + delete[] pMem; + Bitlen = 0; + MemLen = 0; + pMem = nullptr; + } } + + int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n { + if ((n >= 0) && (n < BitLen)) + { + return n >> 5; + } + else + { + throw "error"; + } return FAKE_INT; } TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n { + if ((n >= 0) && (n < BitLen)) + { + return 1 << (n & 31); + } + else + { + throw "error"; + } return FAKE_INT; } @@ -37,19 +83,51 @@ TELEM TBitField::GetMemMask(const int n) const // битовая маска дл int TBitField::GetLength(void) const // получить длину (к-во битов) { - return FAKE_INT; + return MemLen; } void TBitField::SetBit(const int n) // установить бит { + if ((n >= 0) && (n < BitLen)) + { + int i = GetMemIndex(n); + int m = GetMemMask(n); + pMem[i] = pMem[i] | m; + } + else + { + throw "error index"; + } } void TBitField::ClrBit(const int n) // очистить бит { + if ((n >= 0) && (n < BitLen)) + { + int i = GetMemIndex(n); + + int m = GetMemMask(n); + + pMem[i] = pMem[i] & ~m; + } + else + { + throw "error index"; + } + } int TBitField::GetBit(const int n) const // получить значение бита { + if ((n >= 0) && (n < BitLen)) + { + int i = GetMemIndex(n); + int m = GetMemMask(n); + return (pMem[i] & m) == 0 ? 0 : 1; + } + else { + throw "Error GetBit"; + } return FAKE_INT; } @@ -57,12 +135,42 @@ int TBitField::GetBit(const int n) const // получить значение б TBitField& TBitField::operator=(const TBitField &bf) // присваивание { - return FAKE_BITFIELD; + if (bf.pMem == pMem) + { + return *this; + } + else + { + delete[] pMem; + BitLen = bf.BitLen; + MemLen = bf.MemLen; + 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; + if (bf.MemLen != pMem) + { + return false; + } + else + { + for (int i = 0; i < MemLen; i++) + { + if (pMem[i] != bf.pMem[i]) + { + return false; + } + } + return true; + } } int TBitField::operator!=(const TBitField &bf) const // сравнение From 82da5b52407f91f8d543a1007e7c94774741657e Mon Sep 17 00:00:00 2001 From: dmitr1yR <104106501+dmitr1yR@users.noreply.github.com> Date: Sun, 16 Oct 2022 22:44:43 +0300 Subject: [PATCH 2/3] Update tbitfield.cpp . --- src/tbitfield.cpp | 260 +++++++++++++++++++++++++--------------------- 1 file changed, 144 insertions(+), 116 deletions(-) diff --git a/src/tbitfield.cpp b/src/tbitfield.cpp index 4e43c5c66..22b22ecdd 100644 --- a/src/tbitfield.cpp +++ b/src/tbitfield.cpp @@ -13,194 +13,222 @@ static TBitField FAKE_BITFIELD(1); TBitField::TBitField(int len) { - if (len <= 0) - { - throw "init error"; - } - else + if (len > -1) + { + BitLen = len; + MemLen = len * sizeof(TELEM) * 8; + pMem = new TELEM[MemLen]; + if (pMem != NULL) { - BitLen = sizeof(int) * 8 * len; - MemLen = len; - pMem = new TELEM[MemLen]; - for (int i = 0; i < len; i++) { - pMem[i] = 0; - } + for (int i = 0; i < MemLen; i++) + pMem[i] = 0; } + } + else + { + throw "error"; + } } -TBitField::TBitField(const TBitField &bf) // конструктор копирования +TBitField::TBitField(const TBitField& bf) // конструктор копирования { - BitLen = bf.BitLen; + if (bf.BitLen <= 0) + throw "error"; + else + { MemLen = bf.MemLen; + BitLen = bf.BitLen; pMem = new TELEM[MemLen]; - for (int i = 0; i < MemLen; i++) + for (int i = 0; i < MemLen; i++) { - pMem[i] = bf.pMem[i]; + pMem[i] = bf.pMem[i]; } - + } } TBitField::~TBitField() { - if (pMem != nullptr) - { - delete[] pMem; - Bitlen = 0; - MemLen = 0; - pMem = nullptr; - } + if (pMem != 0) + delete[] pMem; + pMem = 0; + MemLen = 0; + BitLen = 0; } - - int TBitField::GetMemIndex(const int n) const // индекс Мем для бита n { - if ((n >= 0) && (n < BitLen)) - { - return n >> 5; - } - else - { - throw "error"; - } - return FAKE_INT; + if ((n < 0) || (n > BitLen)) + { + throw "error"; + } + else + return n >> 5; } TELEM TBitField::GetMemMask(const int n) const // битовая маска для бита n { - if ((n >= 0) && (n < BitLen)) - { - return 1 << (n & 31); - } - else - { - throw "error"; - } - return FAKE_INT; + if (n < 0 && n < BitLen) throw "error"; + return 1 << (n & 31); } // доступ к битам битового поля int TBitField::GetLength(void) const // получить длину (к-во битов) { - return MemLen; + return BitLen; } void TBitField::SetBit(const int n) // установить бит { - if ((n >= 0) && (n < BitLen)) - { - int i = GetMemIndex(n); - int m = GetMemMask(n); - pMem[i] = pMem[i] | m; - } - else - { - throw "error index"; - } + 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)) - { - int i = GetMemIndex(n); - - int m = GetMemMask(n); - - pMem[i] = pMem[i] & ~m; - } - else - { - throw "error index"; - } - + 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 // получить значение бита { - if ((n >= 0) && (n < BitLen)) - { - int i = GetMemIndex(n); - int m = GetMemMask(n); - return (pMem[i] & m) == 0 ? 0 : 1; - } - else { - throw "Error GetBit"; - } - 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) // присваивание +TBitField& TBitField::operator=(const TBitField& bf) // присваивание { - if (bf.pMem == pMem) - { - return *this; - } - else + 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++) { - delete[] pMem; - BitLen = bf.BitLen; - MemLen = bf.MemLen; - pMem = new TELEM[MemLen]; - for (int i = 0; i < MemLen; i++) - { - pMem[i] = bf.pMem[i]; - } + pMem[i] = bf.pMem[i]; } return *this; - + } } -int TBitField::operator==(const TBitField &bf) const // сравнение +int TBitField::operator==(const TBitField& bf) const // сравнение { - if (bf.MemLen != pMem) + if (MemLen != bf.MemLen || BitLen != bf.BitLen) + return 0; + else { - return false; - } - else + for (int i = 0; i < MemLen; i++) { - for (int i = 0; i < MemLen; i++) - { - if (pMem[i] != bf.pMem[i]) - { - return false; - } - } - return true; + if (pMem[i] != bf.pMem[i]) + return 0; } + return 1; + } } -int TBitField::operator!=(const TBitField &bf) const // сравнение +int TBitField::operator!=(const TBitField& bf) const // сравнение { - return FAKE_INT; + 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) // операция "или" +TBitField TBitField::operator|(const TBitField& bf) // операция "или" { - return FAKE_BITFIELD; + 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) // операция "и" +TBitField TBitField::operator&(const TBitField& bf) // операция "и" { - return FAKE_BITFIELD; + 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; } From 50b38cc60d6b162311406b5b9b26a30a50ee9ace Mon Sep 17 00:00:00 2001 From: dmitr1yR <104106501+dmitr1yR@users.noreply.github.com> Date: Sun, 16 Oct 2022 22:45:36 +0300 Subject: [PATCH 3/3] Update tset.cpp . --- src/tset.cpp | 71 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/tset.cpp b/src/tset.cpp index e70a76e58..0904d4ad9 100644 --- a/src/tset.cpp +++ b/src/tset.cpp @@ -12,93 +12,118 @@ 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; + TBitField res(this->BitField); + return res; } 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; + BitField = s.BitField; + MaxPower = s.GetMaxPower(); + return *this; } -int TSet::operator==(const TSet &s) const // сравнение +int TSet::operator==(const TSet& s) const // сравнение { - return FAKE_INT; + return BitField == s.BitField; } -int TSet::operator!=(const TSet &s) const // сравнение +int TSet::operator!=(const TSet& s) const // сравнение { - return FAKE_INT; + return BitField != s.BitField; } -TSet TSet::operator+(const TSet &s) // объединение +TSet TSet::operator+(const TSet& s) // объединение { - return FAKE_SET; + TSet res(BitField | s.BitField); + return res; } TSet TSet::operator+(const int Elem) // объединение с элементом { - return FAKE_SET; + BitField.SetBit(Elem); + return *this; } TSet TSet::operator-(const int Elem) // разность с элементом { - return FAKE_SET; + TSet res(BitField & Elem); + return res; } -TSet TSet::operator*(const TSet &s) // пересечение +TSet TSet::operator*(const TSet& s) // пересечение { - return FAKE_SET; + TSet res(BitField & s.BitField); + return res; } TSet TSet::operator~(void) // дополнение { - return FAKE_SET; + TSet res(*this); + res.BitField = ~res.BitField; + return res; } // перегрузка ввода/вывода -istream &operator>>(istream &istr, TSet &s) // ввод +istream& operator>>(istream& istr, TSet& s) // ввод { - return istr; + int in; + cin >> in; + if (in >= 0 && in < s.GetMaxPower()) + { + s.InsElem(in); + } + return istr; } -ostream& operator<<(ostream &ostr, const TSet &s) // вывод +ostream& operator<<(ostream& ostr, const TSet& s) // вывод { - return ostr; + for (int i = 0; i < s.GetMaxPower(); i++) + if (s.IsMember(i)) + { + ostr << ' ' << i; + } + return ostr; }