push_final

This commit is contained in:
root 2026-02-04 02:08:57 +00:00
parent cd9912e9fc
commit b605c0bc52
6 changed files with 297 additions and 0 deletions

13
ex01/Iter.hpp Normal file
View File

@ -0,0 +1,13 @@
#ifndef ITER_HPP
#define ITER_HPP
#include <iostream>
template <typename Func, typename Arr>
void iter(Arr *ptr, size_t len, Func fonction)
{
size_t i = 0;
while (i < len)
fonction(ptr[i++]);
}
#endif

27
ex01/Main.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "Iter.hpp"
void print(std::string &str)
{
std::cout << str << std::endl;
}
void afficherConst(const int &nb)
{
std::cout << nb << std::endl;
}
void add_one(int nb)
{
std::cout << nb+1 << std::endl;
}
int main()
{
std::string tab[] = {"Hello", ", ", "World ", "!"};
iter(tab, 4, print);
std::cout << std::endl;
int tab2[] = {1, 2, 3, 41};
iter(tab2, 4, add_one);
std::cout << std::endl;
iter(tab2, 4, afficherConst);
return 0;
}

36
ex01/Makefile Normal file
View File

@ -0,0 +1,36 @@
CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
OBJDIR = obj
SOURCES = Main.cpp
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
NAME = Iter
all: $(NAME)
$(OBJDIR):
@echo "📁 Creating obj directory..."
@mkdir -p $(OBJDIR)
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
@echo "🧠 Compiling $< ..."
@$(CXX) $(CXXFLAGS) -c $< -o $@
@echo "$@ ready!"
$(NAME): $(OBJECTS)
@echo "🔗 Linking $(NAME) ..."
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
@echo "🎉 $(NAME) is ready!"
clean:
@echo "🧹 Cleaning object files..."
@rm -rf $(OBJDIR)
@echo "✨ Objects cleaned!"
fclean: clean
@echo "🗑️ Removing $(NAME)..."
@rm -f $(NAME)
@echo "💀 Full clean complete!"
re: fclean all
.PHONY: all clean fclean re

54
ex02/Array.hpp Normal file
View File

@ -0,0 +1,54 @@
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include <iostream>
template <typename T>
class Array
{
private:
T *_data; // pointeur vers les données
size_t _size; // nombre d'éléments
public:
Array() : _data(NULL), _size(0) {}
Array(unsigned int n)
{
_data = new T[n]();
_size = n;
}
Array(const Array &other) : _size(other._size)
{
_data = new T[_size]();
for (size_t i = 0; i < _size; i++)
_data[i] = other._data[i];
}
~Array()
{
delete[] _data;
}
Array &operator=(const Array &other)
{
if (this == &other)
return *this;
delete[] this->_data;
_size = other._size;
_data = new T[_size]();
for (size_t i = 0; i < _size; i++)
_data[i] = other._data[i];
return *this;
}
T &operator[](size_t index)
{
if (this->_size <= index)
throw std::out_of_range("Out of array size");
else
return this->_data[index];
}
size_t size() const
{
return this->_size;
}
};
#endif

131
ex02/Main.cpp Normal file
View File

@ -0,0 +1,131 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#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();
// numbers[i] = value;
// mirror[i] = value;
// }
// {
// Array<int> tmp = numbers;
// Array<int> test(tmp);
// }
// 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;
// }
// }
// try
// {
// numbers[-2] = 0;
// }
// catch(const std::exception& e)
// {
// std::cerr << e.what() << '\n';
// }
// try
// {
// numbers[MAX_VAL] = 0;
// }
// catch(const std::exception& e)
// {
// std::cerr << e.what() << '\n';
// }
// for (int i = 0; i < MAX_VAL; i++)
// {
// numbers[i] = rand();
// }
// delete [] mirror;
// return 0;
// }
int main(int, char**)
{
std::cout << "=== Test 1: Creation d'un Array ===" << std::endl;
Array<int> numbers(MAX_VAL);
std::cout << "Array cree avec " << numbers.size() << " elements" << std::endl;
int* mirror = new int[MAX_VAL];
srand(time(NULL));
std::cout << "\n=== Test 2: Remplissage de l'Array ===" << std::endl;
for (int i = 0; i < MAX_VAL; i++)
{
const int value = rand();
numbers[i] = value;
mirror[i] = value;
}
std::cout << "Array rempli avec " << MAX_VAL << " valeurs aleatoires" << std::endl;
std::cout << "\n=== Test 3: Copy Constructor et Destructeur ===" << std::endl;
{
std::cout << "Creation de copies dans un scope..." << std::endl;
Array<int> tmp = numbers;
Array<int> test(tmp);
std::cout << "Copies creees" << std::endl;
}
std::cout << "Sortie du scope, destructeurs appeles" << std::endl;
std::cout << "\n=== Test 4: Verification que les donnees n'ont pas change ===" << std::endl;
bool ok = true;
for (int i = 0; i < MAX_VAL; i++)
{
if (mirror[i] != numbers[i])
{
std::cerr << "didn't save the same value!!" << std::endl;
ok = false;
break;
}
}
if (ok)
std::cout << "OK: Les donnees sont intactes apres les copies" << std::endl;
std::cout << "\n=== Test 5: Exception - Index negatif ===" << std::endl;
try
{
std::cout << "Tentative d'acces avec index -2..." << std::endl;
numbers[-2] = 0;
}
catch(const std::exception& e)
{
std::cout << "Exception capturee: " << e.what() << std::endl;
}
std::cout << "\n=== Test 6: Exception - Index hors limites ===" << std::endl;
try
{
std::cout << "Tentative d'acces avec index " << MAX_VAL << "..." << std::endl;
numbers[MAX_VAL] = 0;
}
catch(const std::exception& e)
{
std::cout << "Exception capturee: " << e.what() << std::endl;
}
std::cout << "\n=== Test 7: Assignment Operator ===" << std::endl;
Array<int> backup(5);
std::cout << "Backup cree avec 5 elements" << std::endl;
backup = numbers;
std::cout << "Assignment: backup = numbers (nouveau size: " << backup.size() << ")" << std::endl;
for (int i = 0; i < MAX_VAL; i++)
{
numbers[i] = rand();
}
delete [] mirror;
return 0;
}

36
ex02/Makefile Normal file
View File

@ -0,0 +1,36 @@
CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
OBJDIR = obj
SOURCES = Main.cpp
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
NAME = Array
all: $(NAME)
$(OBJDIR):
@echo "📁 Creating obj directory..."
@mkdir -p $(OBJDIR)
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
@echo "🧠 Compiling $< ..."
@$(CXX) $(CXXFLAGS) -c $< -o $@
@echo "$@ ready!"
$(NAME): $(OBJECTS)
@echo "🔗 Linking $(NAME) ..."
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
@echo "🎉 $(NAME) is ready!"
clean:
@echo "🧹 Cleaning object files..."
@rm -rf $(OBJDIR)
@echo "✨ Objects cleaned!"
fclean: clean
@echo "🗑️ Removing $(NAME)..."
@rm -f $(NAME)
@echo "💀 Full clean complete!"
re: fclean all
.PHONY: all clean fclean re