Skip to content

Commit

Permalink
Merge pull request #23 from mendes-jv/detached2
Browse files Browse the repository at this point in the history
Add integer sorting and swapping functions, refactor operation functions
  • Loading branch information
mendes-jv committed Feb 4, 2024
2 parents 11d599a + 5af72cc commit 69fd0e6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ SRC = $(addprefix $(PATH_SRC), ft_isalpha.c ft_isdigit.c ft_isalnum.c \
ft_itoa_hex.c ft_check_string.c ft_printf.c ft_manage_params.c \
ft_write_params.c ft_apply_flags.c ft_for_each.c \
ft_arr_for_each.c ft_arr_split.c ft_arr_len.c ft_ternary.c \
ft_handle_error.c ft_putstr_color_fd.c ft_atol.c ft_operate.c)
ft_handle_error.c ft_putstr_color_fd.c ft_atol.c ft_operate.c \
ft_sort_int_arr.c ft_swap.c)

HEADER = includes/

AR = ar -rcs

FLAGS = -Wall -Wextra -Wunreachable-code -Ofast -g3 -O3
FLAGS = -Wall -Wextra -Wunreachable-code -g3

OBJS = $(patsubst $(PATH_SRC)%.c, $(PATH_OBJ)%.o, $(SRC))

Expand Down
4 changes: 3 additions & 1 deletion includes/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ void ft_putstr_color_fd(char *color, char *s, int fd);

//Push Swap
long ft_atol(const char *nptr);
long long ft_operate(long long nbr1, long long nbr2, char operate);
int ft_operate(int nbr1, int nbr2, char operate);
void ft_sort_int_arr(int *arr, size_t size);
void ft_swap(int *nbr1, int *nbr2);

# ifndef MIN
# define MIN 0b0
Expand Down
2 changes: 1 addition & 1 deletion sources/ft_arr_for_each.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "../includes/libft.h"

void ft_array_for_each(void **array, void (*array_f)(void *),
void ft_arr_for_each(void **array, void (*array_f)(void *),
void (*index_f)(void *))
{
while (*array)
Expand Down
14 changes: 7 additions & 7 deletions sources/ft_operate.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

#include "../includes/libft.h"

long long ft_operate(long long a, long long b, char operator)
int ft_operate(int nbr1, int nbr2, char operator)
{
if (operator == MIN)
{
if (a <= b)
return (a);
return (b);
if (nbr1 <= nbr2)
return (nbr1);
return (nbr2);
}
if (a >= b)
return (a);
return (b);
if (nbr1 >= nbr2)
return (nbr1);
return (nbr2);
}
32 changes: 32 additions & 0 deletions sources/ft_sort_int_arr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_int_arr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <jovicto2@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 11:00:40 by jovicto2 #+# #+# */
/* Updated: 2023/12/11 11:00:41 by jovicto2 ### ########.org.br */
/* */
/* ************************************************************************** */

#include "../includes/libft.h"

void ft_sort_int_arr(int *arr, size_t size)
{
size_t first_index;
size_t second_index;

first_index = 0;
while (first_index < size)
{
second_index = first_index + 1;
while (second_index < size)
{
if (arr[first_index] > arr[second_index])
ft_swap((arr + first_index), (arr + second_index));
second_index++;
}
first_index++;
}
}
20 changes: 20 additions & 0 deletions sources/ft_swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <jovicto2@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 11:00:40 by jovicto2 #+# #+# */
/* Updated: 2023/12/11 11:00:41 by jovicto2 ### ########.org.br */
/* */
/* ************************************************************************** */

#include "../includes/libft.h"

void ft_swap(int *nbr1, int *nbr2)
{
*nbr1 ^= *nbr2;
*nbr2 ^= *nbr1;
*nbr1 ^= *nbr2;
}

0 comments on commit 69fd0e6

Please sign in to comment.