From eaf9e302067ed6f8e9fd2836fa6f5834736493fd Mon Sep 17 00:00:00 2001 From: aortigos Date: Tue, 21 Apr 2026 11:18:55 +0200 Subject: [PATCH] ex01 --- ex01/Makefile | 43 ++++++++++++++++++++++++ ex01/Span/Span.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++ ex01/Span/Span.hpp | 52 ++++++++++++++++++++++++++++ ex01/main.cpp | 32 ++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 ex01/Makefile create mode 100644 ex01/Span/Span.cpp create mode 100644 ex01/Span/Span.hpp create mode 100644 ex01/main.cpp diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..3c050fe --- /dev/null +++ b/ex01/Makefile @@ -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 diff --git a/ex01/Span/Span.cpp b/ex01/Span/Span.cpp new file mode 100644 index 0000000..ef3b3cd --- /dev/null +++ b/ex01/Span/Span.cpp @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 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 vec = this->data; + std::sort(vec.begin(), vec.end()); + + return (vec.back() - vec.front()); +} diff --git a/ex01/Span/Span.hpp b/ex01/Span/Span.hpp new file mode 100644 index 0000000..d279b75 --- /dev/null +++ b/ex01/Span/Span.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +# include +# include +# include +# include + +class Span +{ + private: + std::vector 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 + void addNumbers(iter it, iter itt) + { + while (it != itt) + { + this->data.push_back(*it); + // std::cout << "addNumbers: adding " << *it << std::endl; + it++; + } + } +}; + +#endif diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..50a3a3f --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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); +} \ No newline at end of file