فهرست منبع

added casts exercices

bastien 4 هفته پیش
والد
کامیت
7e33606fc9

+ 50 - 0
Module_06/ex00/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/05 12:50:21 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main ScalarConverter
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 41 - 0
Module_06/ex00/includes/ScalarConverter.hpp

@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ScalarConverter.hpp                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/03 13:23:10 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/03 15:30:48 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef SCALARCONVERTER_HPP
+# define SCALARCONVERTER_HPP
+
+# include <iostream>
+# include <string>
+
+class ScalarConverter
+{
+
+	public:
+
+		ScalarConverter();
+		ScalarConverter( ScalarConverter const & src );
+		virtual ~ScalarConverter() = 0;
+
+		ScalarConverter &		operator=( ScalarConverter const & rhs );
+		static	void			convert(std::string str);
+
+	private:
+
+	static void 					_printChar(double converted, bool isPossible);
+	static void 					_printInt(double converted, bool isPossible);
+	static void 					_printFloat(double converted, bool isPossible);
+	static void 					_printDouble(double converted, bool isPossible);
+};
+
+std::ostream &			operator<<( std::ostream & o, ScalarConverter const & i );
+
+#endif /* ************************************************* SCALARCONVERTER_H */

+ 110 - 0
Module_06/ex00/srcs/ScalarConverter.cpp

@@ -0,0 +1,110 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ScalarConverter.cpp                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/03 13:23:10 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 13:45:35 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ScalarConverter.hpp"
+#include <iostream>
+#include <iomanip>
+#include <limits>
+#include <string>
+#include <ctype.h>
+#include <cerrno>
+#include <cstdlib>
+#include <cmath>
+
+/*
+** ------------------------------- CONSTRUCTOR --------------------------------
+*/
+
+
+/*
+** --------------------------------- OVERLOAD ---------------------------------
+*/
+
+ScalarConverter &				ScalarConverter::operator=( ScalarConverter const & rhs )
+{
+	(void) rhs;
+	//if ( this != &rhs )
+	//{
+		//this->_value = rhs.getValue();
+	//}
+	return *this;
+}
+
+std::ostream &			operator<<( std::ostream & o, ScalarConverter const & i )
+{
+	(void)i;
+	//o << "Value = " << i.getValue();
+	return o;
+}
+
+
+/*
+** --------------------------------- METHODS ----------------------------------
+*/
+void			ScalarConverter::convert(std::string str) {
+	char	*endptr;
+	bool	isPossible = false;
+	double converted = strtod(str.c_str(), &endptr);
+
+	if (errno != ERANGE && (*endptr == '\0' || (*endptr == 'f' && *(endptr + 1) == '\0'))) {
+		isPossible = true;
+	} else {
+		isPossible = false;
+	}
+	if (!isPossible && str.length() == 1) {
+		converted = static_cast<char>(str[0]);
+		isPossible = true;
+	}
+	ScalarConverter::_printChar(converted, isPossible);
+	ScalarConverter::_printInt(converted, isPossible);
+	ScalarConverter::_printFloat(converted, isPossible);
+	ScalarConverter::_printDouble(converted, isPossible);
+}
+
+void	ScalarConverter::_printChar(double const converted, bool isPossible) {
+	if (!isPossible || std::isnan(converted) || converted > std::numeric_limits<char>::max() || converted < std::numeric_limits<char>::min())
+		std::cout << "char : Impossible" << std::endl;
+	else if (!isprint(static_cast<char>(converted)))
+		std::cout << "char : Non displayable" << std::endl;	
+	else
+		std::cout << "char : '" << static_cast<char>(converted) << "'" << std::endl;
+}
+
+void	ScalarConverter::_printInt(double const converted, bool isPossible) {
+	if (!isPossible || std::isnan(converted) || converted > std::numeric_limits<int>::max() || converted < std::numeric_limits<int>::min())
+		std::cout << "int : Impossible" << std::endl;
+	else
+		std::cout << "int : " << static_cast<int>(converted) << std::endl;
+}
+
+void	ScalarConverter::_printFloat(double const converted, bool isPossible) {
+	if (!isPossible || converted > std::numeric_limits<float>::max() || converted < -std::numeric_limits<float>::max())
+		std::cout << "float : Impossible" << std::endl;
+	else if (std::isnan(converted)) 
+		std::cout << "float : nanf" << std::endl;
+	else
+		std::cout << "float : " << std::fixed << std::setprecision(2) << static_cast<float>(converted) << "f" << std::endl;
+}
+
+void	ScalarConverter::_printDouble(double const converted, bool isPossible) {
+	if (!isPossible)
+		std::cout << "double : Impossible" << std::endl;
+	else
+		std::cout << "double : " << std::fixed << std::setprecision(2) << static_cast<double>(converted) << std::endl;
+}
+
+/*
+** --------------------------------- ACCESSOR ---------------------------------
+*/
+
+
+/* ************************************************************************** */

+ 19 - 0
Module_06/ex00/srcs/main.cpp

@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 12:47:49 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 12:54:24 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ScalarConverter.hpp"
+
+int main(int ac, char **av) {
+    if (ac > 1) {
+        ScalarConverter::convert(av[1]);    
+    }
+}

+ 50 - 0
Module_06/ex01/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/05 14:44:17 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main Serializer
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 26 - 0
Module_06/ex01/includes/Data.hpp

@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Data.hpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 13:57:51 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 14:44:51 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef DATA_HPP
+# define DATA_HPP
+
+# include <iostream>
+# include <string>
+# include <stdint.h>
+
+typedef struct s_data
+{
+	uintptr_t 	data;
+
+} Data;
+
+#endif /* ************************************************************ DATA_H */

