feat: finishing the ex00 of the module 06

This commit is contained in:
Raphael 2025-03-24 15:17:27 +01:00
parent d32b496a87
commit 6631b3042a
4 changed files with 59 additions and 53 deletions

View file

@ -26,13 +26,16 @@ CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
CXXFLAGS += -MMD CXXFLAGS += -MMD
# Flags to have the debug (can be removed for correction) # Flags to have the debug (can be removed for correction)
DEBUG = -g3 DEBUG = -g3
DEBUG += -fsanitize=address # DEBUG += -fsanitize=address
CXXFLAGS += $(DEBUG) CXXFLAGS += $(DEBUG)
# Sources # Sources
SRC = sources/char.cpp \ SRC = sources/char.cpp \
sources/ScalarConverter.cpp \ sources/exeception.cpp \
sources/float.cpp \
sources/integer.cpp \ sources/integer.cpp \
sources/double.cpp \
sources/ScalarConverter.cpp \
main.cpp main.cpp
# Objects # Objects

View file

@ -6,17 +6,11 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 16:41:16 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 "ScalarConverter.hpp"
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <sstream>
#include <string>
ScalarConverter::ScalarConverter() { ScalarConverter::ScalarConverter() {
std::cout << "[ScalarConverter] Default constructor called" << std::endl; std::cout << "[ScalarConverter] Default constructor called" << std::endl;
@ -38,19 +32,34 @@ ScalarConverter& ScalarConverter::operator = (ScalarConverter const &assign) {
} }
void ScalarConverter::convert(std::string *input) { void ScalarConverter::convert(std::string *input) {
// std::cout << "Input:\t\t" << *input << std::endl; if (*input == "nan" || *input == "nanf")
if (isChar(input->c_str())) 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); convertChar(*input);
else if (isInt(input->c_str())) }
convertInt(*input); else if (isInt(input->c_str())) {
// else if (isFloat(input)) try {
// convertFloat(*input); convertInt(*input);
// else if (isDouble(input)) } catch (std::exception &e) {
// convertDouble(*input); std::cerr << CLR_RED << e.what() << CLR_RESET << std::endl;
// else return;
// 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; else if (isFloat(input->c_str())) {
// std::cout << "IsFloat:\t" << (isFloat(input) ? "✅ | true" : "❌ | false") << std::endl; convertFloat(*input);
// std::cout << "IsDouble:\t" << (isDouble(input) ? "✅ | true" : "❌ | false") << std::endl; }
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;
}
} }

View file

@ -6,11 +6,12 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/22 22:06:48 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 "ScalarConverter.hpp"
#include "color.hpp"
bool isChar(std::string const &str) { bool isChar(std::string const &str) {
if (str.length() == 1 && !std::isdigit(str[0])) { if (str.length() == 1 && !std::isdigit(str[0])) {
@ -21,11 +22,8 @@ bool isChar(std::string const &str) {
void convertChar(std::string const &str) { void convertChar(std::string const &str) {
char toChar = str[0]; char toChar = str[0];
int toInt = static_cast<int>(toChar); std::cout << CLR_BLUE << "Char:\t'" << CLR_GOLD << toChar << CLR_BLUE << "'" << CLR_RESET << std::endl;
float toFloat = static_cast<float>(toChar); std::cout << CLR_BLUE << "Int:\t" << CLR_GOLD << static_cast<int>(toChar) << CLR_RESET << std::endl;
double toDouble = static_cast<double>(toChar); std::cout << CLR_BLUE << "Float:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<float>(toChar) << "f" << CLR_RESET << std::endl;
std::cout << "Char: '" << toChar << "'" << std::endl; std::cout << CLR_BLUE << "Double:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<double>(toChar) << CLR_RESET << std::endl;
std::cout << "Int: " << toInt << std::endl;
std::cout << "Float: " << toFloat << ".0f" << std::endl;
std::cout << "Double: " << toDouble << ".0" << std::endl;
} }

View file

@ -6,37 +6,33 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/22 22:11:44 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 "ScalarConverter.hpp"
#include <cstdlib>
#include <climits>
bool isInt(std::string const &str) { bool isInt(std::string const &str) {
long long tmp = std::atoll(str.c_str()); char *end;
if (tmp > INT_MAX || tmp < INT_MIN) std::strtol(str.c_str(), &end, 10);
if (*end)
return false; 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; return true;
} }
void convertInt(std::string const &str) { void convertInt(std::string const &str) {
int integer = std::atoi(str.c_str()); long long integer = std::atoll(str.c_str());
std::cout << "char: "; if (integer < INT_MIN || integer > INT_MAX)
if (std::isprint(integer)) {
std::cout << "'" << static_cast<char>(integer) << "'" << std::endl; std::cout << CLR_RED << "Error: Integer out of range" << CLR_RESET << std::endl;
else return;
std::cout << "Non displayable" << std::endl; }
std::cout << "int: " << integer << std::endl; std::cout << CLR_BLUE << "Char:\t" << CLR_RESET;
std::cout << "float: " << static_cast<float>(integer) << ".0f" << std::endl; if (integer > -128 && integer < 127 && std::isprint(integer))
std::cout << "double: " << static_cast<double>(integer) << ".0" << std::endl; std::cout << CLR_GOLD << "'" << static_cast<char>(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<int>(integer) << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "Float:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<float>(integer) << "f" << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "Double:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<double>(integer) << CLR_RESET << std::endl;
} }