Compare commits

...

8 Commits

8 changed files with 262 additions and 1633 deletions

View File

@@ -13,7 +13,10 @@ Date::Date(void)
Date::Date(std::string str) 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) for (size_t i = 0; str[i]; ++i)
if (!isdigit(str[i]) && !((i == (str.length() - 6) || i == (str.length() - 3)) && str[i] == '-'))
throw std::invalid_argument("invalid date.");
_value = 0; _value = 0;
for (std::string::iterator it = str.begin(); it < str.end(); ++it) for (std::string::iterator it = str.begin(); it < str.end(); ++it)
{ {
@@ -22,7 +25,6 @@ Date::Date(std::string str)
_value *= 10; _value *= 10;
_value += *it - '0'; _value += *it - '0';
} }
int year = _value / 10000; int year = _value / 10000;
int month = (_value / 100) % 100; int month = (_value / 100) % 100;
int day = _value % 100; int day = _value % 100;
@@ -156,7 +158,7 @@ void BitcoinExchange::str(std::string filename)
std::cout << "Error: bad input => " << line << std::endl; std::cout << "Error: bad input => " << line << std::endl;
continue ; continue ;
} }
if (std::atol(value_str.c_str()) > 2147483647) if (std::atol(value_str.c_str()) > 1000)
{ {
std::cout << "Error: too large a number." << std::endl; std::cout << "Error: too large a number." << std::endl;
continue ; continue ;
@@ -167,7 +169,7 @@ void BitcoinExchange::str(std::string filename)
continue ; continue ;
} }
try { try {
Date date(date_str); Date date(trim(date_str));
if (date < _data.begin()->first) if (date < _data.begin()->first)
{ {
std::cout << "Error: input date too low." << std::endl; std::cout << "Error: input date too low." << std::endl;

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +0,0 @@
date | value
1971-01-02 | 5
1971-31-02 | 5
2011-01-03 | 3
2011-01-03 | 2
2011-01-03 | 1
2011-01-03 | 1.2
2011-01-09 | 1
2012-01-11 | -1
2001-42-42
2012-01-11 | 1
2012-01-11 | 2147483648

View File

@@ -15,10 +15,10 @@ typedef int (*OperatorFunctions)(int, int);
void rpn(char *av) void rpn(char *av)
{ {
OperatorFunctions calc[128] = { NULL }; OperatorFunctions calc[128] = { NULL };
calc['+'] = &add; calc[(int) '+'] = &add;
calc['-'] = &subst; calc[(int) '-'] = &subst;
calc['*'] = &mult; calc[(int) '*'] = &mult;
calc['/'] = &div; calc[(int) '/'] = &div;
std::istringstream ss_input((std::string(av))); std::istringstream ss_input((std::string(av)));
std::stack<int> stack; std::stack<int> stack;

58
ex02/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/09/01 15:45:57 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CXX = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = PmergeMe
SRCS = main.cpp PmergeMe.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CXX) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CXX) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

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;
}

92
ex02/PmergeMe.hpp Normal file
View File

@@ -0,0 +1,92 @@
#pragma once
#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <deque>
std::vector<size_t> generateJacobsthal(size_t n);
template <typename Container, typename T>
typename Container::iterator binaryInsertPosition(Container &c, const T &value)
{
typename Container::iterator first = c.begin();
typename Container::iterator last = c.end();
while (first < last)
{
typename Container::iterator mid = first + (last - first) / 2;
if (*mid < value)
first = mid + 1;
else
last = mid;
}
return first;
}
template <typename Container>
Container fordJohnsonSort(const Container &input)
{
typedef typename Container::value_type T;
Container result;
if (input.size() <= 1)
return input;
std::vector<std::pair<T, T> > pairs;
size_t i = 0;
for (; i + 1 < input.size(); i += 2)
{
T a = input[i];
T b = input[i+1];
if (a > b)
std::swap(a, b);
pairs.push_back(std::make_pair(a, b));
}
Container larger;
for (size_t j = 0; j < pairs.size(); ++j)
larger.push_back(pairs[j].second);
larger = fordJohnsonSort(larger);
result = larger;
std::vector<size_t> order = generateJacobsthal(pairs.size());
std::vector<bool> inserted(pairs.size(), false);
for (size_t idx = 0; idx < order.size(); ++idx)
{
size_t j = order[idx];
if (j < pairs.size() && !inserted[j])
{
typename Container::iterator pos = binaryInsertPosition(result, pairs[j].first);
result.insert(pos, pairs[j].first);
inserted[j] = true;
}
}
for (size_t j = 0; j < pairs.size(); ++j)
{
if (!inserted[j])
{
typename Container::iterator pos = binaryInsertPosition(result, pairs[j].first);
result.insert(pos, pairs[j].first);
}
}
if (i < input.size())
{
T leftover = input[i];
typename Container::iterator pos = binaryInsertPosition(result, leftover);
result.insert(pos, leftover);
}
return result;
}

79
ex02/main.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include "PmergeMe.hpp"
bool isPositiveInteger(const std::string &s) {
if (s.empty()) return false;
for (size_t i = 0; i < s.size(); i++) {
if (!isdigit(s[i])) return false;
}
return true;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cerr << "Error" << std::endl;
return 1;
}
std::vector<int> vec;
std::deque<int> deq;
for (int i = 1; i < argc; ++i)
{
std::string arg(argv[i]);
if (!isPositiveInteger(arg))
{
std::cerr << "Error" << std::endl;
return 1;
}
std::istringstream iss(arg);
long num;
iss >> num;
if (num < 0 || num > INT_MAX)
{
std::cerr << "Error" << std::endl;
return 1;
}
vec.push_back(static_cast<int>(num));
deq.push_back(static_cast<int>(num));
}
std::cout << "Before: ";
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
std::cout << "[...] (" << vec.size() << ")";
std::cout << std::endl;
clock_t startVec = clock();
vec = fordJohnsonSort(vec);
clock_t endVec = clock();
clock_t startDeq = clock();
deq = fordJohnsonSort(deq);
clock_t endDeq = clock();
std::cout << "After: ";
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
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;
std::cout << "Time to process a range of " << vec.size()
<< " elements with std::vector : " << std::fixed << std::setprecision(1) << vecTime << " us" << std::endl;
std::cout << "Time to process a range of " << deq.size()
<< " elements with std::deque : " << std::fixed << std::setprecision(1) << deqTime << " us" << std::endl;
return 0;
}