-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
331 lines (277 loc) · 10.5 KB
/
main.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
// sort | (c) 2020 Icinga GmbH | GPLv2+
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <random>
#include <vector>
#include <set>
#include <map>
#include <boost/lexical_cast.hpp>
using std::cout;
template<typename T>
void bubbleSort(std::vector<T> *arr, std::size_t length,
bool (*funcCallback)(std::string *, std::string *));
template<typename T>
void quickSort(std::vector<T> *arr, int low, int high,
bool (*funcCallback)(std::string *, std::string *));
template<typename T>
void mergeSort(std::vector<T> *arr, int begin, int vecSize,
bool (*funcCallback)(std::string *, std::string *));
template<typename T>
void printSortedArray(const std::vector<T> &arr);
bool checkForEquality(std::string *firstNum, std::string *secondNum);
bool checkNumeric(std::string *firstNum, std::string *secondNum);
template<typename T, typename Key>
bool key_exists(const T &container, const Key &key);
template<typename Key, typename Value>
bool findByValue(std::map<Key, Value> mapOfElemen, Value value);
void printHelp();
int main(int argc, char **argv) {
std::vector<std::string> stringVector;
std::string fileName;
std::map<std::string, std::string> mapOption{};
int options, optionIndex = 0;
bool (*funcCallback)(std::string *, std::string *) = checkForEquality;
static struct option $longOptions[] = {
{"numeric-sort", no_argument, nullptr, 'n'},
{"output", required_argument, nullptr, 'o'},
{"unique", no_argument, nullptr, 'u'},
{"ignore-leading-blanks", no_argument, nullptr, 'b'},
{"ignore-case", no_argument, nullptr, 'f'},
{"random-sort", no_argument, nullptr, 'R'},
{"reverse", no_argument, nullptr, 'r'},
{"quick-sort", no_argument, nullptr, 'q'},
{"merge-sort", no_argument, nullptr, 'm'},
{"help", no_argument, nullptr, 'h'},
};
while ((options = getopt_long(argc, argv, "no:ubfRrqmh",
$longOptions, &optionIndex)) != -1) {
switch (options) {
case 'n':
mapOption.insert(std::make_pair("n", "numeric-sort"));
funcCallback = checkNumeric;
break;
case 'o':
mapOption.insert(std::make_pair("o", "output"));
fileName = optarg;
break;
case 'u':
mapOption.insert(std::make_pair("u", "unique"));
break;
case 'b':
mapOption.insert(std::make_pair("b", "ignore-leading-blanks"));
break;
case 'f':
mapOption.insert(std::make_pair("f", "ignore-case"));
break;
case 'R':
mapOption.insert(std::make_pair("R", "random-sort"));
break;
case 'r':
mapOption.insert(std::make_pair("r", "reverse"));
break;
case 'q':
mapOption.insert(std::make_pair("q", "quick-sort"));
break;
case 'm':
mapOption.insert(std::make_pair("m", "merge-sort"));
break;
case 'h':
mapOption.insert(std::make_pair("h", "help"));
break;
default: /* For invalid option e.g '?' */
exit(EXIT_FAILURE);
}
}
if (key_exists(mapOption, "h")) {
printHelp();
}
if (argv[optind] != nullptr) {
std::fstream file{argv[optind]};
for (std::string line; getline(file, line);) {
if (key_exists(mapOption, "b") && line.empty()) {
continue;
}
stringVector.emplace_back(line);
}
file.close();
} else {
for (std::string line; getline(std::cin, line);) {
if (key_exists(mapOption, "b") && line.empty()) {
continue;
}
stringVector.emplace_back(line);
}
}
if (key_exists(mapOption, "u")) {
std::set<std::string> s(stringVector.begin(), stringVector.end());
stringVector.assign(s.begin(), s.end());
} else if (key_exists(mapOption, "f")) {
for (auto &line : stringVector) {
for (auto &c : line) {
c = toupper(c);
}
}
}
int vectorSize = stringVector.size();
if (key_exists(mapOption, "R")) {
std::random_device randomDevice;
shuffle(stringVector.begin(), stringVector.end(), randomDevice);
} else if (key_exists(mapOption, "q")) {
quickSort(&stringVector, 0, vectorSize - 1, funcCallback);
} else if (key_exists(mapOption, "m")) {
mergeSort(&stringVector, 0, vectorSize - 1, funcCallback);
} else {
bubbleSort(&stringVector, vectorSize, funcCallback);
}
if (key_exists(mapOption, "r")) {
reverse(stringVector.begin(), stringVector.end());
}
if (key_exists(mapOption, "o")) {
std::ofstream file{fileName};
if (file.is_open() && file.good()) {
for (const auto &line : stringVector) {
file << line << std::endl;
}
file.close();
} else {
printf("Could not open file name %s", fileName.c_str());
exit(EXIT_FAILURE);
}
} else {
printSortedArray(stringVector);
}
return EXIT_SUCCESS;
}
template<typename T>
void bubbleSort(std::vector<T> *arr, std::size_t length,
bool (*funcCallback)(std::string *, std::string *)) {
bool isSwapped = true;
for (std::size_t i{0}; (i < length - 1) && (isSwapped); i++) {
isSwapped = false;
for (std::size_t j{0}; j < (length - i) - 1; j++) {
if (funcCallback(&arr->at(j + 1), &arr->at(j))) {
swap(arr->at(j + 1), arr->at(j));
isSwapped = true;
}
}
}
}
template<typename T>
void quickSort(std::vector<T> *arr, int low, int high,
bool (*funcCallback)(std::string *, std::string *)) {
if (low < high) {
std::string pivot = arr->at(high);
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (funcCallback(&arr->at(j), &pivot)) {
i++;
swap(arr->at(i), arr->at(j));
}
}
swap(arr->at(i + 1), arr->at(high));
int newPivot = i + 1;
quickSort(arr, low, newPivot - 1, funcCallback);
quickSort(arr, newPivot + 1, high, funcCallback);
}
}
template<typename T>
void mergeSort(std::vector<T> *arr, int begin, int vecSize,
bool (*funcCallback)(std::string *, std::string *)) {
if (begin >= vecSize) {
return;
}
int middle = (begin + vecSize) / 2;
mergeSort(arr, begin, middle, funcCallback);
mergeSort(arr, middle + 1, vecSize, funcCallback);
int i = begin, j = middle + 1;
int l = vecSize - begin + 1;
T *tempVec = new T[l];
for (int k{}; k < l; k++) {
if (j > vecSize || (i <= middle &&
funcCallback(&arr->at(i), &arr->at(j)))) {
tempVec[k] = arr->at(i);
i++;
} else {
tempVec[k] = arr->at(j);
j++;
}
}
for (int k = 0, n = begin; k < l; k++, n++) {
arr->at(n) = tempVec[k];
}
delete[] tempVec;
}
template<typename T>
void printSortedArray(const std::vector<T> &arr) {
for (const auto &num : arr) {
cout << num << std::endl;
}
}
bool checkForEquality(std::string *firstNum, std::string *secondNum) {
return *firstNum < *secondNum;
}
bool checkNumeric(std::string *firstNum, std::string *secondNum) {
if (firstNum->empty() || secondNum->empty()) {
return false;
}
char character2, character1;
std::string::const_iterator it = firstNum->begin();
std::string::const_iterator iterator = secondNum->begin();
while (it != firstNum->end() && isdigit(*it)) ++it;
while (iterator != secondNum->end() && isdigit(*iterator)) ++iterator;
if (!firstNum->empty() && it == firstNum->end() &&
!secondNum->empty() && iterator == secondNum->end()) {
return boost::lexical_cast<int>(*firstNum) <
boost::lexical_cast<int>(*secondNum);
} else {
for (auto &lin : *firstNum) {
character1 = lin;
}
for (auto &line : *secondNum) {
character2 = line;
}
return static_cast<int>(character1) < static_cast<int>(character2);
}
}
template<typename T, typename Key>
bool key_exists(const T &container, const Key &key) {
return container.find(key) != end(container);
}
template<typename Key, typename Value>
bool findByValue(std::map<Key, Value> mapOfElemen, Value value) {
auto iterator = mapOfElemen.begin();
while (iterator != mapOfElemen.end()) {
if (iterator->second == value) {
return true;
}
iterator++;
}
return false;
}
void printHelp() {
cout << "Usage: Program PATH [OPTION] < [FILE] ...\n"
" or: Program PATH [OPTION] file name\n"
"If you execute the sort commands without "
"the o parameter and a new text file,\n"
"it simply uses the standard output.\n\n";
cout << "Arguments required for long options "
"are also required for short ones.\n"
"Sort options:\n\n";
cout << "-b, --ignore-leading-blanks Ignore leading spaces\n"
"-f, --ignore-case Treat lower case as upper case\n"
"-n, --numeric-sort Sort by numeric value\n"
"-R, --random-sort Use a random hash to sort the keys. See shuf(1)\n"
"-r, --reverse Reverse the result of the sorting output\n"
"-o, --output=FILE Write result to FILE instead of standard output\n"
"-m, --merge-sort Use merge sort Algorithm\n"
"-u, --unique Only the first of output several equals\n"
"-q, --quick-sort Use Quick sort Algorithm\n"
"-h, --help Display this help and the program will be exited\n";
cout << "\n\nif you have any problem executing "
"commands you can open an issue \n"
"in github or you can fix it yourself "
"by opening a pull request \n\n"
"Thanks for using my sort program :)\n";
exit(EXIT_SUCCESS);
}