Files
CPP_Module_06/ex00/PrintNb.hpp

60 lines
2.1 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PrintNb.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/08 14:30:07 by adjoly #+# #+# */
/* Updated: 2025/04/09 10:32:48 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <cctype>
#include <iostream>
#include <limits>
#include <sstream>
template <typename T> void _printFloat(T nb) {
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
}
template <typename T> void _printInt(T nb) {
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
}
template <typename T> void _printChar(T nb) {
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
}
template <typename T> void _printDouble(T nb) {
std::cout << "double: " << static_cast<double>(nb) << std::endl;
}
template <typename T> T _ConvertTo(const std::string &str) {
std::stringstream ss(str);
T ret;
ss >> ret;
return ret;
}
template <typename T> void _Convert(const std::string &str) {
T nb = _ConvertTo<T>(str);
_printChar(nb);
_printInt(nb);
_printFloat(nb);
_printDouble(nb);
}