diff --git a/cpp06/ex00/Makefile b/cpp06/ex00/Makefile index bb88dba..a54792d 100644 --- a/cpp06/ex00/Makefile +++ b/cpp06/ex00/Makefile @@ -26,13 +26,16 @@ CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ CXXFLAGS += -MMD # Flags to have the debug (can be removed for correction) DEBUG = -g3 -DEBUG += -fsanitize=address +# DEBUG += -fsanitize=address CXXFLAGS += $(DEBUG) # Sources SRC = sources/char.cpp \ - sources/ScalarConverter.cpp \ + sources/exeception.cpp \ + sources/float.cpp \ sources/integer.cpp \ + sources/double.cpp \ + sources/ScalarConverter.cpp \ main.cpp # Objects diff --git a/cpp06/ex00/sources/ScalarConverter.cpp b/cpp06/ex00/sources/ScalarConverter.cpp index 7b23fda..34792ef 100644 --- a/cpp06/ex00/sources/ScalarConverter.cpp +++ b/cpp06/ex00/sources/ScalarConverter.cpp @@ -6,17 +6,11 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 16:41:16 by rparodi #+# #+# */ -/* Updated: 2025/03/23 15:01:09 by rparodi ### ########.fr */ +/* Updated: 2025/03/24 15:07:58 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" -#include -#include -#include -#include -#include -#include ScalarConverter::ScalarConverter() { std::cout << "[ScalarConverter] Default constructor called" << std::endl; @@ -38,19 +32,34 @@ ScalarConverter& ScalarConverter::operator = (ScalarConverter const &assign) { } void ScalarConverter::convert(std::string *input) { - // std::cout << "Input:\t\t" << *input << std::endl; - if (isChar(input->c_str())) + if (*input == "nan" || *input == "nanf") + not_a_number(); + else if (*input == "-inf" || *input == "-inff") + minus_infinity(); + else if (*input == "+inf" || *input == "+inff") + plus_infinity(); + else if (isChar(input->c_str())) { convertChar(*input); - else if (isInt(input->c_str())) - convertInt(*input); - // else if (isFloat(input)) - // convertFloat(*input); - // else if (isDouble(input)) - // convertDouble(*input); - // else - // std::cout << "Error: Invalid input" << std::endl; - // std::cout << "IsChar:\t\t" << (isChar(input->c_str()) ? "✅ | true" : "❌ | false") << std::endl; - // std::cout << "IsInt:\t\t" << (isInt(input->c_str()) ? "✅ | true" : "❌ | false") << std::endl; - // std::cout << "IsFloat:\t" << (isFloat(input) ? "✅ | true" : "❌ | false") << std::endl; - // std::cout << "IsDouble:\t" << (isDouble(input) ? "✅ | true" : "❌ | false") << std::endl; + } + else if (isInt(input->c_str())) { + try { + convertInt(*input); + } catch (std::exception &e) { + std::cerr << CLR_RED << e.what() << CLR_RESET << std::endl; + return; + } + } + else if (isFloat(input->c_str())) { + convertFloat(*input); + } + else if (isDouble(input->c_str())) { + convertDouble(*input); + } + else + { + std::cout << CLR_BLUE << "Char:\t" << CLR_RED << " Impossible" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Int:\t" << CLR_RED << " Impossible" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Float:\t" << CLR_RED << " Impossible" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Double:\t" << CLR_RED << " Impossible" << CLR_RESET << std::endl; + } } diff --git a/cpp06/ex00/sources/char.cpp b/cpp06/ex00/sources/char.cpp index 299611a..24ab751 100644 --- a/cpp06/ex00/sources/char.cpp +++ b/cpp06/ex00/sources/char.cpp @@ -6,11 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/22 22:06:48 by rparodi #+# #+# */ -/* Updated: 2025/03/22 22:10:24 by rparodi ### ########.fr */ +/* Updated: 2025/03/24 14:38:51 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" +#include "color.hpp" bool isChar(std::string const &str) { if (str.length() == 1 && !std::isdigit(str[0])) { @@ -21,11 +22,8 @@ bool isChar(std::string const &str) { void convertChar(std::string const &str) { char toChar = str[0]; - int toInt = static_cast(toChar); - float toFloat = static_cast(toChar); - double toDouble = static_cast(toChar); - std::cout << "Char: '" << toChar << "'" << std::endl; - std::cout << "Int: " << toInt << std::endl; - std::cout << "Float: " << toFloat << ".0f" << std::endl; - std::cout << "Double: " << toDouble << ".0" << std::endl; + std::cout << CLR_BLUE << "Char:\t'" << CLR_GOLD << toChar << CLR_BLUE << "'" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Int:\t" << CLR_GOLD << static_cast(toChar) << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Float:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast(toChar) << "f" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Double:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast(toChar) << CLR_RESET << std::endl; } diff --git a/cpp06/ex00/sources/integer.cpp b/cpp06/ex00/sources/integer.cpp index f93e036..c36c9e6 100644 --- a/cpp06/ex00/sources/integer.cpp +++ b/cpp06/ex00/sources/integer.cpp @@ -6,37 +6,33 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/22 22:11:44 by rparodi #+# #+# */ -/* Updated: 2025/03/23 18:44:01 by rparodi ### ########.fr */ +/* Updated: 2025/03/24 15:14:33 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" -#include -#include bool isInt(std::string const &str) { - long long tmp = std::atoll(str.c_str()); - if (tmp > INT_MAX || tmp < INT_MIN) + char *end; + std::strtol(str.c_str(), &end, 10); + if (*end) return false; - if (str.empty() && (str.at(0) == '-' || str.at(0) == '+')) - return false; - for (size_t i = 0; i < str.length(); i++) { - if (str[0] == '-' || str[0] == '+') - i++; - else if (!std::isdigit(str[i])) - return false; - } return true; } void convertInt(std::string const &str) { - int integer = std::atoi(str.c_str()); - std::cout << "char: "; - if (std::isprint(integer)) - std::cout << "'" << static_cast(integer) << "'" << std::endl; - else - std::cout << "Non displayable" << std::endl; - std::cout << "int: " << integer << std::endl; - std::cout << "float: " << static_cast(integer) << ".0f" << std::endl; - std::cout << "double: " << static_cast(integer) << ".0" << std::endl; + long long integer = std::atoll(str.c_str()); + if (integer < INT_MIN || integer > INT_MAX) + { + std::cout << CLR_RED << "Error: Integer out of range" << CLR_RESET << std::endl; + return; + } + std::cout << CLR_BLUE << "Char:\t" << CLR_RESET; + if (integer > -128 && integer < 127 && std::isprint(integer)) + std::cout << CLR_GOLD << "'" << static_cast(integer) << "'" << CLR_RESET << std::endl; + else + std::cout << CLR_RED << "Non-Printable" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Int:\t" << CLR_GOLD << static_cast(integer) << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Float:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast(integer) << "f" << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Double:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast(integer) << CLR_RESET << std::endl; }