Project made from zero

This commit is contained in:
aortigos
2026-03-23 14:45:44 +01:00
parent 582f5ad276
commit d05d0063a7
7 changed files with 122 additions and 141 deletions

View File

@@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 15:51:20 by aortigos #+# #+# */ /* Created: 2026/03/23 14:09:42 by aortigos #+# #+# */
/* Updated: 2026/03/21 15:51:47 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:18:16 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -22,6 +22,7 @@ int main( void ) {
std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl; std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl;
std::string c = "chaine1"; std::string c = "chaine1";
std::string d = "chaine2"; std::string d = "chaine2";
::swap(c, d); ::swap(c, d);
std::cout << "c = " << c << ", d = " << d << std::endl; std::cout << "c = " << c << ", d = " << d << std::endl;
std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl; std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl;

View File

@@ -5,40 +5,39 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 15:44:59 by aortigos #+# #+# */ /* Created: 2026/03/23 14:10:38 by aortigos #+# #+# */
/* Updated: 2026/03/21 15:48:28 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:13:22 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef SWAP_HPP #ifndef WHATEVER_HPP
# define SWAP_HPP # define WHATEVER_HPP
# include <iostream> # include <iostream>
template <typename T> template <typename T>
void swap(T &a, T &b) void swap(T &x, T &y)
{ {
T tmp = a; T tmp = x;
a = b; x = y;
b = tmp; y = tmp;
} }
template <typename T> template <typename T>
T min(T const &a, T const &b) T min(T &x, T &y)
{ {
if (a < b) if (x < y)
return (a); return (x);
return (b); return (y);
} }
template <typename T> template <typename T>
T max(T const &a, T const &b) T max(T &x, T &y)
{ {
if (a > b) if (x > y)
return (a); return (x);
return (b); return (y);
} }
#endif #endif

View File

@@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 15:53:06 by aortigos #+# #+# */ /* Created: 2026/03/23 14:19:16 by aortigos #+# #+# */
/* Updated: 2026/03/21 16:58:31 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:25:35 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,18 +15,22 @@
# define ITER_HPP # define ITER_HPP
# include <iostream> # include <iostream>
# include <cctype>
template <typename PTR, typename F> template <typename PTR, typename T>
void iter(PTR *address, size_t const lenght, F func) void iter(PTR *address, unsigned int const length, T func)
{ {
size_t i; for (unsigned int i = 0; i < length; i++)
{
i = 0; func(address[i]);
while (i < lenght) }
}
template <typename PTR, typename T>
void iter(PTR const *address, unsigned int const length, T func)
{
for (unsigned int i = 0; i < length; i++)
{ {
func(address[i]); func(address[i]);
i++;
} }
} }

View File

@@ -5,60 +5,45 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 16:57:02 by aortigos #+# #+# */ /* Created: 2026/03/23 14:19:00 by aortigos #+# #+# */
/* Updated: 2026/03/21 16:57:36 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:27:08 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "iter.hpp" #include "iter.hpp"
void toUpper(char &c)
{
if (c >= 'a' && c <= 'z')
c = c - 32;
}
void increment(int &n)
{
n++;
}
template <typename T> template <typename T>
void printElement(const T& elem) void print(T const &x)
{ {
std::cout << elem << " "; std::cout << x << " ";
}
void toUpper(std::string& str)
{
for (size_t i = 0; i < str.length(); i++)
str[i] = std::toupper(str[i]);
}
void increment(int& x)
{
x++;
} }
int main() int main()
{ {
// -------- STRING ARRAY -------- char letters[] = {'h', 'o', 'l', 'a', 'm', 'u', 'n', 'd', 'o'};
std::string words[] = {"hola", "mundo", "iter"}; int numbers[] = {1, 2, 3, 4, 5};
size_t wordsLen = sizeof(words) / sizeof(words[0]);
std::cout << "Original strings: "; std::cout << "Before: ";
iter(words, wordsLen, printElement<std::string>); iter(letters, 9, print<char>);
iter(letters, 9, toUpper);
std::cout << "\nAfter: ";
iter(letters, 9, print<char>);
std::cout << "\n\nBefore: ";
iter(numbers, 5, print<int>);
iter(numbers, 5, increment);
std::cout << "\nAfter: ";
iter(numbers, 5, print<int>);
std::cout << std::endl; std::cout << std::endl;
iter(words, wordsLen, toUpper);
std::cout << "Uppercase strings: ";
iter(words, wordsLen, printElement<std::string>);
std::cout << std::endl;
// -------- INT ARRAY --------
int numbers[] = {1, 2, 3, 4};
size_t numLen = sizeof(numbers) / sizeof(numbers[0]);
std::cout << "\nOriginal ints: ";
iter(numbers, numLen, printElement<int>);
std::cout << std::endl;
iter(numbers, numLen, increment);
std::cout << "Incremented ints: ";
iter(numbers, numLen, printElement<int>);
std::cout << std::endl;
return 0;
} }

View File

@@ -5,13 +5,12 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:04:01 by aortigos #+# #+# */ /* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:04:01 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ARRAY_HPP #ifndef ARRAY_HPP
# define ARRAY_HPP # define ARRAY_HPP
# include <iostream> # include <iostream>
@@ -19,24 +18,23 @@
# include <ctime> # include <ctime>
template <typename T> template <typename T>
class Array class Array
{ {
private: private:
T *data; T *data;
unsigned int _size; unsigned int length;
public: public:
Array(); Array();
Array(unsigned int n); Array(const Array &other);
Array(const Array &other); Array& operator=(const Array &other);
Array &operator=(const Array &other); ~Array();
~Array();
T &operator[](unsigned int i); Array(unsigned int n);
T const &operator[](unsigned int i) const; T &operator[](unsigned int n);
unsigned int size() const; unsigned int size() const;
}; };
#include "Array.tpp" # include "Array.tpp"
#endif #endif

View File

@@ -1,73 +1,78 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* Array.tpp :+: :+: :+: */ /* Array.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */ /* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
/* Updated: 2026/03/22 21:42:21 by aortigos ### ########.fr */ /* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
//////////////////
// Constructors //
//////////////////
template <typename T> template <typename T>
Array<T>::Array() : data(NULL), _size(0) Array<T>::Array() : data(NULL), length(0)
{ {
// std::cout << "Default constructor called" << std::endl; // std::cout << "Array default constructor called" << std::endl;
} }
template <typename T> template <typename T>
Array<T>::Array(unsigned int n) : _size(n) Array<T>::Array(const Array &other)
{ {
data = new T[n]; data = new T[other.length]();
} length = other.length;
for(unsigned int i = 0; i < length; i++)
template <typename T> {
Array<T>::Array(const Array &other) : _size(other._size) data[i] = other.data[i];
{ }
data = new T[other._size]; // std::cout << "Array copy constructor called" << std::endl;
for (unsigned int i = 0;i < _size; i++)
data[i] = other.data[i];
} }
template <typename T> template <typename T>
Array<T> &Array<T>::operator=(const Array &other) Array<T> &Array<T>::operator=(const Array &other)
{ {
if (this != &other) if (this != &other)
{ {
delete[] data; // Copy attributes here
_size = other._size; delete[] data;
data = new T[_size]; data = new T[other.length]();
for (unsigned int i = 0;i < _size; i++) length = other.length;
data[i] = other.data[i]; for(unsigned int i = 0; i < length; i++)
} {
return (*this); data[i] = other.data[i];
}
}
// std::cout << "Array copy assignment operator called" << std::endl;
return (*this);
} }
template <typename T> template <typename T>
Array<T>::~Array() Array<T>::~Array()
{ {
delete[] data; delete[] data;
// std::cout << "Array destructor called" << std::endl;
} }
template <typename T> template <typename T>
T &Array<T>::operator[](unsigned int i) Array<T>::Array(unsigned int n) : length(n)
{ {
if (i >= _size) data = new T[n]();
throw std::exception();
return (data[i]);
} }
template <typename T> template <typename T>
T const &Array<T>::operator[](unsigned int i) const T &Array<T>::operator[](unsigned int n)
{ {
if (i >= _size) if (n >= length)
throw std::exception(); throw std::exception();
return (data[i]); return (data[n]);
} }
template <typename T> template <typename T>
unsigned int Array<T>::size() const unsigned int Array<T>::size() const
{ {
return (_size); return (this->length);
} }

View File

@@ -1,15 +1,4 @@
/* ************************************************************************** */ #include <iostream>
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:00:50 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:00:54 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Array/Array.hpp" #include "Array/Array.hpp"
#define MAX_VAL 750 #define MAX_VAL 750