From cc93fd4bded2e62198731e316a4ef8fae674334e Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 22 Jan 2025 12:45:29 +0100 Subject: [PATCH] Implement ex00 I despise this exercise. Hence there is a lot to be improved. --- ex00/ScalarConverter.cpp | 65 ++++++++++++++++++++++++++++++++++++++++ ex00/ScalarConverter.h | 12 ++++++++ ex00/main.cpp | 17 +++++++++++ 3 files changed, 94 insertions(+) create mode 100644 ex00/ScalarConverter.cpp create mode 100644 ex00/ScalarConverter.h create mode 100644 ex00/main.cpp diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp new file mode 100644 index 0000000..8b1d48e --- /dev/null +++ b/ex00/ScalarConverter.cpp @@ -0,0 +1,65 @@ +#include "ScalarConverter.h" +#include +#include +#include +#include +#include +#include + +bool ScalarConverter::convert(const std::string &str) +{ + double d_rep; + int i_rep; + char *after_last_digit; + std::stringstream int_string_stream; + + if (str.size() == 3 && str[0] == '\'' && str[2] == '\'') + { + i_rep = str[1]; + d_rep = i_rep; + } + else if (str == "nan" || str == "nanf") + d_rep = NAN; + else if (str == "inf" || str == "inff" || str == "+inf" || str == "+inff") + d_rep = INFINITY; + else if (str == "-inf" || str == "-inff") + d_rep = -INFINITY; + else if (str.find('e') != std::string::npos || str.find('f') != std::string::npos || str.find('.') != std::string::npos) + { + d_rep = std::strtod(str.c_str(), &after_last_digit); + if (*after_last_digit && *after_last_digit == 'f') + d_rep = static_cast(d_rep); + else if (*after_last_digit) + return (1); + i_rep = static_cast(d_rep); + } + else + { + i_rep = std::atoi(str.c_str()); + int_string_stream << i_rep; + if (int_string_stream.str() != str) + d_rep = NAN; + else + d_rep = i_rep; + } + if (d_rep < INT_MIN || d_rep > INT_MAX || std::isnan(d_rep)) + { + std::cout + << "char: impossible\n" + << "int: impossible\n"; + } + else + { + if (i_rep < 0 || i_rep > ((1 << 8) - 1)) + std::cout << "char: impossible\n"; + else if (!std::isprint(i_rep)) + std::cout << "char: non-printable\n"; + else + std::cout << "char: '" << static_cast(i_rep) << "'\n"; + std::cout << "int: " << i_rep << '\n'; + } + std::cout + << "float: " << std::setprecision(10) << std::showpoint << std::fixed << static_cast(d_rep) << "f\n" + << "double: " << std::setprecision(10) << std::showpoint << std::fixed << d_rep << '\n'; + return (0); +} diff --git a/ex00/ScalarConverter.h b/ex00/ScalarConverter.h new file mode 100644 index 0000000..128c729 --- /dev/null +++ b/ex00/ScalarConverter.h @@ -0,0 +1,12 @@ +#ifndef SCALARCONVERTER_H +# define SCALARCONVERTER_H + +# include + +class ScalarConverter +{ + public: + static bool convert(const std::string &str); +}; + +#endif // SCALARCONVERTER_H diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..6e2f5a7 --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,17 @@ +#include "ScalarConverter.h" +#include + +int main(int argc, char **argv) +{ + if (argc != 2) + { + std::cout << "Incorrect number of arguments.\n"; + return (1); + } + if (ScalarConverter::convert(argv[1])) + { + std::cout << "Incorrect input string.\n"; + return (1); + } + return (0); +} -- 2.30.2