d06 ex01 mise en place tableau de pointeurs sur fonctions
This commit is contained in:
75
d06/ex00/Makefile
Normal file
75
d06/ex00/Makefile
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . name = value \ . += append to a variable #
|
||||||
|
# VARIABLES . value . != set result of command #
|
||||||
|
# . name is case sensitive . ?= set if not already set #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
NAME = convert
|
||||||
|
|
||||||
|
#TYPE = c
|
||||||
|
TYPE = cpp
|
||||||
|
|
||||||
|
ifeq "$(TYPE)" "c"
|
||||||
|
CC = c
|
||||||
|
EXT = c
|
||||||
|
else ifeq "$(TYPE)" "cpp"
|
||||||
|
CC = c++
|
||||||
|
EXT = cpp
|
||||||
|
endif
|
||||||
|
|
||||||
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
|
||||||
|
ifeq "$(TYPE)" "cpp"
|
||||||
|
CFLAGS += -std=c++98
|
||||||
|
endif
|
||||||
|
|
||||||
|
VPATH = $(D_SRCS)
|
||||||
|
|
||||||
|
LIBS =
|
||||||
|
|
||||||
|
INCLUDES = -I$(D_HEADERS)
|
||||||
|
|
||||||
|
D_SRCS = .
|
||||||
|
SRCS = main.cpp
|
||||||
|
|
||||||
|
D_HEADERS = .
|
||||||
|
HEADERS =
|
||||||
|
|
||||||
|
D_OBJS = builds
|
||||||
|
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
||||||
|
|
||||||
|
ifeq "$(D_OBJS)" "."
|
||||||
|
RM_OBJS = rm -f $(OBJS)
|
||||||
|
else
|
||||||
|
RM_OBJS = rm -rf $(D_OBJS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
# . target: prerequisites . $@ : target #
|
||||||
|
# RULES . recipe . $< : 1st prerequisite #
|
||||||
|
# . recipe . $^ : all prerequisites #
|
||||||
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(D_OBJS):
|
||||||
|
mkdir $@
|
||||||
|
|
||||||
|
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
||||||
|
|
||||||
|
$(NAME): $(OBJS)
|
||||||
|
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM_OBJS)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY : all clean fclean re
|
||||||
|
|
||||||
25
d06/ex00/color.h
Normal file
25
d06/ex00/color.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#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
|
||||||
|
|
||||||
BIN
d06/ex00/convert
Executable file
BIN
d06/ex00/convert
Executable file
Binary file not shown.
219
d06/ex00/main.cpp
Normal file
219
d06/ex00/main.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user