-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.h
More file actions
302 lines (243 loc) · 5.43 KB
/
Copy pathHeader.h
File metadata and controls
302 lines (243 loc) · 5.43 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
using namespace std;
int StrLen( char* );
void StrCpy( char*, char* );
bool StrCmp( char*, char* );
class String
{
public:
String();
String( const char* _str );
String( const String& );
~String();
String& operator=( const String& );
String& operator=( const char* s );
friend String operator+( const String&, const String& );
String& operator+=( const String& );
friend bool operator==( const String&, const String& );
//friend bool operator=(const String&, char*);
friend bool operator!=( const String&, const String& );
friend bool operator>( const String&, const String& );
friend bool operator>=( const String&, const String& );
friend bool operator<( const String&, const String& );
friend bool operator<=( const String&, const String& );
const char& operator[]( int ) const;
char& operator[]( int );
friend std::ostream& operator<<( std::ostream&, const String& );
friend std::istream& operator>>( std::istream&, String& );
int lenght();
private:
char* str;
int* counter;
int len;
};
String::String( const char* s ):counter(new int)
{
//if (*this->counter<1 )
//{
*counter = 1;
//}
len = strlen( s );
str = new char[len + 1];//ðàñïðåäåëåíèå ïàìÿòè
StrCpy( str, ( char* )s );// èíèöèàëèçàöèÿ óêàçàòåëÿ
}
//êîíñòðóêòîð ïî óìîë÷àíèþ
String::String():counter(new int)
{
len = 0;
str = new char[1];
str[0] = '\0';//ñòðîêà çàäàííàÿ ïî óìîë÷àíèþ
*counter = 1;
}
String::String( const String& rhs )
{
counter = rhs.counter;
str = new char[StrLen( rhs.str ) + 1];
StrCpy( str, rhs.str );
len = rhs.len;
++* counter;
}
String::~String()
{
//delete str;
if ( *counter > 1 )
{
delete[] str;
//str = nullptr;
}
if ( *counter > 0 )
{
--* counter;
}
if ( *counter == 0 )
{
delete[] str;
//str = nullptr;
delete[] counter;
//counter = nullptr;
std::cout << "my Class String deleted the object. The counter equals 0" << std::endl;
}
}
// ---
String& String::operator=( const String& rhs )
{
if ( this != &rhs )
{
delete[] this->str;
this->str = new char[StrLen( rhs.str ) + 1];
StrCpy( this->str, rhs.str );
counter = rhs.counter;
len = rhs.len;
}
return *this;
}
String& String::operator=( const char* s )
{
if ( str != s && (*counter>1) )
{
--*counter;
int* count = new int;
counter = count;
*counter = 1;
}
delete[] str;
len = strlen( s );
str = new char[len + 1];
StrCpy( str, ( char* )s );
return *this;
}
String& String::operator+=( const String& rhs )
{
int sz = this->len + rhs.len ;
char* ts = new char[sz + 1];
for ( int i = 0; i < StrLen( this->str ); i++ )
ts[i] = this->str[i];
for ( int ii = StrLen( this->str ), j = 0; ii <= sz; ii++, j++ )
ts[ii] = rhs.str[j];
delete[] this->str;
this->str = ts;
this->len += rhs.len;
return *this;
}
String operator+( const String& lhs, const String& rhs )
{
String ts = lhs;
return ts += rhs;
}
// --
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 ));
}
bool operator>( const String& lhs, const String& rhs )
{
return (StrLen( lhs.str ) > StrLen( rhs.str )) ? true : false;
}
bool operator>=( const String& lhs, const String& rhs )
{
return (StrLen( lhs.str ) >= StrLen( rhs.str )) ? true : false;
}
bool operator<( const String& lhs, const String& rhs )
{
return (StrLen( lhs.str ) < StrLen( rhs.str )) ? true : false;
}
bool operator<=( const String& lhs, const String& rhs )
{
return StrLen( lhs.str ) <= StrLen( rhs.str );
}
// ---
const char& String::operator[]( int i ) const
{
//std::cerr << "Index out of range. \n";
return (i >= 0 && i < StrLen( this->str )) ? this->str[i] : 0;
}
char& String::operator[]( int i )
{
static char DUMMY;
DUMMY = '\0';
//std::cerr << "Index out of range. \n";
return (i >= 0 && i < StrLen( this->str )) ? this->str[i] : DUMMY;
}
inline int String::lenght()
{
return this->len;
}
// ---
std::ostream& operator<<( std::ostream& os, const String& obj )
{
return os << obj.str;
}
std::istream& operator>>( std::istream& is, String& obj )
{
char BUFF[2048];
is.getline( BUFF, sizeof BUFF );
obj = BUFF;
return is;
}
// ---
int StrLen( char* _str )
{
int size = 0;
for ( ; _str[size] != 0; size++ );
return size;
}
void StrCpy( char* in_str, char* src_str )
{
for ( int i = 0; i < StrLen( in_str ); i++ )
in_str[i] = src_str[i];
}
bool StrCmp( char* str_f, char* str_s )
{
int i = 0;
for ( ; str_f[i] == str_s[i] && i < StrLen( str_f ); i++ );
return i == StrLen( str_f );
}
int main()
{
//String s = "asdfa"; // O(n)
//String s2 = s; // O(1)
//s2="aaaa" ;
//s2 += "bbbb";
//s2 = "ll";
//String s3(s2);
//s = s + s3;
////String s2 = "aaaaaaaa";
//if (s[0]==s[s.lenght()] )
//{
// cout << "yes\n";
//}
//for (int i=0;i<s.lenght();i++ )
//{
// s[i] = '!';
//}
//cout << endl;
//cout << s;
//
//String s1( s );
//String a;
//cin >> a;
//if (a.lenght()<=s1.lenght() )
//{
// cout << "\na<=s1\n";
//}
//if (a<s1 )
//{
// cout << "\na<s1";
//}
//else
//{
// cout << "\na!< s1";
//}
//cout << endl;
//
String s1;
cin >> s1;
String s2( s1 );
String s3 = s2;
cout << s2[0];
s2[0] = 'Y';
}