diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-18 12:11:36 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-18 12:11:36 +0100 |
| commit | 5e0e41652315114a8b0d883c473dbbbfc1c28342 (patch) | |
| tree | 8e1440ccf9d686294c05c6f8b5907fe90ba4fda8 | |
| parent | 2d34349b54a9fed4d34c96bdbec5172f7695cf60 (diff) | |
| download | push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.tar.gz push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.tar.bz2 push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.zip | |
filled action functions (propably bloat), checker working, stack functions not so much
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 6 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | src/checker/check.c | 32 | ||||
| -rw-r--r-- | src/checker/checker.h | 37 | ||||
| -rw-r--r-- | src/checker/main.c | 50 | ||||
| -rw-r--r-- | src/common/action.c | 34 | ||||
| -rw-r--r-- | src/common/common.h | 13 | ||||
| -rw-r--r-- | src/common/stack.c | 1 |
9 files changed, 104 insertions, 70 deletions
@@ -5,3 +5,4 @@ push_swap !src/push_swap *.o *.ghc +*.dSYM @@ -3,7 +3,7 @@ RM = rm -f LIBFT_DIR = libft CC = gcc -CCFLAGS = -I$(COMMON_DIR) -I$(LIBFT_DIR)/include -Wall -Wextra #-Werror +CCFLAGS = -I$(COMMON_DIR) -I$(LIBFT_DIR)/include -Wall -Wextra -g#-Werror LDFLAGS = -L$(LIBFT_DIR) -lft SRC_DIR = src @@ -35,11 +35,11 @@ PUSH_SWAP_OBJ += $(COMMON_OBJ) all: libft_all $(CHECKER_NAME) $(PUSH_SWAP_NAME) $(CHECKER_NAME): CCFLAGS += -I$(CHECKER_DIR) -$(CHECKER_NAME): $(CHECKER_OBJ) $(CHECKER_HEADER) +$(CHECKER_NAME): $(CHECKER_OBJ) $(CHECKER_HEADER) $(LIBFT_DIR)/libft.a $(CC) $(LDFLAGS) -o $@ $(CHECKER_OBJ) $(PUSH_SWAP_NAME): CCFLAGS += -I$(PUSH_SWAP_DIR) -$(PUSH_SWAP_NAME): $(PUSH_SWAP_OBJ) $(PUSH_SWAP_HEADER) +$(PUSH_SWAP_NAME): $(PUSH_SWAP_OBJ) $(PUSH_SWAP_HEADER) $(LIBFT_DIR)/libft.a $(CC) $(LDFLAGS) -o $@ $(PUSH_SWAP_OBJ) %.o: %.c diff --git a/libft b/libft -Subproject ae1412fdc283e442a0869aa7d63778449a7e5cf +Subproject 676b37f963789f3d85b18ed5b3708374971b65e diff --git a/src/checker/check.c b/src/checker/check.c index b1ef4ca..35bd992 100644 --- a/src/checker/check.c +++ b/src/checker/check.c @@ -6,14 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:08 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:52:42 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:04:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "checker.h" -#define ACTION_ID_BUF_SIZE 4 - t_status check(t_stack *a, t_stack *b) { t_status read_status; @@ -46,27 +44,29 @@ t_status read_action(t_stack *a, t_stack *b) return (ret == 0 ? STATUS_EOF : STATUS_ERROR); } -t_action g_actions[] = { +static t_action g_actions[] = { {"sa", FLAG_ARG_A, {.arg_1 = &swap_a}}, {"sb", FLAG_ARG_B, {.arg_1 = &swap_b}}, {"ss", FLAG_ARG_A_B, {.arg_2 = &swap_both}}, - {"pa", FLAG_ARG_A_B, {.arg_2 = push_a}}, - {"pb", FLAG_ARG_B_A, {.arg_2 = push_a}}, - {"ra", FLAG_ARG_A, {.arg_1 = rotate_a}}, - {"rb", FLAG_ARG_B, {.arg_1 = rotate_b}}, - {"rr", FLAG_ARG_A_B, {.arg_2 = rotate_both}}, - {"rra", FLAG_ARG_A, {.arg_1 = reverse_rotate_a}}, - {"rrb", FLAG_ARG_B, {.arg_1 = reverse_rotate_b}}, - {"rrr", FLAG_ARG_A_B, {.arg_2 = reverse_rotate_both}}, + {"pa", FLAG_ARG_A_B, {.arg_2 = &push_a}}, + {"pb", FLAG_ARG_B_A, {.arg_2 = &push_a}}, + {"ra", FLAG_ARG_A, {.arg_1 = &rotate_a}}, + {"rb", FLAG_ARG_B, {.arg_1 = &rotate_b}}, + {"rr", FLAG_ARG_A_B, {.arg_2 = &rotate_both}}, + {"rra", FLAG_ARG_A, {.arg_1 = &reverse_rotate_a}}, + {"rrb", FLAG_ARG_B, {.arg_1 = &reverse_rotate_b}}, + {"rrr", FLAG_ARG_A_B, {.arg_2 = &reverse_rotate_both}}, }; #define ACTIONS_SIZE (sizeof(g_actions) / sizeof(t_action)) -t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], t_stack *a, t_stack *b) +t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], + t_stack *a, t_stack *b) { int i; - - i = -1; + + printf("action id %s\n", action_id); + i = 0; while (ft_strcmp(action_id, g_actions[i].id) != 0) i++; if (i == ACTIONS_SIZE) @@ -80,7 +80,7 @@ t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], t_stack *a, t_stack *b) return (STATUS_SUCCESS); } -t_bool stack_sorted(t_stack *stack) +t_bool stack_sorted(t_stack *stack) { int i; diff --git a/src/checker/checker.h b/src/checker/checker.h index a6a2cc3..e6973b6 100644 --- a/src/checker/checker.h +++ b/src/checker/checker.h @@ -6,9 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:12 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:51:42 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:01:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include <stdio.h> #ifndef CHECKER_H # define CHECKER_H @@ -17,6 +18,11 @@ # include "common.h" # include "libft.h" +# define FLAG_ARG_A (1 << 0) +# define FLAG_ARG_B (1 << 1) +# define FLAG_ARG_A_B (1 << 2) +# define FLAG_ARG_B_A (1 << 3) + typedef enum { STATUS_SUCCESS, @@ -25,35 +31,13 @@ typedef enum STATUS_EOF, } t_status; -typedef enum -{ - ACTION_SA, - ACTION_SB, - ACTION_SS, - ACTION_PA, - ACTION_PB, - ACTION_RA, - ACTION_RB, - ACTION_RR, - ACTION_RRA, - ACTION_RRB, - ACTION_RRR, - ACTION_ERROR, - ACTION_EOF -} t_action_id; - - - -# define FLAG_ARG_A (1 << 0) -# define FLAG_ARG_B (1 << 1) -# define FLAG_ARG_A_B (1 << 2) -# define FLAG_ARG_B_A (1 << 3) +# define ACTION_ID_BUF_SIZE 4 typedef char t_flag_arg; typedef struct { - char *id; + const char *id; t_flag_arg args; union { @@ -64,7 +48,8 @@ typedef struct t_status check(t_stack *a, t_stack *b); t_status read_action(t_stack *a, t_stack *b); -t_status exec_action(char *action_id, t_stack *a, t_stack *b); +t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], + t_stack *a, t_stack *b); t_bool stack_sorted(t_stack *stack); #endif diff --git a/src/checker/main.c b/src/checker/main.c index fdac230..3866800 100644 --- a/src/checker/main.c +++ b/src/checker/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:14 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:44:28 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:04:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,35 +16,53 @@ int main(int argc, char **argv) { t_status s; + t_stack *a; + t_stack *b; if (argc == 1) return (0); - t_stack *a = stack_new(argc - 1); - while (--argc > 1) + if ((a = stack_new(argc - 1)) == NULL) + return (1); + while (--argc >= 1) { + printf("argc %d\n", argc); errno = 0; - /* ft_strict_atoi(argv[argc]); */ stack_push(a, ft_strict_atoi(argv[argc])); if (errno != 0) { - stack_destroy(a); ft_putendl_fd("Error", STDERR_FILENO); + stack_destroy(a); return (1); } } - t_stack *b = stack_new(argc - 1); - for (int i = 0; i < a->top; i++) + if ((b = stack_new(stack_length(a))) == NULL) + { + stack_destroy(a); + return (1); + } + printf("stack a: "); + for (int i = 0; i <= a->top; i++) + printf("[%d]", a->elements[i]); + printf("\nstack b: "); + for (int i = 0; i <= b->top; i++) + printf("[%d]", b->elements[i]); + printf("\n"); + + s = check(a, b); + if (s == STATUS_SUCCESS) + ft_putendl("OK"); + else if (s == STATUS_FAILURE) + ft_putendl("KO"); + else if (s == STATUS_ERROR) + ft_putendl_fd("Error", STDERR_FILENO); + + printf("stack a: "); + for (int i = 0; i <= a->top; i++) printf("[%d]", a->elements[i]); - for (int i = 0; i < b->top; i++) + printf("\nstack b: "); + for (int i = 0; i <= b->top; i++) printf("[%d]", b->elements[i]); - printf("\n%d", stack_empty(b)); - /* s = check(a, b); */ - /* if (s == STATUS_SUCCESS) */ - /* ft_putendl("OK"); */ - /* else if (s == STATUS_FAILURE) */ - /* ft_putendl("KO"); */ - /* else if (s == STATUS_ERROR) */ - /* ft_putendl_fd("Error", STDERR_FILENO); */ + printf("\n"); stack_destroy(a); stack_destroy(b); return 0; diff --git a/src/common/action.c b/src/common/action.c index bfbe436..c095e59 100644 --- a/src/common/action.c +++ b/src/common/action.c @@ -2,55 +2,71 @@ void swap_a(t_stack *a) { - + stack_swap(a); } void swap_b(t_stack *b) { - + stack_swap(b); } void swap_both(t_stack *a, t_stack *b) { - + swap_a(a); + swap_b(b); } void push_a(t_stack *a, t_stack *b) { + int tmp; + if (stack_empty(b)) + return ; + tmp = stack_peek(b); + stack_pop(b); + stack_push(a, tmp); } void push_b(t_stack *b, t_stack *a) { + int tmp; + + if (stack_empty(a)) + return ; + tmp = stack_peek(a); + stack_pop(a); + stack_push(b, tmp); } void rotate_a(t_stack *a) { - + stack_rotate(a); } void rotate_b(t_stack *b) { - + stack_rotate(b); } void rotate_both(t_stack *a, t_stack *b) { - + rotate_a(a); + rotate_b(b); } void reverse_rotate_a(t_stack *a) { - + stack_reverse_rotate(a); } void reverse_rotate_b(t_stack *b) { - + stack_reverse_rotate(b); } void reverse_rotate_both(t_stack *a, t_stack *b) { - + reverse_rotate_a(a); + reverse_rotate_b(b); } diff --git a/src/common/common.h b/src/common/common.h index 18ccae3..b914d5e 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -1,3 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/18 11:20:54 by cacharle #+# #+# */ +/* Updated: 2020/01/18 11:21:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include <stdio.h> + #ifndef STACK_H # define STACK_H diff --git a/src/common/stack.c b/src/common/stack.c index 02d3207..269dbc4 100644 --- a/src/common/stack.c +++ b/src/common/stack.c @@ -26,6 +26,7 @@ void stack_destroy(t_stack *stack) void stack_push(t_stack *stack, int n) { stack->top++; + printf("%d\n", stack->top); stack->elements[stack->top] = n; } |
