Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
51 lines (46 loc) · 1.41 KB
/
Copy pathmain.cpp
File metadata and controls
51 lines (46 loc) · 1.41 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
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include "sort.cpp"
int main(int argc, char* argv[]) {
if (argc < 2) { std::cout << "Requared file\n"; exit(1); }
std::string fileName;
std::string sort_by_numbers_flag;
for (int i = 0; i < argc; i++) {
if (std::regex_match(argv[i], std::regex("\\/(\\w+\\/)+\\w+\\.\\w{1,3}$")))
fileName = argv[i];
if (strcmp(argv[i], "-n") == 0)
sort_by_numbers_flag = argv[i];
}
std::fstream *file = new std::fstream(fileName, std::ios::in);
if (!file->is_open()) {
std::cout << "Cannot open file. Be sure if file name is givven write.\n";
delete file;
exit(1);
}
if (!sort_by_numbers_flag.empty()) {
std::vector<int> text;
std::string temp;
int num;
while (getline(*file, temp)) {
if (num = atoi(temp.c_str()))
text.push_back(num);
}
quick_sort(text, 0 ,text.size());
for(int i = 0; i < text.size(); i++)
std::cout << text[i] << std::endl;
}
else {
std::vector<std::string> text;
std::string temp;
while (getline(*file, temp))
text.push_back(temp);
quick_sort(text, 0 ,text.size());
for(int i = 0; i < text.size(); i++)
std::cout << text[i] << std::endl;
}
file->close();
delete file;
return 0;
}