diff --git a/ex00/ScalarConverter b/ex00/ScalarConverter new file mode 100755 index 0000000..c726990 Binary files /dev/null and b/ex00/ScalarConverter differ diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp index 3d9a231..3f0d1b7 100644 --- a/ex00/ScalarConverter.cpp +++ b/ex00/ScalarConverter.cpp @@ -1,5 +1,5 @@ #include "ScalarConverter.hpp" - +#include ScalarConverter::ScalarConverter() { @@ -45,6 +45,12 @@ bool ScalarConverter::isInt(const std::string& input) return false; i++; } + char* end; + long value = strtol(input.c_str(), &end, 10); + + if (value < std::numeric_limits::min() || + value > std::numeric_limits::max()) + return false; return true; } @@ -63,13 +69,19 @@ bool ScalarConverter::isFloat(const std::string& input) hasDigit = true; else if (input[i] == '.' && !hasPoint) hasPoint = true; - else if (input[i] == 'f' && i == input.length() - 1 && hasDigit) - return true; + else if (input[i] == 'f' && i == input.length() - 1 && hasDigit && hasPoint) + { + float f = static_cast(atof(input.c_str())); + + if (std::isinf(f)) + return false; + return true; + } else return false; i++; } - return (hasDigit && hasPoint); + return false; } bool ScalarConverter::isDouble(const std::string& input) @@ -91,6 +103,12 @@ bool ScalarConverter::isDouble(const std::string& input) return false; i++; } + double d = atof(input.c_str()); + std::cout << "DEBUG double: d = " << d << std::endl; // ← AJOUTE + std::cout << "DEBUG isinf: " << std::isinf(d) << std::endl; // ← AJOUTE + if (std::isinf(d)) + return false; + return (hasDigit && hasPoint); } bool ScalarConverter::isOther(const std::string &input) @@ -108,6 +126,8 @@ bool ScalarConverter::isOther(const std::string &input) void ScalarConverter::toChar(const std::string &input) { char c = input[0]; + if (input.size() == 3 && input[0] == '\'' && input[2] == '\'') + c = input[1]; int i = static_cast(c); float f = static_cast(c); double d = static_cast(c); @@ -127,20 +147,25 @@ void ScalarConverter::toChar(const std::string &input) void ScalarConverter::toInt(const std::string &input) { - int i = atoi(input.c_str()); - if (i >= std::numeric_limits::min() && i <= std::numeric_limits::max()) - { - char c = static_cast(i); - if (isprint(c)) - std::cout << "char: \t'" << c << "'" << std::endl; - else - std::cout << "char: \tnon displayable" << std::endl; - } - else - std::cout << "char: \timpossible" << std::endl; - std::cout << "int: \t" << i << std::endl; - std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast(i) << "f" << std::endl; - std::cout << "double: \t" << static_cast(i) << std::endl; + char* end; + long temp = strtol(input.c_str(), &end, 10); + int i = static_cast(temp); + if (i >= std::numeric_limits::min() && + i <= std::numeric_limits::max()) + { + char c = static_cast(i); + if (isprint(c)) + std::cout << "char: \t'" << c << "'" << std::endl; + else + std::cout << "char: \tnon displayable" << std::endl; + } + else + std::cout << "char: \timpossible" << std::endl; + + std::cout << "int: \t" << i << std::endl; + std::cout << "float: \t" << std::fixed << std::setprecision(1) + << static_cast(i) << "f" << std::endl; + std::cout << "double: \t" << static_cast(i) << std::endl; } void ScalarConverter::toFloat(const std::string &input) diff --git a/ex00/obj/Main.o b/ex00/obj/Main.o new file mode 100644 index 0000000..b4aff79 Binary files /dev/null and b/ex00/obj/Main.o differ diff --git a/ex00/obj/ScalarConverter.o b/ex00/obj/ScalarConverter.o new file mode 100644 index 0000000..9d903c4 Binary files /dev/null and b/ex00/obj/ScalarConverter.o differ