add: ex02
This commit is contained in:
parent
6e0271c5a2
commit
f2fb5bf00b
26
ex02/Makefile
Normal file
26
ex02/Makefile
Normal file
@ -0,0 +1,26 @@
|
||||
CXX := c++
|
||||
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
|
||||
SRCDIR := src
|
||||
OBJDIR := obj
|
||||
NAME := PmergeMe
|
||||
|
||||
SRCS := $(wildcard $(SRCDIR)/*.cpp)
|
||||
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
||||
mkdir -p obj
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJDIR)
|
||||
|
||||
fclean: clean
|
||||
rm -fr $(NAME)
|
||||
|
||||
re: fclean
|
||||
@make --no-print-directory all
|
33
ex02/src/PmergeMe.cpp
Normal file
33
ex02/src/PmergeMe.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "./PmergeMe.hpp"
|
||||
#include <future>
|
||||
#include <utility>
|
||||
|
||||
void sort_pair(std::pair<int, int>* pairs, size_t len)
|
||||
{
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
if (pairs[i].second > pairs[i].first)
|
||||
std::swap(pairs[i].first, pairs[i].second);
|
||||
}
|
||||
}
|
||||
|
||||
void sort_pairs(std::pair<int, int>* pairs, size_t len)
|
||||
{
|
||||
if (len == 1)
|
||||
return;
|
||||
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
if (pairs[i].first > pairs[i + 1].first)
|
||||
{
|
||||
std::swap(pairs[i], pairs[i + 1]);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
sort_pair(pairs, len - 1);
|
||||
}
|
51
ex02/src/PmergeMe.hpp
Normal file
51
ex02/src/PmergeMe.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
void sort_pair(std::pair<int, int>* pairs, size_t len);
|
||||
void sort_pairs(std::pair<int, int>* pairs, size_t len);
|
||||
|
||||
template <typename T>
|
||||
std::pair<int, int>* create_pairs(const T& array, size_t len)
|
||||
{
|
||||
std::pair<int, int>* pairs = new std::pair<int, int>[len];
|
||||
|
||||
for (size_t i = 0; i != len; i++)
|
||||
{
|
||||
if (i % 2)
|
||||
pairs[i].second = array[i];
|
||||
else
|
||||
pairs[i].first = array[i];
|
||||
}
|
||||
if (len > 0 && len % 2)
|
||||
pairs[len - 1].second = pairs[len - 1].first;
|
||||
return pairs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void binary_search(std::pair<int, int>* pairs, size_t len, T& array)
|
||||
{
|
||||
(void) pairs;
|
||||
(void) array;
|
||||
std::vector<int> bozo;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void PmergeMe(T& array)
|
||||
{
|
||||
std::size_t len = array.size() / 2 + array.size() % 2;
|
||||
std::pair<int, int>* pairs = create_pairs(array, len);
|
||||
sort_pair(pairs, len);
|
||||
sort_pairs(pairs, len);
|
||||
std::cout << pairs;
|
||||
array.clear();
|
||||
binary_search(pairs, len, array);
|
||||
}
|
17
ex02/src/main.cpp
Normal file
17
ex02/src/main.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include "PmergeMe.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
std::vector<int> input(ac);
|
||||
std::cout << "Before: ";
|
||||
for (int i = 1; i != ac; i++)
|
||||
{
|
||||
input.push_back(atoi(av[i]));
|
||||
std::cout << av[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
PmergeMe(input);
|
||||
}
|
Loading…
Reference in New Issue
Block a user