🗑️」 clean: cleanup things and slightly improved ex02/main.cpp

This commit is contained in:
y-syo
2025-09-01 16:00:41 +02:00
parent 7b47994ddd
commit f9c38cfe86
6 changed files with 37 additions and 28 deletions

View File

@@ -13,8 +13,6 @@ Date::Date(void)
Date::Date(std::string str)
{
// check format (4 char & isdigit into dash into 2 char isdigit & < 1 && isdigit & < 2, and same for day except ahah funny cuz 30 and 31 and 28 and 29 agfjasfgfjahkfga)
_value = 0;
for (size_t i = 0; str[i]; ++i)
{
if (!isdigit(str[i]) && !((i == (str.length() - 6) || i == (str.length() - 3)) && str[i] == '-'))
@@ -23,6 +21,8 @@ Date::Date(std::string str)
throw std::invalid_argument("invalid date.");
}
}
_value = 0;
for (std::string::iterator it = str.begin(); it < str.end(); ++it)
{
if (!isdigit(*it))

View File

@@ -1,4 +1,5 @@
date | value
2002.01-2 | 5
1971-01-02 | 5
1971-31-02 | 5
2011-01-03 | 3

View File

@@ -6,7 +6,7 @@
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/30 13:38:12 by mmoussou ### ########.fr #
# Updated: 2025/09/01 15:45:57 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
@@ -27,7 +27,7 @@ INCLUDES = .
NAME = PmergeMe
SRCS = main.cpp #PmergeMe.cpp
SRCS = main.cpp PmergeMe.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))

23
ex02/PmergeMe.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "PmergeMe.hpp"
std::vector<size_t> generateJacobsthal(size_t n)
{
std::vector<size_t> seq;
if (n == 0)
return seq;
seq.push_back(0);
if (n == 1)
return seq;
seq.push_back(1);
size_t j0 = 0, j1 = 1;
while (true)
{
size_t jn = j1 + 2 * j0;
if (jn >= n)
break;
seq.push_back(jn);
j0 = j1;
j1 = jn;
}
return seq;
}

View File

@@ -11,27 +11,7 @@
#include <ctime>
#include <deque>
std::vector<size_t> generateJacobsthal(size_t n)
{
std::vector<size_t> seq;
if (n == 0)
return seq;
seq.push_back(0);
if (n == 1)
return seq;
seq.push_back(1);
size_t j0 = 0, j1 = 1;
while (true)
{
size_t jn = j1 + 2 * j0;
if (jn >= n)
break;
seq.push_back(jn);
j0 = j1;
j1 = jn;
}
return seq;
}
std::vector<size_t> generateJacobsthal(size_t n);
template <typename Container, typename T>
typename Container::iterator binaryInsertPosition(Container &c, const T &value)
@@ -41,7 +21,7 @@ typename Container::iterator binaryInsertPosition(Container &c, const T &value)
while (first < last)
{
typename Container::iterator mid = first + (last - first) / 2; //tzvetan told me a better way iirc but i don't remember D:
typename Container::iterator mid = first + (last - first) / 2;
if (*mid < value)
first = mid + 1;
else

View File

@@ -43,7 +43,7 @@ int main(int argc, char **argv)
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
std::cout << "[...]";
std::cout << "[...] (" << vec.size() << ")";
std::cout << std::endl;
clock_t startVec = clock();
@@ -58,9 +58,14 @@ int main(int argc, char **argv)
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
std::cout << "[...]";
std::cout << "[...] (" << vec.size() << ")";
std::cout << std::endl;
std::vector<int> sortedVec = vec;
std::sort(sortedVec.begin(), sortedVec.end());
if (vec != sortedVec)
throw std::runtime_error("it's not sorted, what-");
double vecTime = (double)(endVec - startVec) / CLOCKS_PER_SEC * 1000000.0;
double deqTime = (double)(endDeq - startDeq) / CLOCKS_PER_SEC * 1000000.0;