update stuff

This commit is contained in:
Maieul BOYER 2024-02-08 22:05:19 +01:00
parent f980794456
commit ee1a1f272b
No known key found for this signature in database
8 changed files with 106 additions and 28 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */
/* Updated: 2024/01/31 15:09:44 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:05:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,10 @@
# include "me/vec/vec_i64.h"
# include "me/vec/vec_i64_bool.h"
#ifndef BONUS
#define BONUS 0
#endif
typedef struct s_state
{
t_vec_i64_bool sorted;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */
/* Updated: 2024/02/08 18:53:49 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:05:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,14 @@
#include "me/vec/vec_i64_bool.h"
#include <stdlib.h>
#if BONUS == 1
# define ERR_INVALID_NUM "KO\n"
# define ERR_DUPLICATE "KO\n"
#else
# define ERR_INVALID_NUM "Error:\nInvalid Number\n"
# define ERR_DUPLICATE "Error:\nDuplicate Number\n"
#endif
void sort_3(t_state *state);
void sort_2(t_state *state);
void sort_5(t_state *state);
@ -68,8 +76,7 @@ t_state parses_arguments(t_usize count, t_str nums[])
while (i < count)
{
if (str_to_i32(nums[i], 10, &atoi))
(free_state(state), me_eprintf("Error:\nInvalid Number\n"),
exit(1));
(free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1));
vec_i64_push(&state.stack_a, atoi);
vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi,
.active = false});
@ -77,7 +84,7 @@ t_state parses_arguments(t_usize count, t_str nums[])
}
vec_i64_bool_sort(&state.sorted, sort_i64_bool);
if (duplicate_check(&state))
(free_state(state), me_eprintf("Error:\nDuplicate Number\n"), exit(1));
(free_state(state), me_eprintf(ERR_DUPLICATE), exit(1));
return (state);
}
@ -92,8 +99,8 @@ int main(t_i32 argc, t_str argv[])
if (is_sorted(&state.stack_a))
return (free_state(state), 0);
if (state.stack_a.len == 0)
(free_state(state), me_eprintf("Error:\nNo Input\n"), exit(1));
if (state.stack_a.len == 2)
(free_state(state), exit(0));
else if (state.stack_a.len == 2)
sort_2(&state);
else if (state.stack_a.len == 3)
sort_3(&state);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */
/* Updated: 2024/02/02 21:40:17 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:15:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
#include "me/printf/printf.h"
#include "me/vec/vec_i64.h"
static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag,
void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag,
t_const_str print)
{
t_i64 e;

View file

@ -6,14 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */
/* Updated: 2024/02/02 14:12:23 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:16:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "me/printf/printf.h"
static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag,
void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
{
t_i64 e;

View file

@ -6,38 +6,37 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */
/* Updated: 2024/02/08 14:30:21 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:17:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "me/printf/printf.h"
static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print)
{
t_i64 e;
t_i64 e;
(void)(tag);
if (stack->len <= 1)
return ;
return;
vec_i64_pop_front(stack, &e);
vec_i64_push(stack, e);
if (print)
ft_printf("%s\n", print);
}
void rotate_a(t_state *s)
void rotate_a(t_state *s)
{
rotate_inner(&s->stack_a, "Rotate A", "ra");
}
void rotate_b(t_state *s)
void rotate_b(t_state *s)
{
rotate_inner(&s->stack_b, "Rotate B", "rb");
}
void rotate_both(t_state *s)
void rotate_both(t_state *s)
{
rotate_inner(&s->stack_a, "Rotate Both", NULL);
rotate_inner(&s->stack_b, "Rotate Both", "rr");

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */
/* Updated: 2024/02/02 14:14:12 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 19:17:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,15 +14,14 @@
#include "me/printf/printf.h"
#include "me/vec/vec_i64.h"
static inline void swap_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print)
{
t_i64 first;
t_i64 second;
t_i64 first;
t_i64 second;
(void)(tag);
if (stack->len <= 1)
return ;
return;
vec_i64_pop(stack, &first);
vec_i64_pop(stack, &second);
vec_i64_push(stack, first);
@ -31,17 +30,17 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag,
ft_printf("%s\n", print);
}
void swap_a(t_state *s)
void swap_a(t_state *s)
{
swap_inner(&s->stack_a, "Swap A", "sa");
}
void swap_b(t_state *s)
void swap_b(t_state *s)
{
swap_inner(&s->stack_b, "Swap B", "sb");
}
void swap_both(t_state *s)
void swap_both(t_state *s)
{
swap_inner(&s->stack_a, "Swap Both", NULL);
swap_inner(&s->stack_b, "Swap Both", "ss");

56
src/bonus/main.c Normal file
View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */
/* Updated: 2024/02/08 22:05:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "me/buffered_str/buf_str.h"
#include "me/gnl/gnl.h"
#include "me/string/str_n_compare.h"
#undef BONUS
#define BONUS 1
#if BONUS
void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag,
t_const_str print);
void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print);
void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print);
void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print);
void handle_operation(t_buffer_str s, t_state *state)
{
if (str_n_compare(s.buf, "pa", 2))
push_inner(&state->stack_a, &state->stack_b, NULL, NULL);
if (str_n_compare(s.buf, "pb", 2))
push_inner(&state->stack_b, &state->stack_a, NULL, NULL);
if (str_n_compare(s.buf, "sa", 2) )
push_inner(&state->stack_a, &state->stack_b, NULL, NULL);
if (str_n_compare(s.buf, "sb", 2))
push_inner(&state->stack_b, &state->stack_a, NULL, NULL);
}
int main(t_i32 argc, t_str argv[])
{
t_state state;
t_buffer_str s;
bool err;
(void)(argc--, argv++);
state = parses_arguments(argc, argv);
s = get_next_line(0, &err);
while (err)
{
handle_operation(s, &state);
str_free(s);
s = get_next_line(0, &err);
}
}
#endif

13
src/bonus/move1.c Normal file
View file

@ -0,0 +1,13 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 19:15:21 by maiboyer #+# #+# */
/* Updated: 2024/02/08 19:15:21 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */