ex02 starting

This commit is contained in:
2026-03-21 17:12:14 +01:00
parent cbcc65b196
commit 5c54cbee03
7 changed files with 183 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
NAME = swap NAME = swap
SRC = main.cpp \ SRC = main.cpp
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)

View File

@@ -1,6 +1,6 @@
NAME = iter NAME = iter
SRC = main.cpp \ SRC = main.cpp
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)

0
ex02/.build_start Normal file
View File

21
ex02/Array/Array.hpp Normal file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:04:01 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:04:01 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARRAY_HPP
# define ARRAY_HPP
# include <iostream>
# include "Array.tpp"
#endif

54
ex02/Array/Array.tpp Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:11:51 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
template <typename T>
class Array
{
private:
T *element;
unsigned int size;
public:
Array()
{
new T[n]();
}
Array(const Array &other)
{
if (this != other)
{
this->element = other.element;
this->size = other.size;
}
return (*this);
}
Array& operator=(const Array &other)
{
this = other;
}
~Array()
{
delete[] T;
}
Array(unsigned int n)
{
this->size = n;
}
int size()
{
return (this->size);
}
};

42
ex02/Makefile Normal file
View File

@@ -0,0 +1,42 @@
NAME = array
SRC = main.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

64
ex02/main.cpp Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:00:50 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:00:54 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Array/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;
}
//SCOPE
{
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;
}