From ca8912c56ee3c6f6776b9f9aaa8eb437b5147c80 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 16 Dec 2023 15:27:01 +0100 Subject: [PATCH] ex02: fix: parsing --- ex02/src/PmergeMe.hpp | 8 +-- ex02/src/main.cpp | 110 ++++++++++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 39 deletions(-) diff --git a/ex02/src/PmergeMe.hpp b/ex02/src/PmergeMe.hpp index 548d7c9..8143838 100644 --- a/ex02/src/PmergeMe.hpp +++ b/ex02/src/PmergeMe.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -14,14 +15,13 @@ std::pair* create_pairs(const T& array, size_t len) { std::pair* pairs = new std::pair[len]; + pairs[len - 1].second = -1; for (size_t i = 0; i < len; i++) { - pairs[i].second = array[i * 2]; + pairs[i].first = array[i * 2]; if (array.size() > i * 2 + 1) - pairs[i].first = array[i * 2 + 1]; + pairs[i].second = array[i * 2 + 1]; } - if (len % 2) - pairs[len - 1].first = pairs[len - 1].second; return pairs; } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 6f01395..6bf7b59 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -12,7 +13,72 @@ template void display(const T& array) { for (size_t i = 0; i != array.size(); i++) - std::cout << array[i] << " "; + if (array[i] != -1) + std::cout << array[i] << " "; +} + +template +int parsing_arg(const char* str, T& array) +{ + int number = -1; + for (size_t i = 0; true; i++) + { + if (std::isdigit(str[i])) + { + if (number == -1) + number = 0; + number = number * 10 + str[i] - '0'; + } + else if (str[i] == '\0' || str[i] == ' ') + { + if (number != -1) + { + array.push_back(number); + number = -1; + } + if (str[i] == '\0') + return 0; + } + else + { + std::cout << "Error: " << str[i] << i << " invalid character." << std::endl; + return 1; + } + } +} + +template +int parsing_args(const char* const * args, size_t len, T& array) +{ + for (size_t i = 0; i != len; i++) + { + if (parsing_arg(args[i], array)) + return 1; + } + return 0; +} + +template +int parsing_duplicate(T& array) +{ + for (size_t i = 0; i + 1 != array.size(); i++) + { + for (size_t j = i + 1; j != array.size(); j++) + { + if (array[i] == array[j]) + { + std::cout << "Error: " << array[i] << " is duplicated." << std::endl; + return 1; + } + } + } + return 0; +} + +template +int parsing(const char* const* args, size_t len, T& array) +{ + return parsing_args(args, len, array) || parsing_duplicate(array); } int main(int ac, char** av) @@ -20,21 +86,8 @@ int main(int ac, char** av) { std::vector array; - for (int i = 1; i != ac; i++) - { - for (int j = 0; av[i][j] != '\0'; j++) - { - if (j == 0 || av[i][j] == ' ') - array.push_back(atoi(av[i] + j + (j != 0))); - if (!std::isdigit(av[i][j]) and av[i][j] != ' ') - { - std::cout << "Error" << std::endl; - return 1; - } - } - } - if (array.size() > 0) - array.pop_back(); + if (parsing(av + 1, (size_t) ac - 1, array)) + return 1; std::cout << "Before: "; display(array); @@ -54,28 +107,15 @@ int main(int ac, char** av) { std::deque array; - for (int i = 1; i != ac; i++) - { - for (int j = 0; av[i][j] != '\0'; j++) - { - if (j == 0 || av[i][j] == ' ') - array.push_back(atoi(av[i] + j + (j != 0))); - else if (!std::isdigit(av[i][j])) - { - std::cout << "Error" << std::endl; - return 1; - } - } - } - if (array.size() > 0) - array.pop_back(); + if (parsing(av + 1, (size_t) ac - 1, array)) + return 1; - const clock_t time_deque_start = clock(); + const clock_t time_vec_start = clock(); PmergeMe(array); - const clock_t time_deque_stop = clock(); - - std::cout << "Time to process a range of " << array.size() <<" elements with std::vector : " << ((double) (time_deque_stop - time_deque_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl; + const clock_t time_vec_stop = clock(); + + std::cout << "Time to process a range of " << array.size() <<" elements with std::vector : " << ((double) (time_vec_stop - time_vec_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl; } } \ No newline at end of file