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

@@ -13,10 +13,6 @@ void fromChar(char c) {
std::cout << std::setw(7) << std::left << "double" << B_CYAN << d << RESET "\n";
}
char toChar(std::string str) {
return str[0];
}
bool isChar(std::string str) {
if (str.length() != 1 || isdigit(str[0]))
return false;
@@ -25,15 +21,12 @@ bool isChar(std::string str) {
bool checkChar(std::string str) {
char c;
std::cout << "char\n";
if (str.length() != 1 || isdigit(str[0]))
return false;
c = str[0];
c = toChar(str);
std::cout << B_YELLOW << str << RESET " is a char"
<< " of value "
<< B_CYAN << c << RESET << "\n";
std::cout << B_CYAN << c << B_YELLOW " char" RESET "\n";
fromChar(c);
return true;

View File

@@ -1,63 +1,65 @@
#include "convert.h"
void fromDouble(double d) {
char c;
int i;
float f;
void fromDouble(double 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";
c = static_cast<char>(d);
std::cout << std::setw(7) << std::left << "char" << B_CYAN << c << RESET "\n";
i = static_cast<int>(d);
std::cout << std::setw(7) << std::left << "int" << B_CYAN << i << RESET "\n";
f = static_cast<float>(d);
std::cout << std::setw(7) << std::left << "float" << B_CYAN << f << 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";
// float
std::cout << std::setw(7) << std::left << "float";
if (value < std::numeric_limits<float>::min()
|| value > std::numeric_limits<float>::max() )
std::cout << B_RED << "impossible" << RESET "\n";
else
std::cout << B_CYAN << static_cast<double>(value) << RESET "\n";
}
//Char toChar(str) {
// char c;
// std::istringstream(str) >> c;
// return c;
//}
bool isDouble(std::string str) {
size_t l;
std::string neg = "";
if (str[0] == '-')
neg = "-";
size_t l;
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
if (!str.length())
if (str.length() == 0)
return false;
if (!str.compare("inf") || !str.compare("nan"))
return true;
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != '.')
if (l == std::string::npos || str[l] != '.' || l > DOUBLE_MAX_LENGTH)
return false;
str.erase(0,l + 1);
if (str.find_first_not_of("0123456789") != std::string::npos)
if (str.find_first_not_of("0123456789", l + 1) != std::string::npos)
return false;
if (l < DOUBLE_MAX_LENGTH)
return true;
if (str.compare(0, l, MAX_DOUBLE) > 0)
return false;
str.insert(0, neg);
strtod(str.c_str(), NULL);
if (errno == ERANGE)
return false;
return true;
}
bool checkDouble(std::string str) {
// double d;
std::cout << "double\n";
double d;
if (!isDouble(str))
return false;
// d = toDouble(str);
// std::cout << "double"RESET" equal to : "B_CYAN << d << RESET << "\n";
// fromDouble(d);
std::istringstream(str) >> d;
std::cout << B_CYAN << d << B_YELLOW " double" RESET "\n";
fromDouble(d);
return true;
}

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

View File

@@ -1,8 +1,23 @@
#include "convert.h"
//Char toInt(str) {
//
//}
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;
@@ -24,17 +39,14 @@ bool isInt(std::string str) {
}
bool checkInt(std::string str) {
// int i;
std::cout << "int\n";
int i;
if (!isInt(str))
return false;
// i = toInt(str);
// std::cout << "int"RESET" equal to : "B_CYAN << i << RESET << "\n";
// fromInt(i);
std::istringstream(str) >> i;
std::cout << B_CYAN << i << B_YELLOW " int" RESET "\n";
fromInt(i);
return true;
}

View File

@@ -2,27 +2,21 @@
#include <string>
#include "convert.h"
bool isElse(std::string str) {
std::cout << B_RED << str << CYAN " is not valid type for this exercise" RESET "\n";
return true;
}
bool (*checkFunc[])(std::string str) =
{
checkChar,
checkInt,
checkFloat,
checkDouble,
isElse
};
void convert(std::string str) {
std::cout << B_BLUE << str << RESET "\n";
std::cout << "\n" B_BLUE << str << RESET "\n";
int size = sizeof(checkFunc) / sizeof(checkFunc[0]);
for (int it = 0; it < size; it++)
if ((*checkFunc[it])(str))
break;
std::cout << "\n";
return ;
std::cout << B_RED "is not valid type for this exercise" RESET "\n";
}