ex01 done

This commit is contained in:
2026-03-21 16:58:41 +01:00
parent f05f057399
commit cbcc65b196
3 changed files with 139 additions and 0 deletions

42
ex01/Makefile Normal file
View File

@@ -0,0 +1,42 @@
NAME = iter
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

33
ex01/iter.hpp Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iter.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 15:53:06 by aortigos #+# #+# */
/* Updated: 2026/03/21 16:58:31 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ITER_HPP
# define ITER_HPP
# include <iostream>
# include <cctype>
template <typename PTR, typename F>
void iter(PTR *address, size_t const lenght, F func)
{
size_t i;
i = 0;
while (i < lenght)
{
func(address[i]);
i++;
}
}
#endif

64
ex01/main.cpp Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 16:57:02 by aortigos #+# #+# */
/* Updated: 2026/03/21 16:57:36 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "iter.hpp"
template <typename T>
void printElement(const T& elem)
{
std::cout << elem << " ";
}
void toUpper(std::string& str)
{
for (size_t i = 0; i < str.length(); i++)
str[i] = std::toupper(str[i]);
}
void increment(int& x)
{
x++;
}
int main()
{
// -------- STRING ARRAY --------
std::string words[] = {"hola", "mundo", "iter"};
size_t wordsLen = sizeof(words) / sizeof(words[0]);
std::cout << "Original strings: ";
iter(words, wordsLen, printElement<std::string>);
std::cout << std::endl;
iter(words, wordsLen, toUpper);
std::cout << "Uppercase strings: ";
iter(words, wordsLen, printElement<std::string>);
std::cout << std::endl;
// -------- INT ARRAY --------
int numbers[] = {1, 2, 3, 4};
size_t numLen = sizeof(numbers) / sizeof(numbers[0]);
std::cout << "\nOriginal ints: ";
iter(numbers, numLen, printElement<int>);
std::cout << std::endl;
iter(numbers, numLen, increment);
std::cout << "Incremented ints: ";
iter(numbers, numLen, printElement<int>);
std::cout << std::endl;
return 0;
}