Compare commits
3 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
6090fe193d | |
|
|
93f06c3b6d | |
|
|
bf4a571b4b |
|
|
@ -9,24 +9,44 @@ int main()
|
|||
vec.push_back(42);
|
||||
vec.push_back(56);
|
||||
vec.push_back(87);
|
||||
std::cout << "Test vector container" << std::endl << "Find 42 (pos 2)" << std::endl;
|
||||
std::vector<int>::iterator it = easyfind(vec, 42);
|
||||
std::cout << "Find " << *it << std::endl;
|
||||
|
||||
std::list<int> lst;
|
||||
lst.push_back(10);
|
||||
lst.push_back(47);
|
||||
lst.push_back(42);
|
||||
lst.push_back(87);
|
||||
std::cout << "Test list container" << std::endl << "Find 42 (pos 3)" << std::endl;
|
||||
std::list<int>::iterator it2 = easyfind(lst, 42);
|
||||
std::cout << "Find " << *it2 << std::endl;
|
||||
|
||||
std::cout << "Test vector container" << std::endl << "Find 41 (not here !)" << std::endl;
|
||||
std::cout << "--- TEST 1 : vector, search 10 (present) ---" << std::endl;
|
||||
try {
|
||||
std::vector<int>::iterator it3 = easyfind(vec, 41);
|
||||
std::cout << "found " << *it3 << std::endl;
|
||||
std::vector<int>::iterator it = easyfind(vec, 10);
|
||||
std::cout << "Found : " << *it << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << "Exeption catch : not found !" << std::endl;
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "--- TEST 2 : list, search 42 (present) ---" << std::endl;
|
||||
try {
|
||||
std::list<int>::iterator it = easyfind(lst, 42);
|
||||
std::cout << "Found : " << *it << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "--- TEST 3 : vector, search 41 (not present) ---" << std::endl;
|
||||
try {
|
||||
std::vector<int>::iterator it = easyfind(vec, 41);
|
||||
std::cout << "Found : " << *it << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "--- TEST 4 : list, search 99 (not present) ---" << std::endl;
|
||||
try {
|
||||
std::list<int>::iterator it = easyfind(lst, 99);
|
||||
std::cout << "Found : " << *it << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << "Exception : " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,35 @@
|
|||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Test 1 :" << std::endl;
|
||||
|
||||
Span sp = Span(5);
|
||||
sp.addNumber(6);
|
||||
sp.addNumber(3);
|
||||
sp.addNumber(17);
|
||||
sp.addNumber(9);
|
||||
sp.addNumber(11);
|
||||
|
||||
std::cout << sp.shortestSpan() << std::endl;
|
||||
std::cout << sp.longestSpan() << std::endl;
|
||||
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
std::cout << "Test 2 :" << std::endl;
|
||||
|
||||
Span sp2 = Span(5);
|
||||
std::vector<int> vec;
|
||||
vec.push_back(42);
|
||||
vec.push_back(1);
|
||||
vec.push_back(99);
|
||||
vec.push_back(7);
|
||||
vec.push_back(55);
|
||||
sp2.addNumber(vec.begin(), vec.end());
|
||||
|
||||
std::cout << sp2.shortestSpan() << std::endl;
|
||||
std::cout << sp2.longestSpan() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp
|
||||
SOURCES = Main.cpp Span.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Span
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,64 @@
|
|||
#include "Span.hpp"
|
||||
|
||||
Span::Span() : _n(0)
|
||||
{
|
||||
}
|
||||
|
||||
Span::Span(unsigned int n) : _n(n)
|
||||
{
|
||||
}
|
||||
|
||||
Span::Span(const Span &other) : _n(other._n), _vec(other._vec)
|
||||
{
|
||||
}
|
||||
|
||||
Span &Span::operator=(const Span& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
_n = other._n;
|
||||
_vec = other._vec;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Span::~Span()
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int Span::longestSpan() const
|
||||
{
|
||||
if (_vec.empty() || _vec.size() == 1)
|
||||
throw std::out_of_range("Span vector has less than 2 values");
|
||||
|
||||
std::vector<int>::const_iterator maxIt = std::max_element(_vec.begin(), _vec.end());
|
||||
std::vector<int>::const_iterator minIt = std::min_element(_vec.begin(), _vec.end());
|
||||
return (*maxIt - *minIt);
|
||||
}
|
||||
|
||||
int Span::shortestSpan() const
|
||||
{
|
||||
if (_vec.empty() || _vec.size() == 1)
|
||||
throw std::out_of_range("Span vector has less than 2 values");
|
||||
|
||||
std::vector<int> tempvec = _vec;
|
||||
std::sort(tempvec.begin(), tempvec.end());
|
||||
int minspan = std::numeric_limits<int>::max();
|
||||
|
||||
for (size_t i = 0; i < _vec.size() - 1; ++i)
|
||||
{
|
||||
int tempspan = tempvec[i + 1] - tempvec[i];
|
||||
if (tempspan < minspan)
|
||||
minspan = tempspan;
|
||||
}
|
||||
return (minspan);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef SPAN_HPP
|
||||
#define SPAN_HPP
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
class Span
|
||||
|
|
@ -22,6 +24,14 @@ class Span
|
|||
int longestSpan() const;
|
||||
void addNumber(int x);
|
||||
|
||||
template <typename Iterator>
|
||||
void addNumber(Iterator begin, Iterator end)
|
||||
{
|
||||
while (begin != end) {
|
||||
addNumber(*begin);
|
||||
begin++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
#include "MutantStack.hpp"
|
||||
#include <list>
|
||||
|
||||
int main()
|
||||
{
|
||||
// TEST 1 - MutantStack
|
||||
std::cout << "Test 1 :" << std::endl;
|
||||
MutantStack<int> 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<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
++it;
|
||||
--it;
|
||||
while (it != ite) {
|
||||
std::cout << *it << std::endl;
|
||||
++it;
|
||||
}
|
||||
std::stack<int> s(mstack);
|
||||
|
||||
std::cout << std::endl << "Test 2 :" << std::endl;
|
||||
// TEST 2 - std::list (même comportement attendu)
|
||||
std::list<int> 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<int>::iterator it2 = mlist.begin();
|
||||
std::list<int>::iterator ite2 = mlist.end();
|
||||
++it2;
|
||||
--it2;
|
||||
while (it2 != ite2) {
|
||||
std::cout << *it2 << std::endl;
|
||||
++it2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef MUTANTSTACK_HPP
|
||||
#define MUTANTSTACK_HPP
|
||||
|
||||
#include <stack>
|
||||
#include <iostream>
|
||||
template <typename T>
|
||||
class MutantStack : public std::stack<T>
|
||||
{
|
||||
public:
|
||||
MutantStack() {};
|
||||
~MutantStack() {};
|
||||
MutantStack(const MutantStack &other) {this = other;};
|
||||
MutantStack &operator=(const MutantStack &other);
|
||||
|
||||
typedef typename std::stack<T>::container_type::iterator iterator;
|
||||
typedef typename std::stack<T>::container_type::const_iterator const_iterator;
|
||||
typedef typename std::stack<T>::container_type::reverse_iterator reverse_iterator;
|
||||
typedef typename std::stack<T>::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
|
||||
Loading…
Reference in New Issue