diff --git a/d06/ex00/Makefile b/d06/ex00/Makefile index 7fd8a82..79357f7 100644 --- a/d06/ex00/Makefile +++ b/d06/ex00/Makefile @@ -37,7 +37,7 @@ SRCS = main.cpp \ checkDouble.cpp D_HEADERS = . -HEADERS = color.h +HEADERS = convert.h D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d06/ex00/color.h b/d06/ex00/color.h deleted file mode 100644 index f40596b..0000000 --- a/d06/ex00/color.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef COLOR_H -# define COLOR_H - -# define GRAY "\e[0;30m" -# define RED "\e[0;31m" -# define GREEN "\e[0;32m" -# define YELLOW "\e[0;33m" -# define BLUE "\e[0;34m" -# define PURPLE "\e[0;35m" -# define CYAN "\e[0;36m" -# define WHITE "\e[0;37m" - -# define B_GRAY "\e[1;30m" -# define B_RED "\e[1;31m" -# define B_GREEN "\e[1;32m" -# define B_YELLOW "\e[1;33m" -# define B_BLUE "\e[1;34m" -# define B_PURPLE "\e[1;35m" -# define B_CYAN "\e[1;36m" -# define B_WHITE "\e[1;37m" - -# define RESET "\e[0m" - -#endif - diff --git a/d06/ex00/convert b/d06/ex00/convert index e63a43b..08780ed 100755 Binary files a/d06/ex00/convert and b/d06/ex00/convert differ diff --git a/d06/ex00/convert.h b/d06/ex00/convert.h new file mode 100644 index 0000000..e81e42c --- /dev/null +++ b/d06/ex00/convert.h @@ -0,0 +1,69 @@ +#ifndef CONVERT_H +# define CONVERT_H + +#include +#include +#include // setw() +#include // isdigit() +#include // numeric_limits +#include // strtod() +#include // errno + +void convert(std::string str); + +bool checkChar(std::string str); +bool checkInt(std::string str); +bool checkFloat(std::string str); +bool checkDouble(std::string str); + +#define MAX_INT "2147483647" +#define MIN_INT "-2147483648" +#define MAX_INT_1 "2147483648" +#define MIN_INT_1 "-2147483649" +#define INT_MAX_LENGTH 10 +#define MAX_FLOAT_INT_PRECISION "16777216" +#define MAX_FLOAT_INT_PREC__1 "16777215" +#define MAX_FLOAT_INT_PREC__2 "16777214" +#define MAX_FLOAT_INT_PREC__3 "16777213" +#define MAX_FLOAT_INT_PREC__4 "16777212" +#define MAX_FLOAT_INT_PREC_1 "16777217" +#define MAX_FLOAT_INT_PREC_2 "16777218" +#define MAX_FLOAT_INT_PREC_3 "16777219" +#define MAX_FLOAT_INT_PREC_4 "16777220" +#define MAX_FLOAT_INT_PREC_5 "16777221" +#define MAX_FLOAT_INT_PREC_6 "16777222" +#define MAX_FLOAT "340282346638528859811704183484516925440" +#define MAX_F__1 "340282346638528859811704183484516925439" +#define MAX_F__2 "340282346638528859811704183484516925438" +#define MAX_F__3 "340282346638528859811704183484516925437" +#define MAX_F__4 "340282346638528859811704183484516925436" +#define MAX_F_1 "340282346638528859811704183484516925441" +#define MAX_F_2 "340282346638528859811704183484516925442" +#define MAX_F_3 "340282346638528859811704183484516925443" +#define MAX_F_4 "340282346638528859811704183484516925444" +#define MAX_F_5 "340282346638528859811704183484516925445" +#define MAX_F_6 "340282346638528859811704183484516925446" +#define FLOAT_MAX_LENGTH 56 + +// colors +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif diff --git a/d06/ex00/main.cpp b/d06/ex00/main.cpp index a6bbff4..e59b463 100644 --- a/d06/ex00/main.cpp +++ b/d06/ex00/main.cpp @@ -1,18 +1,12 @@ #include #include -void convert(std::string str); +#include "convert.h" // 2^24 = 16777216; // 2^31 = 2147483648 // first char printable "!" -> 21 (space -> 20) -#define MAX_INT "2147483647" -#define MIN_INT "-2147483648" -#define MAX_INT_1 "2147483648" -#define MIN_INT_1 "-2147483649" -#define MAX_FLOAT_INT_PRECISION "16777216" - int main(int ac, char **av) { if (ac > 1) @@ -115,6 +109,16 @@ int main(int ac, char **av) { convert(MAX_INT_1); convert(MIN_INT_1); convert(MAX_FLOAT_INT_PRECISION); + convert(MAX_FLOAT_INT_PREC__1); + convert(MAX_FLOAT_INT_PREC__2); + convert(MAX_FLOAT_INT_PREC__3); + convert(MAX_FLOAT_INT_PREC__4); + convert(MAX_FLOAT_INT_PREC_1); + convert(MAX_FLOAT_INT_PREC_2); + convert(MAX_FLOAT_INT_PREC_3); + convert(MAX_FLOAT_INT_PREC_4); + convert(MAX_FLOAT_INT_PREC_5); + convert(MAX_FLOAT_INT_PREC_6); // float convert("0.0f"); convert("-4.2f"); @@ -127,6 +131,27 @@ int main(int ac, char **av) { convert(MAX_INT_1".0f"); convert(MIN_INT_1".0f"); convert(MAX_FLOAT_INT_PRECISION".0f"); + convert(MAX_FLOAT_INT_PREC__1".0f"); + convert(MAX_FLOAT_INT_PREC__2".0f"); + convert(MAX_FLOAT_INT_PREC__3".0f"); + convert(MAX_FLOAT_INT_PREC__4".0f"); + convert(MAX_FLOAT_INT_PREC_1".0f"); + convert(MAX_FLOAT_INT_PREC_2".0f"); + convert(MAX_FLOAT_INT_PREC_3".0f"); + convert(MAX_FLOAT_INT_PREC_4".0f"); + convert(MAX_FLOAT_INT_PREC_5".0f"); + convert(MAX_FLOAT_INT_PREC_6".0f"); + convert(MAX_FLOAT".0f"); + convert(MAX_F__1".0f"); + convert(MAX_F__2".0f"); + convert(MAX_F__3".0f"); + convert(MAX_F__4".0f"); + convert(MAX_F_1".0f"); + convert(MAX_F_2".0f"); + convert(MAX_F_3".0f"); + convert(MAX_F_4".0f"); + convert(MAX_F_5".0f"); + convert(MAX_F_6".0f"); //double convert("0.0"); convert("-4.2"); @@ -139,6 +164,27 @@ int main(int ac, char **av) { convert(MAX_INT_1".0"); convert(MIN_INT_1".0"); convert(MAX_FLOAT_INT_PRECISION".0"); + convert(MAX_FLOAT_INT_PREC__1".0"); + convert(MAX_FLOAT_INT_PREC__2".0"); + convert(MAX_FLOAT_INT_PREC__3".0"); + convert(MAX_FLOAT_INT_PREC__4".0"); + convert(MAX_FLOAT_INT_PREC_1".0"); + convert(MAX_FLOAT_INT_PREC_2".0"); + convert(MAX_FLOAT_INT_PREC_3".0"); + convert(MAX_FLOAT_INT_PREC_4".0"); + convert(MAX_FLOAT_INT_PREC_5".0"); + convert(MAX_FLOAT_INT_PREC_6".0"); + convert(MAX_FLOAT".0"); + convert(MAX_F__1".0"); + convert(MAX_F__2".0"); + convert(MAX_F__3".0"); + convert(MAX_F__4".0"); + convert(MAX_F_1".0"); + convert(MAX_F_2".0"); + convert(MAX_F_3".0"); + convert(MAX_F_4".0"); + convert(MAX_F_5".0"); + convert(MAX_F_6".0"); return 0; } diff --git a/d06/ex00/srcs/checkChar.cpp b/d06/ex00/srcs/checkChar.cpp index 62c6ba5..67c18c3 100644 --- a/d06/ex00/srcs/checkChar.cpp +++ b/d06/ex00/srcs/checkChar.cpp @@ -1,11 +1,21 @@ -#include -#include -#include // isdigit() -#include "color.h" +#include "convert.h" -//Char toChar(str) { -// -//} +void fromChar(char c) { + int i; + float f; + double d; + + i = static_cast(c); + std::cout << std::setw(7) << std::left << "int" << B_CYAN << i << RESET "\n"; + f = static_cast(c); + std::cout << std::setw(7) << std::left << "float" << B_CYAN << f << RESET "\n"; + d = static_cast(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])) @@ -14,17 +24,17 @@ bool isChar(std::string str) { } bool checkChar(std::string str) { - -// char c; - + char c; std::cout << "char\n"; if (str.length() != 1 || isdigit(str[0])) return false; -// c = toChar(str); -// std::cout << "char"RESET" equal to : "B_CYAN << c << RESET << "\n"; -// fromChar(c); + c = toChar(str); + std::cout << B_YELLOW << str << RESET " is a char" + << " of value " + << B_CYAN << c << RESET << "\n"; + fromChar(c); return true; } diff --git a/d06/ex00/srcs/checkDouble.cpp b/d06/ex00/srcs/checkDouble.cpp index f22e14f..aea3ba7 100644 --- a/d06/ex00/srcs/checkDouble.cpp +++ b/d06/ex00/srcs/checkDouble.cpp @@ -1,16 +1,50 @@ -#include -#include -#include // isdigit() -#include "color.h" +#include "convert.h" + +void fromDouble(double d) { + char c; + int i; + float f; + + c = static_cast(d); + std::cout << std::setw(7) << std::left << "char" << B_CYAN << c << RESET "\n"; + i = static_cast(d); + std::cout << std::setw(7) << std::left << "int" << B_CYAN << i << RESET "\n"; + f = static_cast(d); + std::cout << std::setw(7) << std::left << "float" << B_CYAN << f << RESET "\n"; +} //Char toChar(str) { -// +// char c; + +// std::istringstream(str) >> c; +// return c; //} bool isDouble(std::string str) { - if (str.length() == 3) + size_t l; + std::string neg = ""; + + if (str[0] == '-') + neg = "-"; + if (str[0] == '+' || str[0] == '-') + str.erase(str.begin()); + if (!str.length()) + return false; + if (!str.compare("inf") || !str.compare("nan")) return true; - return false; + l = str.find_first_not_of("0123456789"); + if (l == std::string::npos || str[l] != '.') + return false; + str.erase(0,l + 1); + if (str.find_first_not_of("0123456789") != std::string::npos) + return false; + + str.insert(0, neg); + strtod(str.c_str(), NULL); + if (errno == ERANGE) + return false; + + return true; } bool checkDouble(std::string str) { diff --git a/d06/ex00/srcs/checkFloat.cpp b/d06/ex00/srcs/checkFloat.cpp index 24da83b..019fcf4 100644 --- a/d06/ex00/srcs/checkFloat.cpp +++ b/d06/ex00/srcs/checkFloat.cpp @@ -1,23 +1,43 @@ -#include -#include -#include // isdigit() -#include "color.h" +#include "convert.h" //Char toChar(str) { // //} bool isFloat(std::string str) { - if (str.length() == 4) + size_t l; +// std::string float_xtrem = MAX_FLOAT; + + 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; - return false; + l = str.find_first_not_of("0123456789"); + if (l == std::string::npos || str[l] != '.') + return false; + str.erase(0,l + 1); + l = str.find_first_not_of("0123456789"); + if (l == std::string::npos || str[l] != 'f') + 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 << "in char\n"; +std::cout << "float\n"; if (!isFloat(str)) return false; diff --git a/d06/ex00/srcs/checkInt.cpp b/d06/ex00/srcs/checkInt.cpp index 91b4762..828eecb 100644 --- a/d06/ex00/srcs/checkInt.cpp +++ b/d06/ex00/srcs/checkInt.cpp @@ -1,16 +1,26 @@ -#include -#include -#include // isdigit() -#include "color.h" +#include "convert.h" //Char toInt(str) { // //} bool isInt(std::string str) { - if (str.length() == 2) + 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; - return false; + if (str.compare(int_xtrem) > 0) + return false; + + return true; } bool checkInt(std::string str) { diff --git a/d06/ex00/srcs/convert.cpp b/d06/ex00/srcs/convert.cpp index 4102473..655b190 100644 --- a/d06/ex00/srcs/convert.cpp +++ b/d06/ex00/srcs/convert.cpp @@ -1,14 +1,9 @@ #include #include -#include "color.h" - -bool checkChar(std::string str); -bool checkInt(std::string str); -bool checkFloat(std::string str); -bool checkDouble(std::string str); +#include "convert.h" bool isElse(std::string str) { - std::cout << str << B_RED " is not valid type for this exercise" RESET "\n"; + std::cout << B_RED << str << CYAN " is not valid type for this exercise" RESET "\n"; return true; } @@ -23,9 +18,11 @@ bool (*checkFunc[])(std::string str) = void convert(std::string str) { + std::cout << 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"; }