feat: finishing the ex00 of the module 06
This commit is contained in:
parent
d32b496a87
commit
6631b3042a
4 changed files with 59 additions and 53 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -6,17 +6,11 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <cctype>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<int>(toChar);
|
||||
float toFloat = static_cast<float>(toChar);
|
||||
double toDouble = static_cast<double>(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<int>(toChar) << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "Float:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<float>(toChar) << "f" << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "Double:\t" << CLR_GOLD << std::fixed << std::setprecision(1) << static_cast<double>(toChar) << CLR_RESET << std::endl;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,37 +6,33 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <cstdlib>
|
||||
#include <climits>
|
||||
|
||||
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<char>(integer) << "'" << std::endl;
|
||||
else
|
||||
std::cout << "Non displayable" << std::endl;
|
||||
std::cout << "int: " << integer << std::endl;
|
||||
std::cout << "float: " << static_cast<float>(integer) << ".0f" << std::endl;
|
||||
std::cout << "double: " << static_cast<double>(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<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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue