ex02: fix: parsing
This commit is contained in:
parent
0852968609
commit
ca8912c56e
@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@ -14,14 +15,13 @@ std::pair<int, int>* create_pairs(const T& array, size_t len)
|
||||
{
|
||||
std::pair<int, int>* pairs = new std::pair<int, int>[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;
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
@ -12,29 +13,81 @@ template<typename T>
|
||||
void display(const T& array)
|
||||
{
|
||||
for (size_t i = 0; i != array.size(); i++)
|
||||
if (array[i] != -1)
|
||||
std::cout << array[i] << " ";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
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 <typename T>
|
||||
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 <typename T>
|
||||
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)
|
||||
{
|
||||
{
|
||||
std::vector<int> 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;
|
||||
if (parsing(av + 1, (size_t) ac - 1, array))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array.size() > 0)
|
||||
array.pop_back();
|
||||
|
||||
std::cout << "Before: ";
|
||||
display(array);
|
||||
@ -54,28 +107,15 @@ int main(int ac, char** av)
|
||||
{
|
||||
std::deque<int> 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;
|
||||
if (parsing(av + 1, (size_t) ac - 1, array))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array.size() > 0)
|
||||
array.pop_back();
|
||||
|
||||
const clock_t time_deque_start = clock();
|
||||
const clock_t time_vec_start = clock();
|
||||
|
||||
PmergeMe(array);
|
||||
|
||||
const clock_t time_deque_stop = clock();
|
||||
const clock_t time_vec_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;
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user