+ 38 - 0
Module_06/ex01/includes/Serializer.hpp

@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Serializer.hpp                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 13:55:22 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 19:11:57 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef SERIALIZER_HPP
+# define SERIALIZER_HPP
+
+# include <iostream>
+# include <string>
+# include "Data.hpp"
+
+class Serializer
+{
+	public:
+
+		Serializer( Serializer const & src );
+		~Serializer();
+
+		Serializer &		operator=( Serializer const & rhs );
+
+		static uintptr_t 	serialize(Data* ptr);
+		static Data* 		deserialize(uintptr_t raw);
+
+	private :
+		Serializer();
+};
+
+std::ostream &			operator<<( std::ostream & o, Serializer const & i );
+
+#endif /* ****************************************************** SERIALIZER_H */

+ 74 - 0
Module_06/ex01/srcs/Serializer.cpp

@@ -0,0 +1,74 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Serializer.cpp                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 13:55:21 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 14:49:18 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "Serializer.hpp"
+
+/*
+** ------------------------------- CONSTRUCTOR --------------------------------
+*/
+
+Serializer::Serializer()
+{
+}
+
+/*
+** -------------------------------- DESTRUCTOR --------------------------------
+*/
+
+Serializer::~Serializer()
+{
+}
+
+
+/*
+** --------------------------------- OVERLOAD ---------------------------------
+*/
+
+Serializer &				Serializer::operator=( Serializer const & rhs )
+{
+	(void)rhs;
+	//if ( this != &rhs )
+	//{
+		//this->_value = rhs.getValue();
+	//}
+	return *this;
+}
+
+std::ostream &			operator<<( std::ostream & o, Serializer const & i )
+{
+	(void)i;
+	//o << "Value = " << i.getValue();
+	return o;
+}
+
+
+/*
+** --------------------------------- METHODS ----------------------------------
+*/
+
+uintptr_t 		Serializer::serialize(Data* ptr) {
+	return ptr->data;
+}
+
+Data* 			Serializer::deserialize(uintptr_t raw) {
+	Data 	*ptr = new Data();
+
+	ptr->data = raw;
+	return ptr;
+}
+
+/*
+** --------------------------------- ACCESSOR ---------------------------------
+*/
+
+
+/* ************************************************************************** */

+ 37 - 0
Module_06/ex01/srcs/main.cpp

@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 12:47:49 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/05 14:52:57 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "Serializer.hpp"
+
+int main(void) {
+    Data    ori;
+
+    ori.data = 3940;
+
+    uintptr_t i = Serializer::serialize(&ori);
+
+    if (ori.data == i ) {
+        std::cout << "original uintptr_t from Data == return value of serialize function." << std::endl;
+    }
+    std::cout << "original : " << ori.data << "; Returned : " << i << std::endl;
+
+   Data  *test = Serializer::deserialize(i);
+
+    if (test->data == i ) {
+        std::cout << "original uintptr_t from serialize == Data of return value of deserialize function." << std::endl;
+    }
+    std::cout << "original : " << i << "; returned : " << test->data << std::endl;
+
+    if (ori.data == test->data)
+        std::cout << "both Data struct pointeur are ==" << std::endl;
+    return 0;
+}

+ 50 - 0
Module_06/ex02/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/06 13:56:12 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 24 - 0
Module_06/ex02/includes/Base.hpp

@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Base.hpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 14:56:40 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/06 14:17:06 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef BASE_HPP
+# define BASE_HPP
+
+# include <iostream>
+# include <string>
+
+class Base { public: virtual ~Base() {};};
+class A : public Base {};
+class B : public Base {};
+class C : public Base {};
+
+#endif /* ************************************************************ BASE_H */

+ 76 - 0
Module_06/ex02/srcs/main.cpp

@@ -0,0 +1,76 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/05 12:47:49 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/06 14:21:45 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "Base.hpp"
+#include <cstdlib>
+#include <ctime>
+
+Base    *generate(void) {
+    srand(time(0));
+    int randomNum = rand() % 3;
+
+    if (randomNum == 0)
+        return new A();
+    else if (randomNum == 1)
+        return new B();
+    else
+        return new C();
+}
+
+void    identify(Base *p) {
+    A   *checkA = dynamic_cast<A*>(p);
+    B   *checkB = dynamic_cast<B*>(p);
+    C   *checkC = dynamic_cast<C*>(p);
+
+    std::cout << "With pointer" << std::endl;
+    if (checkA)
+        std::cout << "A" << std::endl;
+    else if (checkB)
+        std::cout << "B" << std::endl;
+    else if (checkC)
+        std::cout << "C" << std::endl;
+    else
+        std::cout << "None of A, B or C" << std::endl;
+}
+
+void    identify(Base &p) {
+    std::cout << "With reference" << std::endl;
+    try {
+        A   &checkA = dynamic_cast<A&>(p);
+        std::cout << "A" << std::endl;
+        (void)checkA;
+        return ;
+    } catch (std::bad_cast &bc) {}
+
+    try {
+        B   &checkB = dynamic_cast<B&>(p);
+        std::cout << "B" << std::endl;
+        (void)checkB;
+        return ;
+    } catch (std::bad_cast &bc) {}
+    try {
+        C   &checkC = dynamic_cast<C&>(p);
+        std::cout << "C" << std::endl;
+        (void)checkC;
+        return ;
+    } catch (std::bad_cast &bc) {}
+
+    std::cout << "None of A, B or C" << std::endl;
+}
+
+int main(void) {
+    C   test = C();
+
+    std::cout << "test" << std::endl;
+    identify(generate());
+    identify(test);
+}