diff --git a/Makefile b/Makefile index 5ac82f1..0cd7075 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -SRCS = main.c ft_split.c util_func1.c util_func2.c +SRCS = main.c ft_split.c util_func1.c util_func2.c operations.c # SRCB = DIR = ./srcs/ BONUS_DIR = ./bonus/ diff --git a/push_swap b/push_swap index 34fa140..41b1a77 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/main.c b/srcs/main.c index 8a13099..b36cc54 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: ael-maaz +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/01 16:58:38 by ael-maaz #+# #+# */ -/* Updated: 2024/04/03 02:00:53 by ael-maaz ### ########.fr */ +/* Updated: 2024/04/03 02:54:25 by ael-maaz ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ void fill_stack(char **av, int ac, t_stack *stack_a) { int i = 0; int j ; - int k = ; + int k = stack_a->size - 1; stack_a->top = -1; char **arg; while(++i < ac) @@ -45,20 +45,36 @@ void fill_stack(char **av, int ac, t_stack *stack_a) while(arg[++j]) { stack_a->top++; - stack_a->stack[k++] = ft_atoi(arg[j]); + stack_a->stack[k--] = ft_atoi(arg[j]); } } - int l = 0; - while(l < stack_a->size) - printf("%d\n",stack_a->stack[l++]); } -void something(char **av, int ac, t_stack *stack_a) +int duplicates(t_stack stack_a) { - // printf("ac = %d\n", ac); - int i = 0; - int j; - char **arg; + int i =0; + int j =0; + while(i < stack_a.size) + { + j = 0; + while(j < i) + { + if(stack_a.stack[j] == stack_a.stack[i]) + return 0; + j++; + } + i++; + } + return 1; +} + +void init_stacks(char **av, int ac, t_stack *stack_a, t_stack *stack_b) +{ + int i; + int j; + char **arg; + + i = 0; stack_a->size = 0; while(++i < ac) { @@ -68,21 +84,59 @@ void something(char **av, int ac, t_stack *stack_a) stack_a->size++; } stack_a->stack = malloc(sizeof(int) * stack_a->size); - // printf("%d\n", stack_a->size); + stack_b->size = stack_a->size; + stack_b->stack = malloc(sizeof(int) * stack_b->size); + stack_b->top = -1; fill_stack(av,ac,stack_a); return ; } +void print_stacks(t_stack a,t_stack b) +{ + int i = 0; + int j = 0; + printf("-----------------stack a-----------\n"); + while(i < a.top + 1) + printf("%d\n", a.stack[i++]); + printf("-----------------stack b------------\n"); + while(j < b.top + 1) + printf("%d\n", b.stack[j++]); +} + int main(int ac, char **av) { t_stack a; - // t_stack b; + t_stack b; if (ac > 1) { if(test_argv(av, ac) == 1) { write(1, "valid\n", 6); - something(av, ac, &a); + init_stacks(av, ac, &a, &b); + if(duplicates(a) == 0) + return(write(1, "Error\n", 6),0); + print_stacks(a,b); + pb(&a,&b); + print_stacks(a,b); + pb(&a,&b); + print_stacks(a,b); + pb(&a,&b); + print_stacks(a,b); + pb(&a,&b); + print_stacks(a,b); + pb(&a,&b); + print_stacks(a,b); + printf("first part\n"); + print_stacks(a,b); + pa(&a,&b); + print_stacks(a,b); + pa(&a,&b); + print_stacks(a,b); + pa(&a,&b); + print_stacks(a,b); + pa(&a,&b); + print_stacks(a,b); + // printf("stack a top: %d stack b top: %d\n", a.stack[a.top],b.stack[b.top]); } else write(1, "Error\n", 6); diff --git a/srcs/main.o b/srcs/main.o index 1ec286b..040b782 100644 Binary files a/srcs/main.o and b/srcs/main.o differ diff --git a/srcs/operations.c b/srcs/operations.c new file mode 100644 index 0000000..a7a969b --- /dev/null +++ b/srcs/operations.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* operations.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ael-maaz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 02:34:02 by ael-maaz #+# #+# */ +/* Updated: 2024/04/03 02:44:08 by ael-maaz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void pb(t_stack *a, t_stack *b) +{ + if(a->top > -1) + { + b->top++; + b->stack[b->top] = a->stack[a->top]; + a->top--; + } +} + +void pa(t_stack *a, t_stack *b) +{ + if(b->top > -1) + { + a->top++; + a->stack[a->top] = b->stack[b->top]; + b->top--; + } +} \ No newline at end of file diff --git a/srcs/operations.o b/srcs/operations.o new file mode 100644 index 0000000..13a40f9 Binary files /dev/null and b/srcs/operations.o differ diff --git a/srcs/push_swap.h b/srcs/push_swap.h index 72b9db4..1da1b8c 100644 --- a/srcs/push_swap.h +++ b/srcs/push_swap.h @@ -6,7 +6,7 @@ /* By: ael-maaz +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/01 16:59:48 by ael-maaz #+# #+# */ -/* Updated: 2024/04/03 01:50:36 by ael-maaz ### ########.fr */ +/* Updated: 2024/04/03 02:44:24 by ael-maaz ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,8 @@ void *ft_memcpy(void *dst, const void *src, size_t n); void ft_putstr(char *s); char **ft_split(char const *s, char c); void ft_putnbr(int n); -int ft_atoi(const char *s); +int ft_atoi(const char *s); +void pb(t_stack *a, t_stack *b); +void pa(t_stack *a, t_stack *b); #endif \ No newline at end of file