Something went wrong. Try again.
Java--'s containers projects.intra.42.fr/projects/cpp-module-09
Something went wrong. Try again.
1.7 kB · 62 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include <PmergeMe.hpp>#include <cstdlib>#include <iomanip>#include <iostream>#include <limits>#include <sstream>#include <string>
int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <positive integers...>" << std::endl; return 1; }
std::cout << std::fixed << std::setprecision(5);
std::vector<int> vector; try { if (argc == 2) { std::string input = argv[1]; std::istringstream inputStream(input); std::string string; while (inputStream >> string) { for (size_t i = 0; i < string.size(); ++i) if (!std::isdigit(static_cast<unsigned char>(string[i]))) throw std::runtime_error("Error: not a number");
long number = std::strtol(string.c_str(), NULL, 10);
if (number < 0) throw std::runtime_error("Error: negative number");
if (number > std::numeric_limits<int>::max()) throw std::runtime_error("Error: too large");
vector.push_back(static_cast<int>(number)); } } else { for (int i = 1; i < argc; ++i) { for (size_t j = 0; argv[i][j] != '\0'; ++j) if (!std::isdigit(static_cast<unsigned char>(argv[i][j]))) throw std::runtime_error("Error: not a number");
long int number = std::strtol(argv[i], NULL, 10);
if (number < 0) throw std::runtime_error("Error: negative number");
if (number > std::numeric_limits<int>::max()) throw std::runtime_error("Error: too large");
vector.push_back(static_cast<int>(number)); } } } catch (const std::exception& exception) { std::cerr << exception.what() << std::endl; return EXIT_FAILURE; } std::deque<int> deque(vector.begin(), vector.end());
PmergeMe::sort(vector, deque);}