-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultifunctionString.hpp
175 lines (142 loc) · 5.05 KB
/
MultifunctionString.hpp
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
#pragma once
#include <string>
#include <cctype>
#include <iostream>
namespace MultifunctionString {
class MString : public std::string {
using base = std::string;
private:
bool check_growing(const MString& str) {
bool is_good = true;
for (size_t i = 1; i < str.size(); ++i) {
if (str[i - 1] > str[i]) {
is_good = false;
break;
}
}
return is_good;
}
std::vector<MString*> additional;
public:
using base::base;
MString(std::string s) : base(s) {}
MString(const char* s) : base(s) {}
//void m_additional(const MString)
void m_insert(size_t ind, const MString& substr) {
this->insert(ind, substr);
}
void m_replace(size_t ind, const char* new_c) {
(*this)[ind - 1] = *new_c;
}
void m_replace(const MString& old_substr, const MString& new_substr) {
MString new_str = "";
for (size_t i = 0; i < this->size(); ++i) {
if (i + old_substr.size() - 1 >= this->size()) {
new_str += (*this)[i];
continue;
}
if (this->substr(i, old_substr.size()) == old_substr) {
new_str += new_substr;
i += old_substr.size() - 1;
} else {
new_str += (*this)[i];
}
}
*this = new_str;
}
void remove_leading_zeros() {
MString new_str = "";
bool is_digit_now = false;
MString digit_str = "";
for (size_t i = 0; i < this->size(); ++i) {
if (!std::isdigit((*this)[i])) {
if (is_digit_now) {
new_str += digit_str;
digit_str.clear();
is_digit_now = false;
}
new_str += (*this)[i];
continue;
}
if (!is_digit_now) {
is_digit_now = true;
}
if ((*this)[i] != '0' && digit_str.size() == 1 && digit_str[0] == '0') {
digit_str[0] = (*this)[i];
} else if (digit_str.empty() || digit_str.size() > 1 || digit_str.size() == 1 && digit_str[0] != '0' || (*this)[i] != '0') {
digit_str += (*this)[i];
}
}
new_str += digit_str;
digit_str.clear();
*this = new_str;
}
void delete_non_growing_subgroups() {
MString new_str = "";
bool is_digit_now = false;
MString digit_str = "";
for (size_t i = 0; i < this->size(); ++i) {
if (!std::isdigit((*this)[i])) {
if (is_digit_now) {
if (check_growing(digit_str)) {
new_str += digit_str;
}
digit_str.clear();
is_digit_now = false;
}
new_str += (*this)[i];
continue;
}
if (!is_digit_now) {
is_digit_now = true;
}
digit_str += (*this)[i];
}
if (check_growing(digit_str)) {
new_str += digit_str;
}
digit_str.clear();
*this = new_str;
}
void replace_asterisk(const size_t N) {
MString new_str = "";
MString new_substr(N / 2, '+');
bool is_aster = false;
int aster_count = 0;
for (size_t i = 0; i < this->size(); ++i) {
if (i + N - 1 >= this->size()) {
new_str += (*this)[i];
continue;
}
if (this->at(i) == '*') {
if (!is_aster) is_aster = true;
++aster_count;
} else {
if (is_aster) {
if (aster_count == N) new_str += std::string(N / 2, '+');
else new_str += std::string(aster_count, '*');
aster_count = 0;
is_aster = false;
}
new_str += this->at(i);
}
}
*this = new_str;
}
void delete_between(const char* c_open, const char* c_close) {
MString new_str = "";
int current_level = 0;
for (size_t i = 0; i < this->size(); ++i) {
if (current_level < 0) return;
if ((*this)[i] == *c_open) {
++current_level;
} else if ((*this)[i] == *c_close) {
--current_level;
} else if (current_level == 0) {
new_str += (*this)[i];
}
}
*this = new_str;
}
};
}