Merge branch 'master' of bitbucket.org:hugogogo/piscine_cpp
This commit is contained in:
BIN
d08/correction_d08.png
Normal file
BIN
d08/correction_d08.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 425 KiB |
@@ -6,7 +6,7 @@
|
||||
|
||||
class easyfindException : public std::exception {
|
||||
virtual char const *what(void) const throw() {
|
||||
return "not found";
|
||||
return B_RED "not found" RESET;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -10,22 +10,20 @@
|
||||
#define N_TEST "3"
|
||||
|
||||
template < typename T >
|
||||
void standardTest(T container) {
|
||||
void standardTest(T container, int start, int end, int test) {
|
||||
typename T::const_iterator it;
|
||||
int test;
|
||||
|
||||
for (int i = -9 ; i < 10 ; i++)
|
||||
for (int i = start ; i < end ; i++)
|
||||
container.push_back(i);
|
||||
for (it = container.begin(); it != container.end(); it++)
|
||||
std::cout << *it << ": " << &*it << "\n";
|
||||
std::cout << "\n";
|
||||
test = -3;
|
||||
try {
|
||||
it = easyfind(container, test);
|
||||
std::cout << *it << ": " << &*it << "\n";
|
||||
std::cout << *it << ": " << &*it << "\n\n";
|
||||
}
|
||||
catch (std::exception const & e) {
|
||||
std::cout << test << ": " << e.what() << "\n";
|
||||
std::cout << test << ": " << e.what() << "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,22 +33,73 @@ int main() {
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests list :" RESET "\n";
|
||||
{
|
||||
std::list<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::list<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::list<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::list<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::list<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::list<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests vector :" RESET "\n";
|
||||
{
|
||||
std::vector<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::vector<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::vector<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::vector<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::vector<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::vector<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests deque :" RESET "\n";
|
||||
{
|
||||
std::deque<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::deque<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::deque<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::deque<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::deque<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::deque<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -68,7 +68,7 @@ bool Span::empty() const {
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void Span::addNumber(int nb) {
|
||||
void Span::addNumber(int nb) {
|
||||
if (_container.size() >= _max)
|
||||
throw std::out_of_range(B_RED "out of range number" RESET);
|
||||
_container.push_back(nb);
|
||||
@@ -98,8 +98,6 @@ void Span::addNumber(InputIterator first, InputIterator last) {
|
||||
}
|
||||
template void Span::addNumber<int*>(int*, int*);
|
||||
|
||||
|
||||
|
||||
unsigned int Span::shortestSpan() {
|
||||
int const size = _container.size();
|
||||
unsigned int shortest = longestSpan();
|
||||
@@ -123,9 +121,3 @@ unsigned int Span::longestSpan() {
|
||||
return (_sort.back() - _sort.front());
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* NESTED CLASS
|
||||
*********************************************/
|
||||
|
||||
//void Span::Class::function() {}
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@ public:
|
||||
Span & operator=( Span const & rhs );
|
||||
|
||||
void addNumber(int nb);
|
||||
// void addNumber(int * arr, unsigned int size);
|
||||
template <class InputIterator>
|
||||
void addNumber(InputIterator first, InputIterator last);
|
||||
// void addNumber(int * arr, unsigned int size);
|
||||
|
||||
unsigned int shortestSpan();
|
||||
unsigned int longestSpan();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "colors.h"
|
||||
#include <cstdlib> // rand
|
||||
|
||||
#include "Span.hpp"
|
||||
|
||||
|
||||
@@ -32,9 +32,8 @@ D_SRCS = .
|
||||
SRCS = main.cpp
|
||||
|
||||
D_HEADERS = .
|
||||
HEADERS =
|
||||
#HEADERS = colors.h \
|
||||
# MutantStack.hpp
|
||||
HEADERS = colors.h \
|
||||
MutantStack.hpp
|
||||
|
||||
D_OBJS = builds
|
||||
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
||||
|
||||
@@ -7,22 +7,24 @@ template <typename T>
|
||||
class MutantStack : public std::stack<T> {
|
||||
|
||||
public:
|
||||
/*
|
||||
typedef T * iterator;
|
||||
typedef T const * const_iterator;
|
||||
|
||||
iterator begin() {return (&this->top() - (this->size() - 1));}
|
||||
iterator end() {return (&this->top()+ 1);}
|
||||
iterator end() {return (&this->top() + 1);}
|
||||
|
||||
const_iterator begin() const {return (&this->top() - (this->size() - 1));}
|
||||
const_iterator end() const {return (&this->top()+ 1);}
|
||||
const_iterator end() const {return (&this->top() + 1);}
|
||||
*/
|
||||
|
||||
// typedef typename std::stack<T>::container_type::iterator iterator;
|
||||
// typedef typename std::stack<T>::container_type::const_iterator const_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();}
|
||||
typedef typename std::stack<T>::container_type::iterator iterator;
|
||||
typedef typename std::stack<T>::container_type::const_iterator const_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();}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include <iostream>
|
||||
#include "colors.h"
|
||||
#include "MutantStack.hpp"
|
||||
#include <cstdlib> // rand
|
||||
|
||||
#define N_TEST "2"
|
||||
#define N_TEST "3"
|
||||
|
||||
int main() {
|
||||
int i = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "test simple iterator :" RESET "\n";
|
||||
@@ -26,6 +28,100 @@ int main() {
|
||||
std::cout << *it << "\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "test stack size 128:" RESET "\n";
|
||||
{
|
||||
MutantStack<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 126; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
|
||||
std::cout << "[ ";
|
||||
for (; it != ite; it++)
|
||||
std::cout << *it << " ";
|
||||
std::cout << "]\n";
|
||||
|
||||
it = mstack.begin();
|
||||
ite = mstack.end();
|
||||
|
||||
std::cout << B_BLUE "firtst and last :" RESET "\n";
|
||||
std::cout << *it << " | " << *(ite - 1) << "\n\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "test stack size 129:" RESET "\n";
|
||||
{
|
||||
MutantStack<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 127; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
|
||||
std::cout << "[ ";
|
||||
for (; it != ite; it++)
|
||||
std::cout << *it << " ";
|
||||
std::cout << "]\n";
|
||||
|
||||
it = mstack.begin();
|
||||
ite = mstack.end();
|
||||
|
||||
std::cout << B_BLUE "firtst and last :" RESET "\n";
|
||||
std::cout << *it << " | " << *(ite - 1) << "\n\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "test stack size 10000:" RESET "\n";
|
||||
{
|
||||
MutantStack<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 10000; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
|
||||
std::cout << "[ ";
|
||||
for (; it != ite; it++)
|
||||
std::cout << *it << " ";
|
||||
std::cout << "]\n";
|
||||
|
||||
it = mstack.begin();
|
||||
ite = mstack.end();
|
||||
|
||||
std::cout << B_BLUE "firtst and last :" RESET "\n";
|
||||
std::cout << *it << " | " << *(ite - 1) << "\n\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "test const iterator :" RESET "\n";
|
||||
{
|
||||
MutantStack<int> mstack;
|
||||
|
||||
mstack.push(7);
|
||||
mstack.push(987);
|
||||
mstack.push(9);
|
||||
mstack.push(8);
|
||||
mstack.push(34);
|
||||
mstack.push(1);
|
||||
|
||||
MutantStack<int>::const_iterator it = mstack.begin();
|
||||
MutantStack<int>::const_iterator ite = mstack.end();
|
||||
|
||||
for (; it != ite; it++)
|
||||
std::cout << *it << "\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests subject :" RESET "\n";
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user