d06 ex00 fini

This commit is contained in:
hugogogo
2022-03-08 16:51:12 +01:00
parent 4ee3ae3e7d
commit 315f62eb96
8 changed files with 158 additions and 90 deletions

View File

@@ -1,50 +1,61 @@
#include "convert.h"
//Char toChar(str) {
//
//}
void fromFloat(float 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";
// int
std::cout << std::setw(7) << std::left << "int";
if (value < std::numeric_limits<int>::min()
|| value > std::numeric_limits<int>::max() )
std::cout << B_RED << "impossible" << RESET "\n";
else
std::cout << B_CYAN << static_cast<int>(value) << RESET "\n";
// double
std::cout << std::setw(7) << std::left << "double";
std::cout << B_CYAN << static_cast<double>(value) << RESET "\n";
}
bool isFloat(std::string str) {
size_t l;
// std::string float_xtrem = MAX_FLOAT;
size_t l2;
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
// if (str.length() == 0 || str.length() > FLOAT_MAX_LENGTH)
if (str.length() == 0)
return false;
if (!str.compare("inff") || !str.compare("nanf"))
return true;
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != '.')
if (l == std::string::npos || str[l] != '.' || l > FLOAT_MAX_LENGTH)
return false;
str.erase(0,l + 1);
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != 'f')
l2 = str.find_first_not_of("0123456789", l + 1);
if (l2 == std::string::npos || l2 != str.length() - 1 || str[l2] != 'f')
return false;
if (l < FLOAT_MAX_LENGTH)
return true;
if (str.compare(0, l, MAX_FLOAT) > 0)
return false;
// to work, one should check the decimal and the integer part
// str.erase(str.end() - 1);
// if (str.length() < FLOAT_MAX_LENGTH)
// return true;
// if (str.compare(float_xtrem) > 0)
// return false;
return true;
}
bool checkFloat(std::string str) {
// float f;
std::cout << "float\n";
float f;
if (!isFloat(str))
return false;
// f = toFloat(str);
// std::cout << "float"RESET" equal to : "B_CYAN << f << RESET << "\n";
// fromFloat(f);
std::istringstream(str) >> f;
std::cout << B_CYAN << f << B_YELLOW " float" RESET "\n";
fromFloat(f);
return true;
}