🎉」 init: hello world !

This commit is contained in:
2025-08-16 20:30:27 +02:00
commit e0535f2b4a
9 changed files with 375 additions and 0 deletions

58
ex00/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/16 16:00:39 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CC = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = ex00
SRCS = main.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

23
ex00/main.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "whatever.hpp"
#include <iostream>
int main(void) {
int a = 2;
int b = 3;
::swap(a, b);
std::cout << "a = " << a << ", b = " << b << std::endl;
std::cout << "min( a, b ) = " << ::min(a, b) << std::endl;
std::cout << "max( a, b ) = " << ::max(a, b) << std::endl;
std::string c = "chaine1";
std::string d = "chaine2";
::swap(c, d);
std::cout << "c = " << c << ", d = " << d << std::endl;
std::cout << "min( c, d ) = " << ::min(c, d) << std::endl;
std::cout << "max( c, d ) = " << ::max(c, d) << std::endl;
return 0;
}

21
ex00/whatever.hpp Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
template <typename T> void swap(T &a, T &b)
{
T tmp;
tmp = a;
a = b;
b = tmp;
}
template <typename T> T min(T &a, T &b)
{
return (a < b ? a : b);
}
template <typename T> T max(T &a, T&b)
{
return (a > b ? a : b);
}

58
ex01/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/16 16:04:00 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CC = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = ex01
SRCS = main.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

13
ex01/iter.hpp Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
template<typename T> void iter(T *arr, int len, void(*func)(T &)) {
for (int i = 0; i< len; i++) {
func(arr[i]);
}
}
template<typename T> void iter(const T *arr, int len, void(*func)(const T &)) {
for (int i = 0; i< len; i++) {
func(arr[i]);
}
}

23
ex01/main.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "iter.hpp"
#include <iostream>
void increment(int &nb)
{
nb++;
}
void print(const int &nb)
{
std::cout << nb << " ";
}
int main(void)
{
int arr[4] = {1, 2, 3, 4};
iter(arr, 4, &print);
std::cout << std::endl;
iter(arr, 4, &increment);
iter(arr, 4, &print);
std::cout << std::endl;
}

47
ex02/Array.hpp Normal file
View File

@@ -0,0 +1,47 @@
#include <cstddef>
#include <exception>
#include <iterator>
#include <string>
template <typename T> class Array {
public:
Array(): _arr(new T[0]), _size(0) {}
Array(const int n): _arr(new T[n]), _size(n) {}
Array(const Array &cpy) {*this = cpy;}
~Array(void) {delete[] _arr;}
Array &operator=(const Array &cpy)
{
if (this != &cpy) {
_size = cpy._size;
_arr = new T[_size];
if (_arr)
{
for (std::size_t i = 0; i < _size; i++)
_arr[i] = cpy._arr[i];
}
}
return (*this);
}
T &operator[](const size_t &pos)
{
if (pos < 0 || pos >= _size)
{
throw outOfBoundException();
}
return _arr[pos];
}
class outOfBoundException : public std::exception {
virtual const char *what() const throw()
{
return ("value is out of bound");
}
};
private:
T *_arr;
size_t _size;
};

58
ex02/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/16 19:49:30 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CC = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = ex02
SRCS = main.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

74
ex02/main.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <iostream>
#include <cstdlib>
#include <Array.hpp>
#define MAX_VAL 750
int main(int, char**)
{
Array<int> numbers(MAX_VAL);
int* mirror = new int[MAX_VAL];
srand(time(NULL));
for (int i = 0; i < MAX_VAL; i++)
{
const int value = rand() % 100;
numbers[i] = value;
mirror[i] = value;
}
//SCOPE
{
Array<int> tmp = numbers;
Array<int> test(tmp);
}
std::cout << "checking if values are the same after a copy..." << std::endl;
for (int i = 0; i < MAX_VAL; i++)
{
if (mirror[i] != numbers[i])
{
std::cerr << "didn't save the same value!!" << std::endl;
return 1;
}
}
std::cout << "the values are the same ! :D" << std::endl << std::endl;
std::cout << "trying -2:" << std::endl;
try
{
numbers[-2] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
std::cout << std::endl << "trying a correct value (15):" << std::endl;
try
{
std::cout << numbers[15] << std::endl;
numbers[15] = 0;
std::cout << numbers[15] << std::endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
std::cout << std::endl << "trying MAX_VAL:" << std::endl;
try
{
numbers[MAX_VAL] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
std::cout << std::endl;
for (int i = 0; i < MAX_VAL; i++)
{
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
delete [] mirror;
return 0;
}