diff --git a/ex00/GetType.cpp b/ex00/GetType.cpp new file mode 100644 index 0000000..2991d41 --- /dev/null +++ b/ex00/GetType.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* GetType.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/09 09:05:44 by adjoly #+# #+# */ +/* Updated: 2025/04/09 09:18:37 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "GetType.hpp" +#include +#include + +bool _isNan(const std::string &str) { + if (str == "nan" || str == "nanf" || str == "-inf" || str == "-inff" || + str == "+inf" || str == "+inff" || str == "inf" || str == "inff") + return true; + return false; +} + +nbType GetType(const std::string &str) { + if (_isNan(str)) + return NAN; + if (str.length() == 1 && !isdigit(str[0])) + return CHAR; + + char *err; + + (void)strtol(str.c_str(), &err, 10); + if (*err == 0) + return INT; + (void)strtod(str.c_str(), &err); + if (*err == 0) + return DOUBLE; + if (*err == 'f' && *(err + 1) == 0) + return FLOAT; + return NONE; +} diff --git a/ex00/GetType.hpp b/ex00/GetType.hpp new file mode 100644 index 0000000..60a6c51 --- /dev/null +++ b/ex00/GetType.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* GetType.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/09 09:04:39 by adjoly #+# #+# */ +/* Updated: 2025/04/09 09:55:16 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +enum nbType { + CHAR, + INT, + FLOAT, + DOUBLE, + NAN, + NONE +}; + +bool _isNan(const std::string &); + +nbType GetType(const std::string &); diff --git a/ex00/Makefile b/ex00/Makefile index 5636370..62aabd3 100644 --- a/ex00/Makefile +++ b/ex00/Makefile @@ -6,13 +6,13 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/03/30 15:14:32 by adjoly ### ########.fr # +# Updated: 2025/04/09 10:23:51 by adjoly ### ########.fr # # # # **************************************************************************** # SHELL = bash -NAME = Form +NAME = ScalarConverter CC = c++ diff --git a/ex00/PrintNb.hpp b/ex00/PrintNb.hpp index f80ac5f..cd2670e 100644 --- a/ex00/PrintNb.hpp +++ b/ex00/PrintNb.hpp @@ -6,23 +6,54 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/08 14:30:07 by adjoly #+# #+# */ -/* Updated: 2025/04/08 14:30:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/09 10:32:48 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include #include +#include +#include template void _printFloat(T nb) { std::cout << "float: " << static_cast(nb) << "f" << std::endl; } template void _printInt(T nb) { - std::cout << "int: " << static_cast(nb) << std::endl; + std::cout << "int: "; + + if (nb < std::numeric_limits::min() || + nb > std::numeric_limits::max()) + std::cout << "impossible" << std::endl; + else + std::cout << static_cast(nb) << std::endl; } template void _printChar(T nb) { - std::cout << "char: '" << static_cast(nb) << "'" << std::endl; + std::cout << "char: "; + if (nb < std::numeric_limits::min() || + nb > std::numeric_limits::max()) + std::cout << "impossible" << std::endl; + else if (!isprint(nb)) + std::cout << "Non displayable" << std::endl; + else + std::cout << "'" << static_cast(nb) << "'" << std::endl; } template void _printDouble(T nb) { std::cout << "double: " << static_cast(nb) << std::endl; } +template T _ConvertTo(const std::string &str) { + std::stringstream ss(str); + T ret; + + ss >> ret; + return ret; +} +template void _Convert(const std::string &str) { + T nb = _ConvertTo(str); + + _printChar(nb); + _printInt(nb); + _printFloat(nb); + _printDouble(nb); +} diff --git a/ex00/ScalarConverter b/ex00/ScalarConverter new file mode 100755 index 0000000..2eb3a36 Binary files /dev/null and b/ex00/ScalarConverter differ diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp index 0dce6c1..b68bcf4 100644 --- a/ex00/ScalarConverter.cpp +++ b/ex00/ScalarConverter.cpp @@ -6,11 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:59:17 by adjoly #+# #+# */ -/* Updated: 2025/04/08 14:17:44 by adjoly ### ########.fr */ +/* Updated: 2025/04/09 10:31:41 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" +#include "GetType.hpp" +#include "PrintNb.hpp" +#include "StrTo.hpp" void _log(std::string emoji, std::string what, std::string who, std::string str) { @@ -40,8 +43,47 @@ ScalarConverter::~ScalarConverter(void) { _log("➖", "ScalarConverter", "", "destructor called"); } -ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) { return *this; } - -void ScalarConverter::convert(std::string &s) { - +ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) { + return *this; +} + +void _ConvertNan(const std::string &str) { + std::string nb; + + if (str[0] == 'n') + nb = "nan"; + else if (str[0] == '-') + nb = "-inf"; + else + nb = "+inf"; + + std::cout << "char: impossible" << std::endl; + std::cout << "int: impossible" << std::endl; + std::cout << "float: " << nb << "f" << std::endl; + std::cout << "double: " << nb << std::endl; +} + +void ScalarConverter::convert(const std::string &s) { + nbType type = GetType(s); + + switch (type) { + case CHAR: + _Convert(s); + break; + case INT: + _Convert(s); + break; + case FLOAT: + _Convert(s); + break; + case DOUBLE: + _Convert(s); + break; + case NAN: + _ConvertNan(s); + break; + case NONE: + std::cout << s << " does not convert to any type" << std::endl; + break; + }; } diff --git a/ex00/ScalarConverter.hpp b/ex00/ScalarConverter.hpp index 822c9b3..f7daad2 100644 --- a/ex00/ScalarConverter.hpp +++ b/ex00/ScalarConverter.hpp @@ -6,18 +6,17 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:38:39 by adjoly #+# #+# */ -/* Updated: 2025/04/08 14:29:28 by adjoly ### ########.fr */ +/* Updated: 2025/04/09 10:31:49 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include #include class ScalarConverter { public: - static void convert(std::string &); + static void convert(const std::string &); private: ScalarConverter(void); @@ -25,17 +24,4 @@ class ScalarConverter { ~ScalarConverter(void); ScalarConverter &operator=(const ScalarConverter &); - - template void _printFloat(T nb) { - std::cout << "float: " << static_cast(nb) << "f" << std::endl; - } - template void _printInt(T nb) { - std::cout << "int: " << static_cast(nb) << std::endl; - } - template void _printChar(T nb) { - std::cout << "char: '" << static_cast(nb) << "'" << std::endl; - } - template void _printDouble(T nb) { - std::cout << "double: " << static_cast(nb) << std::endl; - } }; diff --git a/ex00/StrTo.cpp b/ex00/StrTo.cpp new file mode 100644 index 0000000..fd8ecdb --- /dev/null +++ b/ex00/StrTo.cpp @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* StrTo.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/09 09:59:23 by adjoly #+# #+# */ +/* Updated: 2025/04/09 10:19:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "StrTo.hpp" +#include +#include + diff --git a/ex00/StrTo.hpp b/ex00/StrTo.hpp new file mode 100644 index 0000000..af174af --- /dev/null +++ b/ex00/StrTo.hpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* StrTo.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/09 09:24:16 by adjoly #+# #+# */ +/* Updated: 2025/04/09 10:19:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +char _ToChar(const std::string &); +int _ToInt(const std::string &); +float _ToFloat(const std::string &); +double _ToDouble(const std::string &); diff --git a/ex00/main.cpp b/ex00/main.cpp index dcdb068..0f1d542 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -6,12 +6,17 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:38:09 by adjoly #+# #+# */ -/* Updated: 2025/04/03 19:51:28 by adjoly ### ########.fr */ +/* Updated: 2025/04/09 10:31:55 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" +#include -int main(void) { - +int main(int ac, char **av) { + if (ac < 2) { + std::cout << "needs an input ! :D" << std::endl; + return 1; + } + ScalarConverter::convert(av[1]); }