-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.cpp
732 lines (633 loc) · 13.4 KB
/
String.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "Mystring.h"
String::String() : String("", 80) {}
String::String(const char* text) : String(text, 80) {}
String::String(unsigned int capacity) : String("", capacity) {}
String::String(const String& original) : String(original.text, original.capacity) {}
String::String(const char* text, unsigned int capacity)
{
SetString(text, capacity);
}
String::~String()
{
if (text != nullptr)
{
delete[] text;
text = nullptr;
}
}
// ãåòòåðû-ñåòòåðû
const char* const String::GetString() const
{
return text;
}
void String::SetString(const char* text, unsigned int capacity)
{
length = strlen(text);
if (capacity <= length)
capacity = length + 1;
this->capacity = capacity;
delete[] this->text;
this->text = new char[capacity];
strcpy_s(this->text, capacity, text);
}
int String::GetLength() const
{
return length;
}
int String::GetCapacity() const
{
return capacity;
}
void String::Clear()
{
text[0] = 0;
length = 0;
}
void String::ShrinkToFit()
{
if (length + 1 <= capacity)
{
return;
}
capacity = length + 1;
char* temp = new char[capacity];
strcpy_s(temp, capacity, text);
delete[] text;
text = temp;
}
char String::GetCharAt(unsigned int index) // âåðí¸ò ñèìâîë ñòðîêè ïî óêàçàííîé ïîçèöèè
{
char temp = text[index];
return temp;
}
void String::Print() // ïîêàç ñòðîêè íà ýêðàí
{
cout << this->text;
cout << endl;
}
void String::PrintLn() // ïîêàç ñòðîêè íà ýêðàí ñ ïåðåâîäîì êóðñîðà íà ñëåäóþùóþ ñòðîêó
{
int k = 0;
if (length > 80)
{
while (length > 80)
{
for (int i = 0; i < 80; i++)
{
cout << this->text[k + i];
}
k += 80;
length -= 80;
cout << endl;
}
for (int i = 0; i < length; i++)
{
cout << this->text[k + i];
}
cout << endl;
}
else
{
for (int i = 0; i < length; i++)
{
cout << this->text[i];
}
cout << endl;
}
}
void String::GetLine() // ââîä ñòðîêè ñ êëàâèàòóðû
{
if (this->text != 0)
{
delete[] this->text;
}
char* temp = new char[capacity];
cout << "Enter the string: ";
cin >> temp;
int othelength = strlen(temp);
capacity = othelength + 1;
this->text = temp;
strcpy_s(this->text, capacity, text);
}
const char* String::GetCharArray() const //??? âîçâðàò êîíñòàíòíîãî óêàçàòåëÿ íà ìàññèâ ñèìâîëîâ ñòðîêè
{
char* const ptext = &text[0];
return ptext;
}
int String::CompareTo(String& other) // ñðàâíåíèå íàøåé ñòðîêè ñ êàêîé - òî äðóãîé ñòðîêîé. //Äîáàâèòü âåðñèþ int CompareTo(char*)
{
if (this->length != other.length)
{
return -1;
}
for (int i = 0; i < length; i++)
{
if (this->text[i] != other.text[i])
{
return -1;
}
}
return 1;
}
int String::CompareTo(char* other) // ñðàâíåíèå íàøåé ñòðîêè ñ êàêîé - òî äðóãîé ñòðîêîé âåðñèþ int CompareTo(char*)
{
int othelength = strlen(other);
if (this->length != othelength)
{
return -1;
}
for (int i = 0; i < length; i++)
{
if (text[i] != other[i])
{
return -1;
}
}
return 1;
}
void String::Concat(String& other) //??? ïðèñîåäèíèòü ê íàøåé ñòðîêå ïåðåäàííóþ ñòðîêó
{
char* temp = new char[capacity];
capacity = strlen(text) + strlen(other.text) + 1;
strcpy_s(temp, capacity, this->text);
strcat_s(temp, capacity, other.text);
temp[capacity] = '\0';
delete[] this->text;
this->text = temp;
strcpy_s(this->text, capacity, text);
}
void String::Concat(char* str) // çàïèñü â êîíåö ñòðîêè+++
{
char* temp = new char[capacity];
capacity = strlen(this->text) + strlen(str) + 1;
strcpy_s(temp, capacity, this->text);
strcat_s(temp, capacity, str);
temp[capacity] = '\0';
delete[] this->text;
this->text = temp;
strcpy_s(this->text, capacity, text);
}
void String::Concat(int value) // çàïèñü ÷èñëà â êîíåö ñòðîêè+++
{
int r = 0;
char* tempchar = new char[r];
int temp = value;
while (temp != 0)
{
temp /= 10;
r++;
}
int* array = new int[r];
for (int i = r - 1; i >= 0; i--) {
array[i] = value % 10;
value /= 10;
switch (array[i])
{
case 0: tempchar[i] = { '0' }; break;
case 1: tempchar[i] = { '1' }; break;
case 2: tempchar[i] = { '2' }; break;
case 3: tempchar[i] = { '3' }; break;
case 4: tempchar[i] = { '4' }; break;
case 5: tempchar[i] = { '5' }; break;
case 6: tempchar[i] = { '6' }; break;
case 7: tempchar[i] = { '7' }; break;
case 8: tempchar[i] = { '8' }; break;
case 9: tempchar[i] = { '9' }; break;
default: break;
}
}
tempchar[r] = '\0';
String::Concat(tempchar);
}
void String::Concat(double value) // çàïèñü ÷èñëà â êîíåö ñòðîêè+++
{
{
int r = 0;
char* tempchar = new char[r];
int temp = value * 100; // òî÷íîñòü, çíàêîâ ïîñëå çïÿòîé
while (temp != 0)
{
temp /= 10;
r++;
}
r++;
temp = value * 100;;
int* array = new int[r];
for (int i = r - 1; i >= 0; i--)
{
if (i == r - 3)
{
tempchar[i] = { '.' };
}
else
{
array[i] = temp % 10;
temp /= 10;
switch (array[i])
{
case 0: tempchar[i] = { '0' }; break;
case 1: tempchar[i] = { '1' }; break;
case 2: tempchar[i] = { '2' }; break;
case 3: tempchar[i] = { '3' }; break;
case 4: tempchar[i] = { '4' }; break;
case 5: tempchar[i] = { '5' }; break;
case 6: tempchar[i] = { '6' }; break;
case 7: tempchar[i] = { '7' }; break;
case 8: tempchar[i] = { '8' }; break;
case 9: tempchar[i] = { '9' }; break;
default: break;
}
}
}
tempchar[r] = '\0';
String::Concat(tempchar);
}
}
bool String::Contains(String& other) // ïðîâåðèòü, ñîäåðæèòñÿ ëè â íàøåé ñòðîêå ïåðåäàííàÿ ïîäñòðîêà(â ëþáîé ïîçèöèè)
{
if (other.length > this->length)
{
throw "Input Error";
}
int k = 0; // êîíòðîëü ñîâïàäåíèé
int t = 1;
for (int i = 0; i < this->length; i++)
{
k = i;
t = 1;
for (int j = 0; j < other.length; j++)
{
if (this->text[k] == other.text[j])
{
if (t == other.length)
{
return 1;
}
k++;
t++;
}
}
}
return 0;
}
bool String::EndsWith(String& other) // ïðîâåðèòü, çàêàí÷èâàåòñÿ ëè íàøà ñòðîêà òåêñòîì,
//êîòîðûé ïåðåäàí â ïàðàìåòðå
{
if (other.length > this->length)
{
throw "Input Error";
}
int k = 0; // êîíòðîëü ñîâïàäåíèé
int t = 1;
for (int i = this->length - other.length; i < this->length; i++)
{
k = i;
t = 1;
for (int j = 0; j < other.length; j++)
{
if (this->text[k] == other.text[j])
{
if (t == other.length)
{
return 1;
}
k++;
t++;
}
}
}
return 0;
}
bool String::StartsWith(String& other)
{
if (other.length > this->length)
{
throw "Input Error";
}
// êîíòðîëü ñîâïàäåíèé
int t = 1;
for (int j = 0; j < other.length; j++)
{
if (this->text[j] == other.text[j])
{
if (t == other.length)
{
return 1;
}
t++;
}
}
return 0;
}
bool String::Equals(String& other) // ïðîâåðèòü, ñîîòâåòñòâóåò ëè ëåêñèêîãðàôè÷åñêè íàøà ñòðîêà äðóãîé ñòðîêå(àíàëîãè÷íî CompareTo)
{
if (this->length != other.length)
{
return 0;
}
for (int i = 0; i < length; i++)
{
if (text[i] != other.text[i])
{
return 0;
}
}
return 1;
}
int String::IndexOf(char symbol) // ïðîâåðêà íà âõîæäåíèå ñèìâîëà â ñòðîêó, â ðåçóëüòàòå ðàáîòû âîçâðàùàåò èíäåêñ ïåðâîãî íàéäåííîãî ñèìâîëà(îò íà÷àëà ñòðîêè); åñëè íè÷åãî íå íàéäåíî, âîçâðàùàåò - 1
{
for (int j = 0; j < this->length; j++)
{
if (this->text[j] == symbol)
{
return j;
}
}
return -1;
}
int String::LastIndexOf(char symbol) // ïðîâåðêà íà âõîæäåíèå ñèìâîëà â ñòðîêó, â ðåçóëüòàòå ðàáîòû âîçâðàùàåò èíäåêñ ïîñëåäíåãî íàéäåííîãî ñèìâîëà(ïîèñê ñ êîíöà ñòðîêè); åñëè íè÷åãî íå íàéäåíî, âîçâðàùàåò - 1
{
int t = -1;
for (int j = this->length - 1; j >= 0; j--)
{
if (this->text[j] == symbol)
{
t = j;
}
}
return t;
}
int String::IndexOf(String& other) // ïðîâåðêà íà âõîæäåíèå ïîäñòðîêè â ñòðîêó, â ðåçóëüòàòå ðàáîòû âîçâðàùàåò èíäåêñ íà÷àëà âõîæäåíèÿ; åñëè íè÷åãî íå íàéäåíî, âîçâðàùàåò - 1
{
if (other.length > this->length)
{
throw "Input Error";
}
int k = 0; // êîíòðîëü ñîâïàäåíèé
int t;
for (int i = 0; i < this->length; i++)
{
k = i;
t = 1;
for (int j = 0; j < other.length; j++)
{
if (this->text[k] == other.text[j])
{
if (t == other.length)
{
return i;
}
k++;
t++;
}
}
}
return -1;
}
int String::LastIndexOf(String& other) // ïðîâåðêà íà âõîæäåíèå ïîäñòðîêè â ñòðîêó, â ðåçóëüòàòå ðàáîòû âîçâðàùàåò èíäåêñ íà÷àëà âõîæäåíèÿ(íî ïîèñê èä¸ò ñ êîíöà ñòðîêè); åñëè íè÷åãî íå íàéäåíî, âîçâðàùàåò - 1
{
if (other.length > this->length)
{
throw "Input Error";
}
int k = 0; // êîíòðîëü ñîâïàäåíèé
int t;
for (int i = this->length - 1; i >= 0; i--)
{
k = i;
t = 1;
for (int j = other.length - 1; j >= 0; j--)
{
if (this->text[k] == other.text[j])
{
if (t == other.length)
{
return i;
}
k--;
t++;
}
}
}
return -1;
}
void String::Remove(int index) // óäàëåíèå âñåõ ñèìâîëîâ äî êîíöà ñòðîêè, íà÷èíàÿ îò ïåðåäàííîãî èíäåêñà
{
this->capacity = index;
this->text[this->capacity] = '\0';
}
void String::Remove(int start, int count) // óäàëåíèå count ñèìâîëîâ, íà÷èíàÿ îò èíäåêñà start
{
if (start + count + 1 > this->length)
{
throw "Input Error";
}
for (int i = start; i < this->length; i++)
{
this->text[i] = this->text[i + count];
}
this->text[this->capacity - count] = '\0';
}
void String::Replace(char R, char Z) // ìåíÿåò â ñòðîêå âñå íàéäåííûå ñèìâîëû R íà ïåðåäàííûé ñèìâîë Z
{
for (int i = 0; i < this->length; i++)
{
if (this->text[i] == R) this->text[i] = Z;
}
}
void String::Replace(String substr, String rep) // çàìåíÿåò íàéäåííóþ ïîäñòðîêó substr
//?íà ñòðîêó rep - ïðîèçâîëüíîé äëèíû
{
char* temp = new char[capacity];
capacity = this->length - substr.length + rep.length;
this->capacity = capacity;
if (substr.length > this->length)
{
throw "Input Error";
}
int k = 0;
int t;
for (int i = 0; i < this->length; i++)
{
k = i;
t = 1;
for (int j = 0; j < substr.length; j++)
{
if (this->text[k] == substr.text[j])
{
if (t == substr.length)
{
for (int x = 0; x <= substr.length; x++)
{
temp[x] = this->text[x];
}
int y = 0;
for (int x = i; x < i + rep.length; x++)
{
temp[x] = rep.text[y];
y++;
}
y = 0;
for (int x = i + substr.length; x < capacity; x++)
{
temp[i + rep.length + y] = this->text[x];
y++;
}
temp[capacity] = '\0';
delete[] this->text;
this->text = temp;
}
k++;
t++;
}
}
}
}
String* String::Split(char separator, int& pieces) //--------?????
// äåëèò íàøó ñòðîêó íà êóñî÷êè, ðàçäåë¸ííûå ñèìâîëîì separator.
//Óêàçàòåëü íà ìàññèâ êóñî÷êîâ âåðí¸òñÿ èç ôóíêöèè.
// pieces çàïèøåòñÿ êîëè÷åñòâî ïîëó÷èâøèõñÿ êóñî÷êîâ
{
String* sep = new String[capacity];
char* temp = new char[capacity];
int k = this->length / pieces;
int y = 0;
int f = 1;
for (int x = 0; x <= pieces + 1; x++)
{
for (int i = 0; i < k; i++)
{
temp[i] = this->text[i + k * y];
}
temp[k * f] = '\0';
sep[x] = temp;
temp[0] = separator;
temp[1] = '\0';
sep[x + f] = temp;
y++;
f++;
}
return sep;
}
String String::ToLower() // ïðèâîä ê íèæíåìó ðåãèñòðó(â òîì ÷èñëå è äëÿ ðóññêîãî òåêñòà)
{
for (int i = 0; i < length; i++)
{
this->text[i] = tolower(this->text[i]);
}
return this->text;
}
String String::ToUpper() // ïðèâîä ê âåðõíåìó ðåãèñòðó(â òîì ÷èñëå è äëÿ ðóññêîãî òåêñòà)
{
for (int i = 0; i < length; i++)
{
this->text[i] = towupper(this->text[i]);
}
return this->text;
}
void String::Trim() // óáèðàåò ëèøíèå ïðîáåëû â íà÷àëå è â êîíöå ñòðîêè
{
char* temp = new char[capacity];
int t = 0;
int k = this->length - 1;
while (this->text[k] == ' ')
{
k--;
}
while (this->text[t] == ' ')
{
t++;
}
for (int i = 0; i < this->length; i++)
{
temp[i] = this->text[i + t];
}
temp[this->length - k + 1] = '\0';
delete[] this->text;
this->text = temp;
}
void String::TrimEnd()
{
int k = this->length - 1;
while (this->text[k] == ' ')
{
k--;
}
this->text[++k] = '\0';
cout << k;
}
void String::TrimStart()
{
char* temp = new char[capacity];
int k = 0;
while (this->text[k] == ' ')
{
k++;
}
for (int i = 0; i < this->length; i++)
{
temp[i] = this->text[i + k];
}
temp[this->length - k] = '\0';
delete[] this->text;
this->text = temp;
}
void String::Reverse() // ïåðåâîðà÷èâàíèå ñòðîêè
{
char* temp = new char[capacity];
for (int i = 0; i < this->length; i++)
{
temp[i] = this->text[this->length - i - 1];
}
temp[this->length] = '\0';
delete[] this->text;
this->text = temp;
}
void String::SortAZ() // ñîðòèðîâêà âñåõ ñèìâîëîâ ñòðîêè â àëôàâèòíîì ïîðÿäêå îò À äî ß
{
sort(this->text, this->text + length);
}
void String::SortZA() // ñîðòèðîâêà âñåõ ñèìâîëîâ ñòðîêè â àëôàâèòíîì ïîðÿäêå îò ß äî À
{
sort(this->text, this->text + length);
char* temp = new char[capacity];
for (int i = 0; i < this->length; i++)
{
temp[i] = this->text[this->length - i - 1];
}
temp[this->length] = '\0';
delete[] this->text;
this->text = temp;
}
void String::Shuffle() // ñëó÷àéíîå ïåðåìåøèâàíèå ñèìâîëîâ ñòðîêè
{
int index = 0;
char* temp = new char[capacity];
for (int i = 0; i < this->length; i++)
{
index = rand() % this->length;
temp[i] = this->text[index];
}
temp[this->length] = '\0';
delete[] this->text;
this->text = temp;
}
void String::RandomFill() // çàïîëíåíèå ñòðîêè ñëó÷àéíûìè ñèìâîëàìè(íà âåñü capacity)
{
int index = 0;
char* temp = new char[capacity];
for (int i = 0; i < capacity; i++)
{
index = rand() % 255;
temp[i] = char(index);
}
temp[this->length] = '\0';
delete[] this->text;
this->text = temp;
}
void String::ShowInfo() const
{
cout << "Text: " << GetString() << endl;
cout << "Length: " << GetLength() << endl;
cout << "Capacity: " << GetCapacity() << endl;
}