-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiteString
116 lines (116 loc) · 2.35 KB
/
BiteString
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
#include <iostream>
class String
{
protected:
char* str;
public:
String(const char* str_)
{
str = new char[strlen(str_) + 1];
strcpy_s(str, strlen(str_) + 1, str_);
}
String(const String& copy)
{
str = new char[strlen(copy.str) + 1];
strcpy_s(str, strlen(copy.str) + 1, copy.str);
}
String() : String(" ") {};
~String()
{
delete[] str;
}
void operator +=(String& rhs)
{
*this = *this + rhs;
}
int getSize()
{
return strlen(str);
}
void clear()
{
delete[] str;
str = new char[1]{""};
}
virtual char* plus( String& rhs)
{
char* copy = new char[strlen(str) + strlen(rhs.str) + 1];
int num = strlen(str) + 1;
for (int i = 0; i <= num; ++i)
{
copy[i] = str[i];
}
num = strlen(rhs.str) + 1;
for (int i = strlen(str), d = 0; d < num; ++i, ++d)
{
copy[i] = rhs.str[d];
}
return copy;
}
virtual void operator = (const String& copy);
friend const String operator +(String& lhs, String& rhs);
friend std::ostream& operator<<(std::ostream& out, String& str);
friend std::istream& operator>>(std::istream& in, String& str);
friend bool operator == (const String& lhs, const String& rhs);
friend bool operator != (const String& lhs, const String& rhs);
};
void String :: operator = (const String& copy)
{
if (strlen(str) >= strlen(copy.str))
{
strcpy_s(str, strlen(copy.str) + 1, copy.str);
}
else
{
delete[] str;
str = new char[strlen(copy.str) + 1];
strcpy_s(str, strlen(copy.str) + 1, copy.str);
}
}
bool operator ==(const String& lhs, const String& rhs)
{
return !(strcmp(lhs.str, rhs.str));
}
bool operator !=(const String& lhs, const String& rhs)
{
return (strcmp(lhs.str, rhs.str));
}
const String operator +( String& lhs, String& rhs)
{
return String(lhs.plus(rhs));
}
std::istream& operator>>(std::istream& in, String& str)
{
char d[900];
in >> d;
String copy{ d };
str = copy;
return in;
}
std::ostream& operator<<(std::ostream& out, String& str)
{
return out << str.str;
}
class BiteString:public String
{
public:
BiteString(const String& copy) :String(copy) { perevirka(); };
BiteString() :BiteString("") {};
BiteString(const BiteString& copy) :String(copy){};
~BiteString() { delete[] str; }
void perevirka()
{
for (int i = 0; i < getSize(); ++i)
{
if ((str[i] != '1')and(str[i] != '0'))
{
clear();
}
}
}
};
int main()
{
BiteString str{ "00001" }, str2{ "00000" };
std::cout << str;
}