🎉」 init: hello world !

This commit is contained in:
2025-08-15 15:11:43 +02:00
commit c9ae5bee78
13 changed files with 559 additions and 0 deletions

45
ex00/Makefile Normal file
View File

@@ -0,0 +1,45 @@
SHELL = bash
NAME = ScalarConverter
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

128
ex00/PrintValue.hpp Normal file
View File

@@ -0,0 +1,128 @@
#pragma once
#include "ScalarConverter.hpp"
void _printChar(const std::string &s)
{
std::cout << "char: ";
if (!std::isprint(s[0]))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(s[0]) << "'" << std::endl;
std::cout << "int: " << static_cast<int>(s[0]) << std::endl;
std::cout << "float: " << std::fixed << static_cast<float>(s[0]) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(s[0]) << std::endl;
}
void _printInt(const std::string &s)
{
long int nb = std::strtol(s.c_str(), NULL, 10);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: " << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printFloat(const std::string &s)
{
long int nb = std::strtod(s.c_str(), NULL);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: ";
if (nb < std::numeric_limits<float>::min() ||
nb > std::numeric_limits<float>::max())
std::cout << "impossible" << std::endl;
else
std::cout << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printDouble(const std::string &s)
{
long int nb = std::strtod(s.c_str(), NULL);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: ";
if (nb < std::numeric_limits<float>::min() ||
nb > std::numeric_limits<float>::max())
std::cout << "impossible" << std::endl;
else
std::cout << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printNan(const std::string &s)
{
std::string input = s;
if (input == "inf" || input == "inff")
input = "+inf";
if (input == "nan" || input == "-inf" || input == "+inf")
{
std::cout << "char: impossible\n";
std::cout << "int: impossible\n";
std::cout << "float: " << input << "f" << std::endl;
std::cout << "double: " << input << std::endl;
return;
}
if (input == "nanf" || input == "-inff" || input == "+inff")
{
std::cout << "char: impossible\n";
std::cout << "int: impossible\n";
std::cout << "float: " << input << std::endl;
std::cout << "double: ";
if (input == "nanf")
std::cout << "nan";
else if (input == "-inff")
std::cout << "-inf";
else
std::cout << "+inf";
std::cout << std::endl;
}
}

71
ex00/ScalarConverter.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "ScalarConverter.hpp"
#include "PrintValue.hpp"
ScalarConverter::ScalarConverter(void) {
}
ScalarConverter::ScalarConverter(const ScalarConverter &cpy) {
if (this != &cpy) {
*this = cpy;
}
}
ScalarConverter::~ScalarConverter(void) {
}
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) {
return *this;
}
bool _isNan(const std::string &str) {
if (str == "nan" || str == "nanf" || str == "-inf" || str == "-inff" ||
str == "+inf" || str == "+inff" || str == "inf" || str == "inff")
return true;
return false;
}
e_type _getType(const std::string &s)
{
if (_isNan(s))
return NAN;
if (s.length() == 1 && !isdigit(s[0]))
return CHAR;
char *err;
(void)strtol(s.c_str(), &err, 10);
if (*err == 0)
return INT;
(void)strtod(s.c_str(), &err);
if (*err == 0)
return DOUBLE;
if (*err == 'f' && *(err + 1) == 0)
return FLOAT;
return NONE;
}
void ScalarConverter::convert(const std::string &s)
{
switch (_getType(s))
{
case CHAR:
_printChar(s);
break;
case INT:
_printInt(s);
break;
case FLOAT:
_printFloat(s);
break;
case DOUBLE:
_printDouble(s);
break;
case NAN:
_printNan(s);
break;
default:
std::cerr << "'" << s << "' does not convert to any of the availables types" << std::endl;
}
}

29
ex00/ScalarConverter.hpp Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <cctype>
#include <iostream>
#include <limits>
#include <sstream>
#include <cstdlib>
enum e_type {
CHAR,
INT,
FLOAT,
DOUBLE,
NAN,
NONE
};
class ScalarConverter {
public:
static void convert(const std::string &);
private:
ScalarConverter(void);
ScalarConverter(const ScalarConverter &);
~ScalarConverter(void);
ScalarConverter &operator=(const ScalarConverter &);
};

10
ex00/main.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "ScalarConverter.hpp"
#include <iostream>
int main(int ac, char **av) {
if (ac < 2) {
std::cout << "needs an input ! D:" << std::endl;
return 1;
}
ScalarConverter::convert(av[1]);
}

57
ex01/Makefile Normal file
View File

@@ -0,0 +1,57 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/08/15 14:52:05 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = Serializer
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

30
ex01/Serializer.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "Serializer.hpp"
#include <string>
uintptr_t Serializer::serialize(Data *ptr)
{
return reinterpret_cast<uintptr_t>(ptr);
}
Data *Serializer::deserialize(uintptr_t raw)
{
return reinterpret_cast<Data *>(raw);
}
Serializer::Serializer(void)
{
}
Serializer::Serializer(const Serializer &cpy)
{
if (this != &cpy)
*this = cpy;
}
Serializer::~Serializer(void) {
}
Serializer &Serializer::operator=(const Serializer &)
{
return *this;
}

20
ex01/Serializer.hpp Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <stdint.h>
struct Data {
uintptr_t value;
};
class Serializer {
public:
static uintptr_t serialize(Data *ptr);
static Data *deserialize(uintptr_t raw);
private:
Serializer(void);
~Serializer(void);
Serializer(const Serializer &);
Serializer &operator=(const Serializer &);
};

16
ex01/main.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "Serializer.hpp"
#include <iostream>
int main()
{
Data data;
data.value = 727;
std::cout << "data : (" << &data<< ") value = " << data.value << std::endl;
uintptr_t serialized = Serializer::serialize(&data);
std::cout << "serialize : (" << std::hex << serialized << std::dec << ")\n";
Data *deserialized = Serializer::deserialize(serialized);
std::cout << "deserialize : (" << deserialized << ") value = " << deserialized->value << std::endl;
}

66
ex02/Base.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include "Base.hpp"
#include <cstdlib>
#include <iostream>
Base *generate(void)
{
std::srand(time(0));
switch (std::rand() % 3) {
case 0:
std::cout << "A class generated :D" << std::endl;
return new A;
case 1:
std::cout << "B class generated :D" << std::endl;
return new B;
case 2:
std::cout << "C class generated :D" << std::endl;
return new C;
};
return new A;
}
void identify(Base *p)
{
if (dynamic_cast<A *>(p))
{
std::cout << "omg it's an A object !" << std::endl;
return;
}
if (dynamic_cast<B *>(p))
{
std::cout << "omg it's a B object !" << std::endl;
return;
}
if (dynamic_cast<C *>(p))
{
std::cout << "omg it's a C object !" << std::endl;
return;
}
std::cout << "what is this D:" << std::endl;
}
void identify(Base &p)
{
try {
(void)dynamic_cast<A &>(p);
std::cout << "omg it's an A object !" << std::endl;
return;
}
catch (...) {}
try {
(void)dynamic_cast<B &>(p);
std::cout << "omg it's an B object !" << std::endl;
return;
}
catch (...) {}
try {
(void)dynamic_cast<C &>(p);
std::cout << "omg it's an C object !" << std::endl;
return;
}
catch (...) {}
std::cout << "what is this D:" << std::endl;
}
Base::~Base(void) {}

18
ex02/Base.hpp Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
class Base {
public:
virtual ~Base(void);
private:
};
class A : public Base {};
class B : public Base {};
class C : public Base {};
Base *generate();
void identify(Base *p);
void identify(Base &p);

57
ex02/Makefile Normal file
View File

@@ -0,0 +1,57 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/08/15 14:52:23 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = Base
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

12
ex02/main.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include "Base.hpp"
#include <iostream>
int main()
{
Base *base = generate();
std::cout << "pointer idendifer : ";
identify(base);
std::cout << "ref idendifer : ";
identify(*base);
delete base;
}