Skip to content

Commit 5365caf

Browse files
committed
ex02 done
1 parent 8f625f3 commit 5365caf

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

cpp_module_09/ex02/PmergeMe.cpp

+27-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,28 @@ std::deque<unsigned long> PmergeMe::deq_sort(std::string sequence)
3838

3939
}
4040

41-
// std::string PmergeMe::parse_and_check_input(char **input)
42-
// {
43-
44-
45-
// }
41+
std::string PmergeMe::parse_and_check_input(char **input)
42+
{
43+
std::string str;
44+
input++;
45+
while (*input)
46+
{
47+
str += *input++;
48+
str += " ";
49+
}
50+
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
51+
{
52+
while (*it != ' ')
53+
{
54+
if (*it == '+' && (*(it + 1) > '0' && *(it + 1) < '9'))
55+
break;
56+
if (*it == '-' || *it < '0' || *it > '9')
57+
throw std::exception();
58+
++it;
59+
}
60+
}
61+
return (str);
62+
}
4663

4764
void PmergeMe::sort_sequence(std::string sequence)
4865
{
@@ -58,6 +75,8 @@ void PmergeMe::sort_sequence(std::string sequence)
5875
{
5976
while (*it == ' ')
6077
++it;
78+
if (!*it)
79+
break;
6180
while (*it != ' ')
6281
std::cout << *it++;
6382
std::cout << " ";
@@ -66,7 +85,10 @@ void PmergeMe::sort_sequence(std::string sequence)
6685
for (std::vector<unsigned long>::iterator it = vec.begin(); it != vec.end(); ++it)
6786
{
6887
if (*it == *vec.rbegin())
88+
{
6989
std::cout << *it << std::endl;
90+
break;
91+
}
7092
std::cout << *it << " ";
7193
}
7294
std::cout << "Time to process a range of " << vec.size() << " elements with std::vector : " << std::fixed << std::setprecision(3) << 1000 * (vec_time / (double)CLOCKS_PER_SEC) << " ms" << std::endl;

cpp_module_09/ex02/main.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
int main(int argc, char **argv)
44
{
5-
// if (argc < 2)
6-
// return (std::cout << "Error: no input" << std::endl, 1);
7-
// std::string sequence(PmergeMe::parse_and_check_input(argv));
8-
(void)argc;
9-
(void)argv;
10-
std::string sequence(" 12 34 456456 0 2323 2 55 7 ");
11-
PmergeMe::sort_sequence(sequence);
5+
if (argc < 2)
6+
return (std::cout << "Error: no input" << std::endl, 1);
7+
try
8+
{
9+
std::string sequence(PmergeMe::parse_and_check_input(argv));
10+
PmergeMe::sort_sequence(sequence);
11+
}
12+
catch (std::exception &e)
13+
{
14+
std::cout << "Error: invalid input" << std::endl;
15+
}
1216
return (0);
1317
}

0 commit comments

Comments
 (0)