Skip to content

Commit

Permalink
finished cpp07
Browse files Browse the repository at this point in the history
  • Loading branch information
fcil committed Sep 13, 2022
1 parent 1759ab5 commit 47f6820
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 0 deletions.
Binary file removed CPP/cpp-module-07/en.subject.pdf
Binary file not shown.
38 changes: 38 additions & 0 deletions CPP/cpp-module-07/ex00/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fcil <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/03 11:41:37 by fcil #+# #+# #
# Updated: 2022/09/13 00:18:34 by fcil ### ########.fr #
# #
# **************************************************************************** #

NAME = a.out
CC = c++ -std=c++98
FLAGS = -Wall -Wextra -Werror

SRC_DIR = ./
SRC = $(shell find $(SRC_DIR) -type f -name "*.cpp")

OBJ = $(SRC:.cpp=.o)

$(NAME): $(OBJ)
$(CC) $(FLAGS) -o $(NAME) $(OBJ)

%.o : %.cpp
$(CC) $(FLAGS) -o $@ -c $<

all: $(NAME)

clean:
rm -f $(OBJ)

fclean: clean
rm -f $(NAME)

re: fclean all

.PHONY: all clean fclean re
34 changes: 34 additions & 0 deletions CPP/cpp-module-07/ex00/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 12:13:53 by fcil #+# #+# */
/* Updated: 2022/09/13 13:36:50 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "whatever.hpp"

int main(void) {

int a = 2;
int b = 3;

::swap( a, b );
std::cout << "a = " << a << ", b = " << b << std::endl;
std::cout << "min(a, b) = " << ::min(a, b) << std::endl;
std::cout << "max(a, b) = " << ::max(a, b) << std::endl;

std::string c = "chaine1";
std::string d = "chaine2";

::swap(c, d);
std::cout << "c = " << c << ", d = " << d << std::endl;
std::cout << "min(c, d) = " << ::min(c, d) << std::endl;
std::cout << "max(c, d) = " << ::max(c, d) << std::endl;

return 0;
}
36 changes: 36 additions & 0 deletions CPP/cpp-module-07/ex00/whatever.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* whatever.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 12:14:15 by fcil #+# #+# */
/* Updated: 2022/09/13 13:32:11 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include "iostream"

template<class Type>
void swap(Type &a, Type &b)
{
Type temp;
temp = a;
a = b;
b = temp;
}

template<class Type>
Type min(Type &a, Type &b)
{
return (a < b ? a : b);
}

template<class Type>
Type max(Type &a, Type &b)
{
return (a > b ? a : b);
}
38 changes: 38 additions & 0 deletions CPP/cpp-module-07/ex01/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fcil <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/03 11:41:37 by fcil #+# #+# #
# Updated: 2022/09/13 00:18:34 by fcil ### ########.fr #
# #
# **************************************************************************** #

NAME = a.out
CC = c++ -std=c++98
FLAGS = -Wall -Wextra -Werror

SRC_DIR = ./
SRC = $(shell find $(SRC_DIR) -type f -name "*.cpp")

OBJ = $(SRC:.cpp=.o)

$(NAME): $(OBJ)
$(CC) $(FLAGS) -o $(NAME) $(OBJ)

%.o : %.cpp
$(CC) $(FLAGS) -o $@ -c $<

all: $(NAME)

clean:
rm -f $(OBJ)

fclean: clean
rm -f $(NAME)

re: fclean all

.PHONY: all clean fclean re
22 changes: 22 additions & 0 deletions CPP/cpp-module-07/ex01/iter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iter.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 13:37:46 by fcil #+# #+# */
/* Updated: 2022/09/13 13:38:43 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include "iostream"

template<typename Type>
void iter(Type *array, size_t len, void(*f)(Type const &))
{
for (size_t i = 0; i < len; i++)
f(array[i]);
}
32 changes: 32 additions & 0 deletions CPP/cpp-module-07/ex01/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 13:37:01 by fcil #+# #+# */
/* Updated: 2022/09/13 13:39:45 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "iter.hpp"

void print(const int &n)
{
std::cout << n << " ";
}

void print2(const std::string &s)
{
std::cout << s + " ";
}

int main()
{
int array[5] = {0, 1, 9, 0, 7};
iter(array, 5, print);
std::cout << std::endl;
std::string arr[2] = {"test1", "test2"};
iter(arr, 2, print2);
}
91 changes: 91 additions & 0 deletions CPP/cpp-module-07/ex02/Array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 13:42:33 by fcil #+# #+# */
/* Updated: 2022/09/13 14:09:06 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once
#include <iostream>
#include <cstdlib>



template <typename T>
class Array
{
private:
unsigned int _len;
T *_array;

public:
Array<T>()
{
_len = 0;
_array = new T[_len];
}

Array<T>(unsigned int len)
{
_len = len;
_array = new T[_len];
for (unsigned int i = 0; i < len; i++)
{
_array[i] = 0;
}
}

Array<T>(Array &arr)
{
_len = arr.size();
_array = new T[_len];
*this = arr;
for (unsigned int i = 0; i < _len; i++)
{
_array[i] = arr[i];
}
}

~Array<T>()
{
if (this->_array)
delete [] _array;
}
Array<T> &operator=(const Array &arr)
{
delete[] _array;
_array = new T[arr.size()];
_len = arr.size();
for (unsigned int i = 0; i < _len; i++)
{
_array[i] = arr[i];
}
return (*this);
}

T & operator[](unsigned int i) const
{
if (i >= this->_len)
throw (Array::outOfLimits());
return (this->_array[i]);
}

unsigned int size() const
{
return this->_len;
}

class outOfLimits: public std::exception
{
public:
virtual const char* what() const throw()
{
return "Index out of limits";
}
};
};
38 changes: 38 additions & 0 deletions CPP/cpp-module-07/ex02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fcil <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/03 11:41:37 by fcil #+# #+# #
# Updated: 2022/09/13 00:18:34 by fcil ### ########.fr #
# #
# **************************************************************************** #

NAME = a.out
CC = c++ -std=c++98
FLAGS = -Wall -Wextra -Werror

SRC_DIR = ./
SRC = $(shell find $(SRC_DIR) -type f -name "*.cpp")

OBJ = $(SRC:.cpp=.o)

$(NAME): $(OBJ)
$(CC) $(FLAGS) -o $(NAME) $(OBJ)

%.o : %.cpp
$(CC) $(FLAGS) -o $@ -c $<

all: $(NAME)

clean:
rm -f $(OBJ)

fclean: clean
rm -f $(NAME)

re: fclean all

.PHONY: all clean fclean re
Loading

0 comments on commit 47f6820

Please sign in to comment.