diff --git a/ex01/Span b/ex01/Span deleted file mode 100755 index 5766e55..0000000 Binary files a/ex01/Span and /dev/null differ diff --git a/ex01/Span.cpp b/ex01/Span.cpp index ca8632c..316cfdc 100644 --- a/ex01/Span.cpp +++ b/ex01/Span.cpp @@ -30,6 +30,7 @@ void Span::addNumber(int x) if (this->_vec.size() >= this->_n) throw std::out_of_range("Span vector is full"); + if (this->_vec.size() < this->_n) this->_vec.push_back(x); } @@ -38,6 +39,7 @@ int Span::longestSpan() const { if (_vec.empty() || _vec.size() == 1) throw std::out_of_range("Span vector has less than 2 values"); + std::vector::const_iterator maxIt = std::max_element(_vec.begin(), _vec.end()); std::vector::const_iterator minIt = std::min_element(_vec.begin(), _vec.end()); return (*maxIt - *minIt); @@ -47,10 +49,13 @@ int Span::shortestSpan() const { if (_vec.empty() || _vec.size() == 1) throw std::out_of_range("Span vector has less than 2 values"); + std::vector tempvec = _vec; std::sort(tempvec.begin(), tempvec.end()); int minspan = std::numeric_limits::max(); - for (size_t i = 0; i < _vec.size() - 1; ++i) { + + for (size_t i = 0; i < _vec.size() - 1; ++i) + { int tempspan = tempvec[i + 1] - tempvec[i]; if (tempspan < minspan) minspan = tempspan; diff --git a/ex01/obj/Main.o b/ex01/obj/Main.o deleted file mode 100644 index 4ca5710..0000000 Binary files a/ex01/obj/Main.o and /dev/null differ diff --git a/ex01/obj/Span.o b/ex01/obj/Span.o deleted file mode 100644 index 5d48f9a..0000000 Binary files a/ex01/obj/Span.o and /dev/null differ diff --git a/ex02/Main.cpp b/ex02/Main.cpp new file mode 100644 index 0000000..2fd22bf --- /dev/null +++ b/ex02/Main.cpp @@ -0,0 +1,50 @@ +#include "MutantStack.hpp" +#include + +int main() +{ + // TEST 1 - MutantStack + std::cout << "test 1" << std::endl << std::endl; + MutantStack mstack; + mstack.push(5); + mstack.push(17); + std::cout << mstack.top() << std::endl; + mstack.pop(); + std::cout << mstack.size() << std::endl; + mstack.push(3); + mstack.push(5); + mstack.push(737); + mstack.push(0); + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + ++it; + --it; + while (it != ite) { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + + std::cout << std::endl << "test 2" << std::endl << std::endl; + // TEST 2 - std::list (mΓͺme comportement attendu) + std::list mlist; + mlist.push_back(5); + mlist.push_back(17); + std::cout << mlist.back() << std::endl; + mlist.pop_back(); + std::cout << mlist.size() << std::endl; + mlist.push_back(3); + mlist.push_back(5); + mlist.push_back(737); + mlist.push_back(0); + std::list::iterator it2 = mlist.begin(); + std::list::iterator ite2 = mlist.end(); + ++it2; + --it2; + while (it2 != ite2) { + std::cout << *it2 << std::endl; + ++it2; + } + + return 0; +} diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..fd66934 --- /dev/null +++ b/ex02/Makefile @@ -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 = Mstack + +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 diff --git a/ex02/MutantStack.hpp b/ex02/MutantStack.hpp new file mode 100644 index 0000000..7fd48ce --- /dev/null +++ b/ex02/MutantStack.hpp @@ -0,0 +1,30 @@ +#ifndef MUTANTSTACK_HPP +#define MUTANTSTACK_HPP + +#include +#include +template +class MutantStack : public std::stack +{ + public: + MutantStack() {}; + ~MutantStack() {}; + MutantStack(const MutantStack &other) {this = other;}; + MutantStack &operator=(const MutantStack &other); + + typedef typename std::stack::container_type::iterator iterator; + typedef typename std::stack::container_type::const_iterator const_iterator; + typedef typename std::stack::container_type::reverse_iterator reverse_iterator; + typedef typename std::stack::container_type::const_reverse_iterator const_reverse_iterator; + + iterator begin() {return this->c.begin();} + iterator end() {return this->c.end();} + const_iterator begin() const {return this->c.begin();} + const_iterator end() const {return this->c.end();} + reverse_iterator rbegin() {return this->c.rbegin();} + reverse_iterator rend() {return this->c.rend();} + const_reverse_iterator rbegin() const {return this->c.rbegin();} + const_reverse_iterator rend() const {return this->c.rend();} +}; + +#endif