d06 ex01 mise en place tableau de pointeurs sur fonctions

This commit is contained in:
hugogogo
2022-03-07 14:18:07 +01:00
parent 7abaa2a294
commit b8bf7e45dd
4 changed files with 319 additions and 0 deletions

219
d06/ex00/main.cpp Normal file
View File

@@ -0,0 +1,219 @@
#include <iostream>
#include <string>
#include "color.h"
#include <cctype> // isdigit()
// 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"
//Char toChar(str) {
//
//}
//bool isChar() {}
bool checkChar(std::string str) {
// char c;
//
std::cout << "in 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);
return true;
}
bool checkInt(std::string str) {
std::cout << "in int\n";
if (str.length() == 1 || !isdigit(str[0]))
return false;
return true;
}
bool checkFloat(std::string str) {
std::cout << "in int\n";
if (str.length() == 1 || !isdigit(str[0]))
return false;
return true;
}
bool (*checkFunc[])(std::string str) =
{
checkChar,
checkInt,
checkFloat
};
void convert(std::string str) {
int size = sizeof(checkFunc) / sizeof(checkFunc[0]);
// std::cout << str << " is a "B_YELLOW ;
for (int it = 0; it < size; it++)
if ((*checkFunc[it])(str))
break;
// else if (isInt(str))
// {
// i = toInt(str);
// std::cout << "int"RESET" equal to : "B_CYAN << i << RESET << "\n";
// fromInt(i);
// }
// else if (isFloat(str))
// {
// f = toFloat(str);
// std::cout << "float"RESET" equal to : "B_CYAN << f << RESET << "\n";
// fromFloat(f);
// }
// else if (isDouble(str))
// {
// d = toDouble(str);
// std::cout << "double"RESET" equal to : "B_CYAN << d << RESET << "\n";
// fromDouble(d);
// }
// else
// std::cout << B_RED"not valid type"RESET"\n";
}
int main(int ac, char **av) {
if (ac > 1)
{
convert(av[1]);
return 0;
}
// char
convert("!");
convert("\"");
convert("#");
convert("$");
convert("%");
convert("&");
convert("'");
convert("(");
convert(")");
convert("*");
convert("+");
convert(",");
convert("-");
convert(".");
convert("/");
convert(":");
convert(";");
convert("<");
convert("=");
convert(">");
convert("?");
convert("@");
convert("A");
convert("B");
convert("C");
convert("D");
convert("E");
convert("F");
convert("G");
convert("H");
convert("I");
convert("J");
convert("K");
convert("L");
convert("M");
convert("N");
convert("O");
convert("P");
convert("Q");
convert("R");
convert("S");
convert("T");
convert("U");
convert("V");
convert("W");
convert("X");
convert("Y");
convert("Z");
convert("[");
convert("\\");
convert("]");
convert("^");
convert("_");
convert("`");
convert("a");
convert("b");
convert("c");
convert("d");
convert("e");
convert("f");
convert("g");
convert("h");
convert("i");
convert("j");
convert("k");
convert("l");
convert("m");
convert("n");
convert("o");
convert("p");
convert("q");
convert("r");
convert("s");
convert("t");
convert("u");
convert("v");
convert("w");
convert("x");
convert("y");
convert("z");
convert("{");
convert("|");
convert("}");
convert("~");
// int
convert("0");
convert("-42");
convert("42");
convert(MAX_INT);
convert(MIN_INT);
convert(MAX_INT_1);
convert(MIN_INT_1);
convert(MAX_FLOAT_INT_PRECISION);
// float
convert("0.0f");
convert("-4.2f");
convert("4.2f");
convert("-inff");
convert("+inff");
convert("nanf");
convert(MAX_INT".0f");
convert(MIN_INT".0f");
convert(MAX_INT_1".0f");
convert(MIN_INT_1".0f");
convert(MAX_FLOAT_INT_PRECISION".0f");
//double
convert("0.0");
convert("-4.2");
convert("4.2");
convert("-inf");
convert("+inf");
convert("nan");
convert(MAX_INT".0");
convert(MIN_INT".0");
convert(MAX_INT_1".0");
convert(MIN_INT_1".0");
convert(MAX_FLOAT_INT_PRECISION".0");
return 0;
}