Repository navigation
Expand file tree
/
Copy pathtset.cpp
More file actions
122 lines (100 loc) · 2.32 KB
/
Copy pathtset.cpp
File metadata and controls
122 lines (100 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// ÍÍÃÓ, ÂÌÊ, Êóðñ "Ìåòîäû ïðîãðàììèðîâàíèÿ-2", Ñ++, ÎÎÏ
//
// tset.cpp - Copyright (c) Ãåðãåëü Â.Ï. 04.10.2001
// Ïåðåðàáîòàíî äëÿ Microsoft Visual Studio 2008 Ñûñîåâûì À.Â. (19.04.2015)
//
// Ìíîæåñòâî - ðåàëèçàöèÿ ÷åðåç áèòîâûå ïîëÿ
#include "tset.h"
TSet::TSet(int mp) : BitField(mp)
{
MaxPower = mp;
}
// êîíñòðóêòîð êîïèðîâàíèÿ
TSet::TSet(const TSet &s) : BitField(s.BitField)
{
MaxPower = s.MaxPower;
}
// êîíñòðóêòîð ïðåîáðàçîâàíèÿ òèïà
TSet::TSet(const TBitField &bf) : BitField(bf)
{
MaxPower = bf.GetLength();
}
TSet::operator TBitField()
{
return BitField;
}
int TSet::GetMaxPower(void) const // ïîëó÷èòü ìàêñ. ê-âî ýë-òîâ
{
return MaxPower;
}
int TSet::IsMember(const int Elem) const // ýëåìåíò ìíîæåñòâà?
{
return BitField.GetBit(Elem);
}
void TSet::InsElem(const int Elem) // âêëþ÷åíèå ýëåìåíòà ìíîæåñòâà
{
if (Elem >= MaxPower) throw "Insert non existing element!";
BitField.SetBit(Elem);
}
void TSet::DelElem(const int Elem) // èñêëþ÷åíèå ýëåìåíòà ìíîæåñòâà
{
BitField.ClrBit(Elem);
}
// òåîðåòèêî-ìíîæåñòâåííûå îïåðàöèè
TSet& TSet::operator=(const TSet &s) // ïðèñâàèâàíèå
{
MaxPower = s.MaxPower;
BitField = s.BitField;
return *this;
}
int TSet::operator==(const TSet &s) const // ñðàâíåíèå
{
return MaxPower != s.MaxPower ? 0 : BitField == s.BitField;
}
int TSet::operator!=(const TSet &s) const // ñðàâíåíèå
{
return !(*this == s);
}
TSet TSet::operator+(const TSet &s) // îáúåäèíåíèå
{
TSet result(BitField | s.BitField);
return result;
}
TSet TSet::operator+(const int Elem) // îáúåäèíåíèå ñ ýëåìåíòîì
{
TSet result(*this);
result.InsElem(Elem);
return result;
}
TSet TSet::operator-(const int Elem) // ðàçíîñòü ñ ýëåìåíòîì
{
TSet result(*this);
result.DelElem(Elem);
return result;
}
TSet TSet::operator*(const TSet &s) // ïåðåñå÷åíèå
{
TSet result(BitField & s.BitField);
return result;
}
TSet TSet::operator~(void) // äîïîëíåíèå
{
TSet result(~BitField);
return result;
}
// ïåðåãðóçêà ââîäà/âûâîäà
istream &operator>>(istream &istr, TSet &s) // ââîä
{
int n;
while (istr >> n) {
s.InsElem(n);
}
return istr;
}
ostream& operator<<(ostream &ostr, const TSet &s) // âûâîä
{
for (int n = 0; n < s.MaxPower; ++n) {
if (s.IsMember(n)) ostr << n << ' ';
}
return ostr;
}