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

@ -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;
}