push
This commit is contained in:
parent
81a88d6af1
commit
bf4a571b4b
|
|
@ -10,5 +10,18 @@ int main()
|
||||||
sp.addNumber(11);
|
sp.addNumber(11);
|
||||||
std::cout << sp.shortestSpan() << std::endl;
|
std::cout << sp.shortestSpan() << std::endl;
|
||||||
std::cout << sp.longestSpan() << std::endl;
|
std::cout << sp.longestSpan() << std::endl;
|
||||||
|
|
||||||
|
std::vector<int> vec;
|
||||||
|
vec.push_back(42);
|
||||||
|
vec.push_back(1);
|
||||||
|
vec.push_back(99);
|
||||||
|
vec.push_back(7);
|
||||||
|
vec.push_back(55);
|
||||||
|
|
||||||
|
Span sp2 = Span(5);
|
||||||
|
sp2.addNumber(vec.begin(), vec.end());
|
||||||
|
std::cout << sp2.shortestSpan() << std::endl;
|
||||||
|
std::cout << sp2.longestSpan() << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
CXX = c++
|
CXX = c++
|
||||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
SOURCES = Main.cpp
|
SOURCES = Main.cpp Span.cpp
|
||||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||||
NAME = Span
|
NAME = Span
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1,59 @@
|
||||||
#include "Span.hpp"
|
#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
|
#ifndef SPAN_HPP
|
||||||
#define SPAN_HPP
|
#define SPAN_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <limits>
|
||||||
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Span
|
class Span
|
||||||
|
|
@ -22,6 +24,14 @@ class Span
|
||||||
int longestSpan() const;
|
int longestSpan() const;
|
||||||
void addNumber(int x);
|
void addNumber(int x);
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
void addNumber(Iterator begin, Iterator end)
|
||||||
|
{
|
||||||
|
while (begin != end) {
|
||||||
|
addNumber(*begin);
|
||||||
|
begin++;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue