Skip to content

Commit 03a00b6

Browse files
committed
ex01 working
- more testing needed - more functions in main needed
1 parent ce67ad5 commit 03a00b6

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

ex01/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: tblaase <[email protected]> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2022/04/13 14:43:52 by tblaase #+# #+# #
9-
# Updated: 2022/04/13 15:40:07 by tblaase ### ########.fr #
9+
# Updated: 2022/04/13 15:58:26 by tblaase ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -32,7 +32,7 @@ UP = \033[A
3232
CUT = \033[K
3333

3434
#source files
35-
SRC_FILES =
35+
SRC_FILES = main.cpp
3636

3737
OBJ_FILES = $(SRC_FILES:.cpp=.o)
3838

ex01/include/iter.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* iter.hpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: tblaase <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/13 15:50:00 by tblaase #+# #+# */
9+
/* Updated: 2022/04/13 17:22:11 by tblaase ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#pragma once
14+
15+
#include <string>
16+
17+
template< typename T >
18+
19+
/**
20+
* @brief iterates over your array and applies the function on each element until arr_len is hit
21+
* @note the function has to be a void function that takes a `T &`, otherwise it wont compile
22+
* @param *array: the input array with type of your choice
23+
* @param arr_len: the length of your array
24+
* @param (*foo: the function you want to have used on every element of your array
25+
* @retval None
26+
*/
27+
void iter(T *array, size_t arr_len, void (*foo)(T &))
28+
{
29+
if (array == NULL || foo == NULL)
30+
return ;
31+
for (size_t i = 0; i < arr_len; i++)
32+
foo(array[i]);
33+
}

ex01/src/main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* main.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: tblaase <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/04/13 15:50:03 by tblaase #+# #+# */
9+
/* Updated: 2022/04/13 17:26:38 by tblaase ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "iter.hpp"
14+
15+
#include <iostream>
16+
#include <cctype>
17+
18+
void ft_tolower(char &arr)
19+
{
20+
arr = std::tolower(static_cast<unsigned char>(arr));
21+
}
22+
23+
void ft_toupper(char &arr)
24+
{
25+
arr = std::toupper(static_cast<unsigned char>(arr));
26+
}
27+
28+
int main()
29+
{
30+
char a[] = {'A', 'B', 'C'};
31+
32+
std::cout << "Original:" <<
33+
"\n\ta[0]: " << a[0] <<
34+
"\n\ta[1]: " << a[1] <<
35+
"\n\ta[2]: " << a[2] <<
36+
std::endl << std::endl;
37+
38+
::iter(a, sizeof(a) / sizeof(char), ft_tolower);
39+
40+
std::cout << "Changed:" <<
41+
"\n\ta[0]: " << a[0] <<
42+
"\n\ta[1]: " << a[1] <<
43+
"\n\ta[2]: " << a[2] <<
44+
std::endl << std::endl;
45+
46+
::iter(a, sizeof(a) / sizeof(char), ft_toupper);
47+
48+
std::cout << "Changed:" <<
49+
"\n\ta[0]: " << a[0] <<
50+
"\n\ta[1]: " << a[1] <<
51+
"\n\ta[2]: " << a[2] <<
52+
std::endl << std::endl;
53+
}

0 commit comments

Comments
 (0)