From 16964a3add8fc1cbe5274188a0636dfcde4450ea Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 19 Sep 2023 08:48:43 +0000 Subject: [PATCH] ex00: finish --- ex00/src/ScalarConverter.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/ex00/src/ScalarConverter.cpp b/ex00/src/ScalarConverter.cpp index 392fc6b..2c81521 100644 --- a/ex00/src/ScalarConverter.cpp +++ b/ex00/src/ScalarConverter.cpp @@ -8,7 +8,9 @@ #include #include +#include #include +#include ScalarConverter::ScalarConverter() {} @@ -31,10 +33,10 @@ ScalarConverter& ScalarConverter::operator=(const ScalarConverter& src) double get_value(const std::string& str) { char *pos; + if (str.length() == 3 && str[0] == '\'' && str[2] == '\'') + return str[1]; double value = std::strtod(str.c_str(), &pos); - if (pos) - std::cout << pos << std::endl; - if (errno == ERANGE) + if (errno == ERANGE || (pos != NULL && strcmp(pos, "f"))) throw std::exception(); return value; } @@ -42,7 +44,9 @@ double get_value(const std::string& str) void convert2char(const double value) { std::cout << "char: "; - if (isprint(static_cast(value))) + if (value > CHAR_MAX || CHAR_MIN > value) + std::cout << "imposible"; + else if (isprint(static_cast(value))) std::cout << static_cast(value); else std::cout << "Non displayable"; @@ -52,13 +56,23 @@ void convert2char(const double value) void convert2int(const double value) { std::cout << "int: "; - if (value > INT_MAX) + if (value > INT_MAX || INT_MIN > value) std::cout << "imposible"; else std::cout << static_cast(value); std::cout << std::endl; } +void convert2float(const double value) +{ + std::cout << "float: "; + if (value > FLT_MAX || FLT_MIN > value) + std::cout << "imposible"; + else + std::cout << static_cast(value); + std::cout << std::endl; +} + void ScalarConverter::convert(const std::string &str) { double value; @@ -70,4 +84,6 @@ void ScalarConverter::convert(const std::string &str) } convert2char(value); convert2int(value); + convert2float(value); + std::cout << "double: " << value << std::endl; }