a fine project !
This commit is contained in:
parent
d941a20c30
commit
a679064e4f
5 changed files with 141 additions and 123 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/02/09 15:50:10 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/02/15 17:42:03 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,40 +20,18 @@
|
|||
#include "me/vec/vec_i64_bool.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if BONUS == 1
|
||||
// # define ERR_INVALID_NUM "KO1\n"
|
||||
// # define ERR_DUPLICATE "KO2\n"
|
||||
# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n"
|
||||
# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n"
|
||||
|
||||
t_usize print_error(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_usize ret;
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = me_veprintf(fmt, &args);
|
||||
va_end(args);
|
||||
return (ret);
|
||||
}
|
||||
# define ERR_INVALID_NUM "Error\n"
|
||||
# define ERR_DUPLICATE "Error\n"
|
||||
|
||||
#else
|
||||
// # define ERR_INVALID_NUM "Error:\nInvalid Number\n"
|
||||
// # define ERR_DUPLICATE "Error:\nDuplicate Number\n"
|
||||
|
||||
# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n"
|
||||
# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n"
|
||||
|
||||
t_usize print_error(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_usize ret;
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = me_veprintf(fmt, &args);
|
||||
va_end(args);
|
||||
return (ret);
|
||||
}
|
||||
//# define ERR_INVALID_NUM "Error:\nInvalid Number\n"
|
||||
//# define ERR_DUPLICATE "Error:\nDuplicate Number\n"
|
||||
# define ERR_INVALID_NUM "Error\n"
|
||||
# define ERR_DUPLICATE "Error\n"
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -63,61 +41,6 @@ void sort_5(t_state *state);
|
|||
bool is_sorted(t_vec_i64 *v);
|
||||
void run_with_items(t_state *state);
|
||||
|
||||
bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs)
|
||||
{
|
||||
return (lhs->value <= rhs->value);
|
||||
}
|
||||
|
||||
void free_state(t_state state)
|
||||
{
|
||||
vec_i64_free(state.stack_a);
|
||||
vec_i64_free(state.stack_b);
|
||||
vec_i64_bool_free(state.sorted);
|
||||
}
|
||||
|
||||
bool duplicate_check(t_state *state)
|
||||
{
|
||||
t_i64 last;
|
||||
t_usize index;
|
||||
|
||||
if (state->sorted.len == 0)
|
||||
return (false);
|
||||
index = 1;
|
||||
last = state->sorted.buffer[0].value;
|
||||
while (index < state->sorted.len)
|
||||
{
|
||||
if (last == state->sorted.buffer[index].value)
|
||||
return (true);
|
||||
last = state->sorted.buffer[index++].value;
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
t_state parses_arguments(t_usize count, t_str nums[])
|
||||
{
|
||||
t_state state;
|
||||
t_i32 atoi;
|
||||
t_usize i;
|
||||
|
||||
state.stack_a = vec_i64_new(count, NULL);
|
||||
state.stack_b = vec_i64_new(count, NULL);
|
||||
state.sorted = vec_i64_bool_new(count, NULL);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
if (str_to_i32(nums[i], 10, &atoi))
|
||||
(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});
|
||||
i++;
|
||||
}
|
||||
vec_i64_bool_sort(&state.sorted, sort_i64_bool);
|
||||
if (duplicate_check(&state))
|
||||
(free_state(state), me_eprintf(ERR_DUPLICATE), exit(1));
|
||||
return (state);
|
||||
}
|
||||
|
||||
int main_normal(t_i32 argc, t_str argv[])
|
||||
{
|
||||
t_state state;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/02/10 18:55:20 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,8 +17,6 @@
|
|||
#include "me/string/str_n_compare.h"
|
||||
#include "me/vec/vec_i64.h"
|
||||
|
||||
#undef BONUS
|
||||
#define BONUS 1
|
||||
#if BONUS
|
||||
|
||||
void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag,
|
||||
|
|
@ -28,6 +26,16 @@ 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);
|
||||
bool is_sorted(t_vec_i64 *v);
|
||||
|
||||
bool does_match_strings(t_const_str in, t_const_str s1, t_const_str s2)
|
||||
{
|
||||
bool res;
|
||||
|
||||
res = (str_n_compare(in, s1, 4) == 0);
|
||||
if (s2 != NULL)
|
||||
res = (res || (str_n_compare(in, s2, 4) == 0));
|
||||
return (res);
|
||||
}
|
||||
|
||||
t_error handle_operation(t_buffer_str s, t_state *state)
|
||||
{
|
||||
t_usize op;
|
||||
|
|
@ -35,67 +43,62 @@ t_error handle_operation(t_buffer_str s, t_state *state)
|
|||
op = 0;
|
||||
if (s.buf == NULL)
|
||||
return (ERROR);
|
||||
if (str_n_compare(s.buf, "pa", 2) == 0)
|
||||
if (does_match_strings(s.buf, "pa", NULL))
|
||||
push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "pb", 2) == 0)
|
||||
if (does_match_strings(s.buf, "pb", NULL))
|
||||
push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "sa", 2) == 0 || str_n_compare(s.buf, "ss",
|
||||
2) == 0)
|
||||
if (does_match_strings(s.buf, "sa", "ss"))
|
||||
swap_inner(&state->stack_a, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "sb", 2) == 0 || str_n_compare(s.buf, "ss",
|
||||
2) == 0)
|
||||
if (does_match_strings(s.buf, "sb", "ss"))
|
||||
swap_inner(&state->stack_b, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "ra", 2) == 0 || str_n_compare(s.buf, "rr",
|
||||
2) == 0)
|
||||
if (does_match_strings(s.buf, "ra", "rr"))
|
||||
rotate_inner(&state->stack_a, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "rb", 2) == 0 || str_n_compare(s.buf, "rr",
|
||||
2) == 0)
|
||||
if (does_match_strings(s.buf, "rb", "rr"))
|
||||
rotate_inner(&state->stack_b, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "rra", 3) == 0 || str_n_compare(s.buf, "rrr",
|
||||
3) == 0)
|
||||
if (does_match_strings(s.buf, "rra", "rrr"))
|
||||
rev_rotate_inner(&state->stack_a, NULL, (op++, NULL));
|
||||
if (str_n_compare(s.buf, "rrb", 3) == 0 || str_n_compare(s.buf, "rrr",
|
||||
3) == 0)
|
||||
if (does_match_strings(s.buf, "rrb", "rrr"))
|
||||
rev_rotate_inner(&state->stack_b, NULL, (op++, NULL));
|
||||
if (op == 0)
|
||||
return (ERROR);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
void handle_end(t_state state, t_buffer_str s)
|
||||
{
|
||||
str_free(s);
|
||||
if (is_sorted(&state.stack_a) && state.stack_b.len == 0)
|
||||
me_printf("OK\n");
|
||||
else
|
||||
me_printf("KO\n");
|
||||
free_state(state);
|
||||
}
|
||||
|
||||
int main_checker(t_i32 argc, t_str argv[])
|
||||
{
|
||||
t_state state;
|
||||
t_buffer_str s;
|
||||
bool err;
|
||||
t_usize count;
|
||||
|
||||
(void)(argc--, argv++);
|
||||
s.buf = "";
|
||||
err = false;
|
||||
state = parses_arguments(argc, argv);
|
||||
s = get_next_line(0, &err);
|
||||
count = 1;
|
||||
while (!err && s.buf != NULL)
|
||||
while (!err)
|
||||
{
|
||||
s = get_next_line(0, &err);
|
||||
if (err)
|
||||
break ;
|
||||
if (s.len != 0 && s.buf[s.len - 1] == '\n')
|
||||
str_pop(&s);
|
||||
// me_printf("line[%u]: '%s'\n", count, s.buf);
|
||||
if (s.buf[0] == '\0')
|
||||
continue ;
|
||||
if (handle_operation(s, &state))
|
||||
{
|
||||
me_printf("KO: error\n");
|
||||
me_printf("ERROR[%u] = '%s'\n", count, s.buf);
|
||||
(str_free(s), free_state(state));
|
||||
return (1);
|
||||
}
|
||||
str_free(s);
|
||||
s = get_next_line(0, &err);
|
||||
count++;
|
||||
return (me_printf("KO\n"), str_free(s), free_state(state), 1);
|
||||
if (s.buf != NULL)
|
||||
str_free(s);
|
||||
}
|
||||
str_free(s);
|
||||
if (is_sorted(&state.stack_a) && state.stack_b.len == 0)
|
||||
me_printf("OK\n");
|
||||
else
|
||||
me_printf("KO: Not sorted\n");
|
||||
free_state(state);
|
||||
handle_end(state, s);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
|
|||
91
src/app/state.c
Normal file
91
src/app/state.c
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* state.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/02/15 17:42:13 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/state.h"
|
||||
#include "app/types/type_i64_bool.h"
|
||||
#include "me/convert/str_to_numbers.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_n_compare.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_i64.h"
|
||||
#include "me/vec/vec_i64_bool.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if BONUS == 1
|
||||
|
||||
# define ERR_INVALID_NUM "Error\n"
|
||||
# define ERR_DUPLICATE "Error\n"
|
||||
|
||||
#else
|
||||
|
||||
//# define ERR_INVALID_NUM "Error:\nInvalid Number\n"
|
||||
//# define ERR_DUPLICATE "Error:\nDuplicate Number\n"
|
||||
# define ERR_INVALID_NUM "Error\n"
|
||||
# define ERR_DUPLICATE "Error\n"
|
||||
|
||||
#endif
|
||||
|
||||
bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs)
|
||||
{
|
||||
return (lhs->value <= rhs->value);
|
||||
}
|
||||
|
||||
void free_state(t_state state)
|
||||
{
|
||||
vec_i64_free(state.stack_a);
|
||||
vec_i64_free(state.stack_b);
|
||||
vec_i64_bool_free(state.sorted);
|
||||
}
|
||||
|
||||
bool duplicate_check(t_state *state)
|
||||
{
|
||||
t_i64 last;
|
||||
t_usize index;
|
||||
|
||||
if (state->sorted.len == 0)
|
||||
return (false);
|
||||
index = 1;
|
||||
last = state->sorted.buffer[0].value;
|
||||
while (index < state->sorted.len)
|
||||
{
|
||||
if (last == state->sorted.buffer[index].value)
|
||||
return (true);
|
||||
last = state->sorted.buffer[index++].value;
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
t_state parses_arguments(t_usize count, t_str nums[])
|
||||
{
|
||||
t_state state;
|
||||
t_i32 atoi;
|
||||
t_usize i;
|
||||
|
||||
state.stack_a = vec_i64_new(count, NULL);
|
||||
state.stack_b = vec_i64_new(count, NULL);
|
||||
state.sorted = vec_i64_bool_new(count, NULL);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
if (str_to_i32(nums[i], 10, &atoi))
|
||||
(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});
|
||||
i++;
|
||||
}
|
||||
vec_i64_bool_sort(&state.sorted, sort_i64_bool);
|
||||
if (duplicate_check(&state))
|
||||
(free_state(state), me_eprintf(ERR_DUPLICATE), exit(1));
|
||||
return (state);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue