52 lines
1 KiB
C++
52 lines
1 KiB
C++
#include "Span.hpp"
|
|
|
|
Span::Span() {
|
|
N = 0;
|
|
}
|
|
|
|
Span::Span(unsigned int N) : N(N) {
|
|
|
|
}
|
|
|
|
Span::Span(const Span &span) {
|
|
*this = span;
|
|
}
|
|
|
|
Span::~Span() {
|
|
|
|
}
|
|
|
|
Span &Span::operator=(const Span &span) {
|
|
if (this != &span) {
|
|
this->N = span.N;
|
|
this->list = span.list;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
void Span::addNumber(int nbr) {
|
|
if (list.size() + 1 > N)
|
|
throw std::out_of_range("Out of Range");
|
|
list.push_back(nbr);
|
|
}
|
|
|
|
unsigned int Span::shortestSpan() const {
|
|
if (list.size() < 2)
|
|
throw std::exception();
|
|
unsigned int min = longestSpan();
|
|
for (std::list<int>::const_iterator it0 = list.begin(); it0 != list.end(); it0++) {
|
|
for (std::list<int>::const_iterator it1 = list.begin(); it1 != list.end(); it1++) {
|
|
if (it0 != it1)
|
|
if ((long)std::abs(*it0 - *it1) < (long)min)
|
|
min = std::abs(*it0 - *it1);
|
|
}
|
|
}
|
|
return min;
|
|
}
|
|
|
|
unsigned int Span::longestSpan() const {
|
|
if (list.size() < 2)
|
|
throw std::exception();
|
|
return (*std::max_element(list.begin(), list.end()) - *std::min_element(list.begin(), list.end()));
|
|
}
|