Project made from zero
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 15:51:20 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/21 15:51:47 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:09:42 by aortigos #+# #+# */
|
||||
/* 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::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;
|
||||
|
||||
@@ -5,40 +5,39 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 15:44:59 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/21 15:48:28 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:10:38 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/23 14:13:22 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SWAP_HPP
|
||||
#ifndef WHATEVER_HPP
|
||||
|
||||
# define SWAP_HPP
|
||||
# define WHATEVER_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
template <typename T>
|
||||
void swap(T &a, T &b)
|
||||
void swap(T &x, T &y)
|
||||
{
|
||||
T tmp = a;
|
||||
T tmp = x;
|
||||
|
||||
a = b;
|
||||
b = tmp;
|
||||
x = y;
|
||||
y = tmp;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T min(T const &a, T const &b)
|
||||
T min(T &x, T &y)
|
||||
{
|
||||
if (a < b)
|
||||
return (a);
|
||||
return (b);
|
||||
if (x < y)
|
||||
return (x);
|
||||
return (y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T max(T const &a, T const &b)
|
||||
T max(T &x, T &y)
|
||||
{
|
||||
if (a > b)
|
||||
return (a);
|
||||
return (b);
|
||||
if (x > y)
|
||||
return (x);
|
||||
return (y);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -5,8 +5,8 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 15:53:06 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/21 16:58:31 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:19:16 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/23 14:25:35 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,18 +15,22 @@
|
||||
# define ITER_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <cctype>
|
||||
|
||||
template <typename PTR, typename F>
|
||||
void iter(PTR *address, size_t const lenght, F func)
|
||||
template <typename PTR, typename T>
|
||||
void iter(PTR *address, unsigned int const length, T func)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < lenght)
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
func(address[i]);
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,60 +5,45 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 16:57:02 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/21 16:57:36 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:19:00 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/23 14:27:08 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "iter.hpp"
|
||||
|
||||
void toUpper(char &c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
c = c - 32;
|
||||
}
|
||||
|
||||
void increment(int &n)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void printElement(const T& elem)
|
||||
void print(T const &x)
|
||||
{
|
||||
std::cout << elem << " ";
|
||||
}
|
||||
|
||||
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++;
|
||||
std::cout << x << " ";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// -------- STRING ARRAY --------
|
||||
std::string words[] = {"hola", "mundo", "iter"};
|
||||
size_t wordsLen = sizeof(words) / sizeof(words[0]);
|
||||
char letters[] = {'h', 'o', 'l', 'a', 'm', 'u', 'n', 'd', 'o'};
|
||||
int numbers[] = {1, 2, 3, 4, 5};
|
||||
|
||||
std::cout << "Original strings: ";
|
||||
iter(words, wordsLen, printElement<std::string>);
|
||||
std::cout << "Before: ";
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -5,13 +5,12 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 17:04:01 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/21 17:04:01 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ARRAY_HPP
|
||||
|
||||
# define ARRAY_HPP
|
||||
|
||||
# include <iostream>
|
||||
@@ -23,20 +22,19 @@ class Array
|
||||
{
|
||||
private:
|
||||
T *data;
|
||||
unsigned int _size;
|
||||
unsigned int length;
|
||||
|
||||
public:
|
||||
Array();
|
||||
Array(unsigned int n);
|
||||
Array(const Array &other);
|
||||
Array &operator=(const Array &other);
|
||||
Array& operator=(const Array &other);
|
||||
~Array();
|
||||
|
||||
T &operator[](unsigned int i);
|
||||
T const &operator[](unsigned int i) const;
|
||||
Array(unsigned int n);
|
||||
T &operator[](unsigned int n);
|
||||
unsigned int size() const;
|
||||
};
|
||||
|
||||
#include "Array.tpp"
|
||||
# include "Array.tpp"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,31 +3,33 @@
|
||||
/* ::: :::::::: */
|
||||
/* Array.tpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/22 21:42:21 by aortigos ### ########.fr */
|
||||
/* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
//////////////////
|
||||
// Constructors //
|
||||
//////////////////
|
||||
|
||||
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>
|
||||
Array<T>::Array(unsigned int n) : _size(n)
|
||||
Array<T>::Array(const Array &other)
|
||||
{
|
||||
data = new T[n];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Array<T>::Array(const Array &other) : _size(other._size)
|
||||
{
|
||||
data = new T[other._size];
|
||||
for (unsigned int i = 0;i < _size; i++)
|
||||
data = new T[other.length]();
|
||||
length = other.length;
|
||||
for(unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
data[i] = other.data[i];
|
||||
}
|
||||
// std::cout << "Array copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -35,12 +37,16 @@ Array<T> &Array<T>::operator=(const Array &other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// Copy attributes here
|
||||
delete[] data;
|
||||
_size = other._size;
|
||||
data = new T[_size];
|
||||
for (unsigned int i = 0;i < _size; i++)
|
||||
data = new T[other.length]();
|
||||
length = other.length;
|
||||
for(unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
data[i] = other.data[i];
|
||||
}
|
||||
}
|
||||
// std::cout << "Array copy assignment operator called" << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
@@ -48,26 +54,25 @@ template <typename T>
|
||||
Array<T>::~Array()
|
||||
{
|
||||
delete[] data;
|
||||
// std::cout << "Array destructor called" << std::endl;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Array<T>::operator[](unsigned int i)
|
||||
Array<T>::Array(unsigned int n) : length(n)
|
||||
{
|
||||
if (i >= _size)
|
||||
throw std::exception();
|
||||
return (data[i]);
|
||||
data = new T[n]();
|
||||
}
|
||||
|
||||
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();
|
||||
return (data[i]);
|
||||
return (data[n]);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
unsigned int Array<T>::size() const
|
||||
{
|
||||
return (_size);
|
||||
return (this->length);
|
||||
}
|
||||
@@ -1,15 +1,4 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 <iostream>
|
||||
#include "Array/Array.hpp"
|
||||
|
||||
#define MAX_VAL 750
|
||||
|
||||
Reference in New Issue
Block a user