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

Binary file not shown.

View File

@@ -2,9 +2,10 @@
# define CONVERT_H # define CONVERT_H
#include <iostream> #include <iostream>
#include <sstream>
#include <string> #include <string>
#include <iomanip> // setw() #include <iomanip> // setw()
#include <cctype> // isdigit() #include <cctype> // isdigit() isprint()
#include <limits> // numeric_limits<type> #include <limits> // numeric_limits<type>
#include <cstdlib> // strtod() #include <cstdlib> // strtod()
#include <cerrno> // errno #include <cerrno> // errno
@@ -22,6 +23,12 @@ bool checkDouble(std::string str);
#define MIN_INT_1 "-2147483649" #define MIN_INT_1 "-2147483649"
#define INT_MAX_LENGTH 10 #define INT_MAX_LENGTH 10
#define MAX_FLOAT_INT_PRECISION "16777216" #define MAX_FLOAT_INT_PRECISION "16777216"
#define MAX_FLOAT "340282346638528859811704183484516925440"
#define FLOAT_MAX_LENGTH 39
#define MAX_DOUBLE "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368"
#define DOUBLE_MAX_LENGTH 309
// for tests
#define MAX_FLOAT_INT_PREC__1 "16777215" #define MAX_FLOAT_INT_PREC__1 "16777215"
#define MAX_FLOAT_INT_PREC__2 "16777214" #define MAX_FLOAT_INT_PREC__2 "16777214"
#define MAX_FLOAT_INT_PREC__3 "16777213" #define MAX_FLOAT_INT_PREC__3 "16777213"
@@ -32,7 +39,6 @@ bool checkDouble(std::string str);
#define MAX_FLOAT_INT_PREC_4 "16777220" #define MAX_FLOAT_INT_PREC_4 "16777220"
#define MAX_FLOAT_INT_PREC_5 "16777221" #define MAX_FLOAT_INT_PREC_5 "16777221"
#define MAX_FLOAT_INT_PREC_6 "16777222" #define MAX_FLOAT_INT_PREC_6 "16777222"
#define MAX_FLOAT "340282346638528859811704183484516925440"
#define MAX_F__1 "340282346638528859811704183484516925439" #define MAX_F__1 "340282346638528859811704183484516925439"
#define MAX_F__2 "340282346638528859811704183484516925438" #define MAX_F__2 "340282346638528859811704183484516925438"
#define MAX_F__3 "340282346638528859811704183484516925437" #define MAX_F__3 "340282346638528859811704183484516925437"
@@ -43,7 +49,17 @@ bool checkDouble(std::string str);
#define MAX_F_4 "340282346638528859811704183484516925444" #define MAX_F_4 "340282346638528859811704183484516925444"
#define MAX_F_5 "340282346638528859811704183484516925445" #define MAX_F_5 "340282346638528859811704183484516925445"
#define MAX_F_6 "340282346638528859811704183484516925446" #define MAX_F_6 "340282346638528859811704183484516925446"
#define FLOAT_MAX_LENGTH 56 #define MIN_DOUBLE "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368"
#define MAX_D__1 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858367"
#define MAX_D__2 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858366"
#define MAX_D__3 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858365"
#define MAX_D__4 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858364"
#define MAX_D_1 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858369"
#define MAX_D_2 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858370"
#define MAX_D_3 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858371"
#define MAX_D_4 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858372"
#define MAX_D_5 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858373"
#define MAX_D_6 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858374"
// colors // colors
# define GRAY "\e[0;30m" # define GRAY "\e[0;30m"

View File

@@ -104,6 +104,34 @@ int main(int ac, char **av) {
convert("0"); convert("0");
convert("-42"); convert("-42");
convert("42"); convert("42");
convert("-1");
convert("1");
convert("2");
convert("3");
convert("4");
convert("5");
convert("29");
convert("30");
convert("31");
convert("32");
convert("33");
convert("34");
convert("35");
convert("122");
convert("123");
convert("124");
convert("125");
convert("126");
convert("127");
convert("128");
convert("129");
convert("130");
convert("253");
convert("254");
convert("255");
convert("256");
convert("257");
convert("258");
convert(MAX_INT); convert(MAX_INT);
convert(MIN_INT); convert(MIN_INT);
convert(MAX_INT_1); convert(MAX_INT_1);
@@ -185,6 +213,18 @@ int main(int ac, char **av) {
convert(MAX_F_4".0"); convert(MAX_F_4".0");
convert(MAX_F_5".0"); convert(MAX_F_5".0");
convert(MAX_F_6".0"); convert(MAX_F_6".0");
convert(MAX_DOUBLE".0");
convert(MIN_DOUBLE".0");
convert(MAX_D__1".0");
convert(MAX_D__2".0");
convert(MAX_D__3".0");
convert(MAX_D__4".0");
convert(MAX_D_1".0");
convert(MAX_D_2".0");
convert(MAX_D_3".0");
convert(MAX_D_4".0");
convert(MAX_D_5".0");
convert(MAX_D_6".0");
return 0; return 0;
} }

View File

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

View File

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

View File

@@ -1,50 +1,61 @@
#include "convert.h" #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) { bool isFloat(std::string str) {
size_t l; size_t l;
// std::string float_xtrem = MAX_FLOAT; size_t l2;
if (str[0] == '+' || str[0] == '-') if (str[0] == '+' || str[0] == '-')
str.erase(str.begin()); str.erase(str.begin());
// if (str.length() == 0 || str.length() > FLOAT_MAX_LENGTH)
if (str.length() == 0) if (str.length() == 0)
return false; return false;
if (!str.compare("inff") || !str.compare("nanf")) if (!str.compare("inff") || !str.compare("nanf"))
return true; return true;
l = str.find_first_not_of("0123456789"); 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; return false;
str.erase(0,l + 1); l2 = str.find_first_not_of("0123456789", l + 1);
l = str.find_first_not_of("0123456789"); if (l2 == std::string::npos || l2 != str.length() - 1 || str[l2] != 'f')
if (l == std::string::npos || str[l] != 'f') return false;
if (l < FLOAT_MAX_LENGTH)
return true;
if (str.compare(0, l, MAX_FLOAT) > 0)
return false; 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; return true;
} }
bool checkFloat(std::string str) { bool checkFloat(std::string str) {
float f;
// float f;
std::cout << "float\n";
if (!isFloat(str)) if (!isFloat(str))
return false; return false;
// f = toFloat(str); std::istringstream(str) >> f;
// std::cout << "float"RESET" equal to : "B_CYAN << f << RESET << "\n"; std::cout << B_CYAN << f << B_YELLOW " float" RESET "\n";
// fromFloat(f); fromFloat(f);
return true; return true;
} }

View File

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

View File

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