This commit is contained in:
aortigos
2026-04-21 11:18:55 +02:00
parent ab04a0f8fa
commit eaf9e30206
4 changed files with 211 additions and 0 deletions

43
ex01/Makefile Normal file
View File

@@ -0,0 +1,43 @@
NAME = span
SRC = main.cpp Span/Span.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

84
ex01/Span/Span.cpp Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/21 10:29:07 by aortigos #+# #+# */
/* Updated: 2026/04/21 10:29:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
//////////////////
// Constructors //
//////////////////
Span::Span() : n(0)
{
// std::cout << "Span default constructor called" << std::endl;
}
Span::Span(const Span &other)
{
*this = other;
// std::cout << "Span copy constructor called" << std::endl;
}
Span& Span::operator=(const Span &other)
{
if (this != &other)
{
this->data = other.data;
this->n = other.n;
}
// std::cout << "Span copy assignment operator called" << std::endl;
return (*this);
}
Span::~Span()
{
// std::cout << "Span destructor called" << std::endl;
}
Span::Span(unsigned int n) : n(n) { }
void Span::addNumber(int nb)
{
if (data.size() >= n)
{
throw std::runtime_error("Span is full.");
}
this->data.push_back(nb);
// std::cout << "addNumber: adding " << nb << std::endl;
}
int Span::shortestSpan()
{
if (this->data.size() < 2)
throw std::runtime_error("Span has no elements to analize.");
std::vector<int> vec = this->data;
std::sort(vec.begin(), vec.end());
int gap = INT_MAX;
for (unsigned int i = 0; i < vec.size() - 1; i++)
{
if ((vec[i+1] - vec[i]) < gap)
gap = vec[i+1] - vec[i];
}
return (gap);
}
int Span::longestSpan()
{
if (this->data.size() < 2)
throw std::runtime_error("Span has no elements to analize.");
std::vector<int> vec = this->data;
std::sort(vec.begin(), vec.end());
return (vec.back() - vec.front());
}

52
ex01/Span/Span.hpp Normal file
View File

@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/21 10:29:07 by aortigos #+# #+# */
/* Updated: 2026/04/21 10:29:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SPAN_HPP
# define SPAN_HPP
# include <iostream>
# include <vector>
# include <algorithm>
# include <stdexcept>
# include <climits>
class Span
{
private:
std::vector<int> data;
unsigned int n;
public:
Span();
Span(const Span &other);
Span& operator=(const Span &other);
~Span();
Span(unsigned int n);
void addNumber(int nb);
int shortestSpan();
int longestSpan();
template <typename iter>
void addNumbers(iter it, iter itt)
{
while (it != itt)
{
this->data.push_back(*it);
// std::cout << "addNumbers: adding " << *it << std::endl;
it++;
}
}
};
#endif

32
ex01/main.cpp Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/21 10:56:22 by aortigos #+# #+# */
/* Updated: 2026/04/21 11:18:21 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span/Span.hpp"
int main()
{
std::vector<int> vec;
vec.push_back(5);
vec.push_back(100);
vec.push_back(10);
vec.push_back(20);
Span sp(5);
sp.addNumbers(vec.begin(), vec.end());
sp.addNumber(500);
std::cout << "Shortest span: " << sp.shortestSpan() << std::endl;
std::cout << "Logest span: " << sp.longestSpan() << std::endl;
return (0);
}