Files
42_INT_09_piscine_cpp/d06/ex00/srcs/checkInt.cpp
2022-03-08 16:51:12 +01:00

53 lines
1.2 KiB
C++

#include "convert.h"
void fromInt(int value) {
// char
std::cout << std::setw(7) << std::left << "char";
if (value < 0 || value > std::numeric_limits<char>::max())
std::cout << B_RED << "impossible" << RESET "\n";
else if (!isprint(value))
std::cout << B_RED << "non displayable" << RESET "\n";
else
std::cout << B_CYAN << static_cast<char>(value) << RESET "\n";
// float
std::cout << std::setw(7) << std::left << "float" << B_CYAN
<< static_cast<float>(value) << RESET "\n";
// double
std::cout << std::setw(7) << std::left << "double" << B_CYAN
<< static_cast<double>(value) << RESET "\n";
}
bool isInt(std::string str) {
std::string int_xtrem = MAX_INT;
if (str[0] == '-')
int_xtrem = MAX_INT_1;
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
if (str.length() == 0 || str.length() > INT_MAX_LENGTH)
return false;
if (str.find_first_not_of("0123456789") != std::string::npos)
return false;
if (str.length() < INT_MAX_LENGTH)
return true;
if (str.compare(int_xtrem) > 0)
return false;
return true;
}
bool checkInt(std::string str) {
int i;
if (!isInt(str))
return false;
std::istringstream(str) >> i;
std::cout << B_CYAN << i << B_YELLOW " int" RESET "\n";
fromInt(i);
return true;
}