Initial commit
This commit is contained in:
55
Makefile
Normal file
55
Makefile
Normal file
@@ -0,0 +1,55 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2021/03/05 13:10:44 by lejulien #+# #+# #
|
||||
# Updated: 2021/04/02 13:28:10 by lejulien ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS_UTILS = ./srcs/utils/ft_atoi.c ./srcs/utils/ft_putnbr_fd.c \
|
||||
./srcs/utils/ft_putstr.c ./srcs/utils/ft_sqrt.c \
|
||||
./srcs/utils/ft_itoa.c ./srcs/utils/ft_strcmp.c
|
||||
SRCS_CHECKER = ./srcs/checker/checker.c ./srcs/checker/actions.c \
|
||||
./srcs/checker/error.c ./srcs/checker/checker2.c \
|
||||
./srcs/checker/entry.c ./srcs/checker/check_stack.c \
|
||||
./srcs/checker/parse_entry.c
|
||||
SRCS_PUSH_SWAP = ./srcs/push_swap/push_swap.c ./srcs/push_swap/actions.c \
|
||||
./srcs/push_swap/error.c ./srcs/push_swap/action2.c \
|
||||
./srcs/push_swap/check_stack.c ./srcs/push_swap/bubblesort.c \
|
||||
./srcs/push_swap/sort.c ./srcs/push_swap/push_swap2.c \
|
||||
./srcs/push_swap/chunked.c ./srcs/push_swap/goto.c \
|
||||
./srcs/push_swap/count.c ./srcs/push_swap/last_max.c \
|
||||
./srcs/push_swap/sort_chunk.c
|
||||
OBJS_UTILS = $(SRCS_UTILS:.c=.o)
|
||||
OBJS_CHECKER = $(SRCS_CHECKER:.c=.o)
|
||||
OBJS_PUSH_SWAP = $(SRCS_PUSH_SWAP:.c=.o)
|
||||
|
||||
NAME = checker
|
||||
|
||||
GCC = gcc
|
||||
|
||||
FLAGS = -Wall -Wextra -Werror
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
.PHONY: clean fclean re
|
||||
|
||||
$(NAME): $(OBJS_UTILS) $(OBJS_CHECKER) $(OBJS_PUSH_SWAP)
|
||||
gcc -o $(NAME) $(FLAGS) $(OBJS_UTILS) $(OBJS_CHECKER)
|
||||
gcc -o push_swap $(FLAGS) $(OBJS_UTILS) $(OBJS_PUSH_SWAP)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS_CHECKER) $(OBJS_PUSH_SWAP) $(OBJS_UTILS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
rm -f push_swap
|
||||
|
||||
re: fclean all
|
||||
|
||||
%.o: %.c
|
||||
$(GCC) -c $< -o $(<:.c=.o)
|
||||
108
srcs/checker/actions.c
Normal file
108
srcs/checker/actions.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* actions.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 15:57:06 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/16 15:32:51 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "checker.h"
|
||||
|
||||
int
|
||||
add_to_stack(t_stack **stack, int value, int is_disp)
|
||||
{
|
||||
t_stack *new;
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
if (!(new = malloc(sizeof(t_stack))))
|
||||
return (1);
|
||||
new->value = value;
|
||||
new->next = NULL;
|
||||
new->disp = is_disp;
|
||||
if (*stack == NULL)
|
||||
{
|
||||
*stack = new;
|
||||
return (0);
|
||||
}
|
||||
while (ptr->next != NULL)
|
||||
ptr = ptr->next;
|
||||
ptr->next = new;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
push(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
if (*a_stack == NULL)
|
||||
return ;
|
||||
ptr = *a_stack;
|
||||
*a_stack = ptr->next;
|
||||
ptr->next = *b_stack;
|
||||
*b_stack = ptr;
|
||||
}
|
||||
|
||||
void
|
||||
swap(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
*stack = b;
|
||||
a->next = b->next;
|
||||
b->next = a;
|
||||
}
|
||||
|
||||
void
|
||||
rotate(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
while (b->next != NULL)
|
||||
b = b->next;
|
||||
*stack = a->next;
|
||||
b->next = a;
|
||||
a->next = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
r_rotate(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
while (b->next != NULL)
|
||||
{
|
||||
a = a->next;
|
||||
b = b->next;
|
||||
}
|
||||
a->next = NULL;
|
||||
b->next = *stack;
|
||||
*stack = b;
|
||||
}
|
||||
44
srcs/checker/check_stack.c
Normal file
44
srcs/checker/check_stack.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 13:39:16 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 13:40:34 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "checker.h"
|
||||
#include <stdlib.h>
|
||||
#include "../utils/utils.h"
|
||||
|
||||
void
|
||||
check_stack(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int prev;
|
||||
|
||||
ptr = *stack;
|
||||
if (ptr == NULL)
|
||||
return (ft_putstr("KO\n"));
|
||||
if (ptr->next == NULL)
|
||||
{
|
||||
ft_putstr("OK\n");
|
||||
return ;
|
||||
}
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr != NULL)
|
||||
{
|
||||
if (prev > ptr->value)
|
||||
{
|
||||
ft_putstr("KO\n");
|
||||
return ;
|
||||
}
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ft_putstr("OK\n");
|
||||
}
|
||||
110
srcs/checker/checker.c
Normal file
110
srcs/checker/checker.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:01:57 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/02 13:29:50 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../utils/utils.h"
|
||||
#include <unistd.h>
|
||||
#include "checker.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
push_values_to_stack(t_stack **stack, char **av, int ac, int pos)
|
||||
{
|
||||
int i;
|
||||
t_stack *b_stack;
|
||||
t_stack *ptr;
|
||||
|
||||
b_stack = NULL;
|
||||
i = pos;
|
||||
while (i < ac)
|
||||
{
|
||||
if (add_to_stack(stack, ft_atoi(av[i]), pos))
|
||||
return (ft_free_stacks(stack, &b_stack, 1));
|
||||
i++;
|
||||
}
|
||||
ptr = *stack;
|
||||
if (has_double(stack))
|
||||
return (ft_free_stacks(stack, &b_stack, 1));
|
||||
if (entry(stack, &b_stack))
|
||||
return (ft_free_stacks(stack, &b_stack, 1));
|
||||
return (ft_free_stacks(stack, &b_stack, 1) - 1);
|
||||
}
|
||||
|
||||
static int
|
||||
is_flag(char *str)
|
||||
{
|
||||
if (str[0] == '-' && str[1] == 'v')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
check_flags(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (ac > 2)
|
||||
{
|
||||
if (is_flag(av[1]))
|
||||
i++;
|
||||
}
|
||||
return (1 + i);
|
||||
}
|
||||
|
||||
int
|
||||
check_num(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 1;
|
||||
while (i < ac)
|
||||
{
|
||||
j = 0;
|
||||
while (av[i][j] != '\0')
|
||||
{
|
||||
if (av[i][j] < '0' || av[i][j] > '9')
|
||||
{
|
||||
if (av[i][j] != '-')
|
||||
return (1);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
check_max(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
int i_tmp;
|
||||
|
||||
i = 1;
|
||||
while (i < ac)
|
||||
{
|
||||
if (av[i][0] == '\0')
|
||||
return (1);
|
||||
i_tmp = ft_atoi(av[i]);
|
||||
tmp = ft_itoa(i_tmp);
|
||||
if (i_tmp != 0 && my_strcmp(av[i], tmp))
|
||||
{
|
||||
free(tmp);
|
||||
return (1);
|
||||
}
|
||||
free(tmp);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
40
srcs/checker/checker.h
Normal file
40
srcs/checker/checker.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:38:15 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 18:24:46 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef CHECKER_H
|
||||
# define CHECKER_H
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int value;
|
||||
int disp;
|
||||
void *next;
|
||||
} t_stack;
|
||||
|
||||
int add_to_stack(t_stack **stack, int value, int pos);
|
||||
int ft_free_stacks(t_stack **a_stack, t_stack **b_stack, int ret);
|
||||
void display_stack(t_stack **a_stack, t_stack **b_stack);
|
||||
int has_double(t_stack **stack);
|
||||
int entry(t_stack **a_stack, t_stack **b_stack);
|
||||
void push(t_stack **a_stack, t_stack **b_stack);
|
||||
void swap(t_stack **stack);
|
||||
void rotate(t_stack **stack);
|
||||
void r_rotate(t_stack **stack);
|
||||
void check_stack(t_stack **stack);
|
||||
int push_values_to_stack(t_stack **stack, char **av, int ac,
|
||||
int pos);
|
||||
int check_flags(int ac, char **av);
|
||||
int check_num(int ac, char **av);
|
||||
int check_max(int ac, char **av);
|
||||
int parse_entry(char *str, t_stack **a_stack, t_stack **b_stack);
|
||||
int ft_strlen(char *str);
|
||||
#endif
|
||||
58
srcs/checker/checker2.c
Normal file
58
srcs/checker/checker2.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 13:45:52 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 13:52:35 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "checker.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
is_number(char *c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (c[i] != '\0')
|
||||
{
|
||||
if (c[i] < '0' || c[i] > '9')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int ac, char **av)
|
||||
{
|
||||
t_stack *a_stack;
|
||||
int flags;
|
||||
|
||||
a_stack = NULL;
|
||||
if (ac > 1)
|
||||
{
|
||||
if (check_num(ac, av) || check_max(ac, av))
|
||||
{
|
||||
ft_putstr("Error\n");
|
||||
return (1);
|
||||
}
|
||||
if (!(flags = check_flags(ac, av)))
|
||||
{
|
||||
ft_putstr("Error\n");
|
||||
return (1);
|
||||
}
|
||||
if (push_values_to_stack(&a_stack, av, ac, flags))
|
||||
{
|
||||
ft_putstr("Error\n");
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
74
srcs/checker/entry.c
Normal file
74
srcs/checker/entry.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* entry.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 13:53:40 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 14:27:24 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "checker.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char
|
||||
*add_char(char *src, char c)
|
||||
{
|
||||
char *ret;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
if (src == NULL)
|
||||
{
|
||||
if (!(ret = malloc(2 * sizeof(char))))
|
||||
return (NULL);
|
||||
ret[0] = c;
|
||||
ret[1] = '\0';
|
||||
return (ret);
|
||||
}
|
||||
if (!(ret = malloc((ft_strlen(src) + 1) * sizeof(char))))
|
||||
return (NULL);
|
||||
while (src[index] != '\0')
|
||||
{
|
||||
ret[index] = src[index];
|
||||
index++;
|
||||
}
|
||||
ret[index] = c;
|
||||
ret[index + 1] = '\0';
|
||||
free(src);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
entry(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
int run;
|
||||
char c;
|
||||
char *entry;
|
||||
int ret;
|
||||
|
||||
run = 1;
|
||||
entry = NULL;
|
||||
while (run)
|
||||
{
|
||||
ret = read(0, &c, 1);
|
||||
if (ret == -1 || ret == 0 || c == '\x4')
|
||||
run = 0;
|
||||
entry = add_char(entry, c);
|
||||
if (c == '\n')
|
||||
{
|
||||
ret = parse_entry(entry, a_stack, b_stack);
|
||||
free(entry);
|
||||
if (ret == 1)
|
||||
return (1);
|
||||
entry = NULL;
|
||||
}
|
||||
}
|
||||
free(entry);
|
||||
(*b_stack != NULL) ? ft_putstr("KO\n") : check_stack(a_stack);
|
||||
return (0);
|
||||
}
|
||||
64
srcs/checker/error.c
Normal file
64
srcs/checker/error.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 15:59:25 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 13:40:58 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "checker.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
static void
|
||||
ft_free_stack(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *nxt;
|
||||
|
||||
if (!*stack)
|
||||
return ;
|
||||
tmp = *stack;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
nxt = tmp->next;
|
||||
free(tmp);
|
||||
tmp = nxt;
|
||||
}
|
||||
*stack = NULL;
|
||||
}
|
||||
|
||||
int
|
||||
ft_free_stacks(t_stack **a_stack, t_stack **b_stack, int ret)
|
||||
{
|
||||
ft_free_stack(a_stack);
|
||||
if (b_stack != NULL)
|
||||
ft_free_stack(b_stack);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
has_double(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr1;
|
||||
t_stack *ptr2;
|
||||
|
||||
ptr1 = *stack;
|
||||
ptr2 = ptr1->next;
|
||||
while (ptr1->next != NULL)
|
||||
{
|
||||
ptr2 = ptr1->next;
|
||||
while (ptr2 != NULL)
|
||||
{
|
||||
if (ptr1->value == ptr2->value)
|
||||
return (1);
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
ptr1 = ptr1->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
99
srcs/checker/parse_entry.c
Normal file
99
srcs/checker/parse_entry.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_entry.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 14:15:28 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 14:16:49 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "checker.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
int
|
||||
ft_strlen(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int
|
||||
ft_strcmp(char *src, char *dst)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (ft_strlen(src) != ft_strlen(dst))
|
||||
return (0);
|
||||
while (src[i] != '\0')
|
||||
{
|
||||
if (src[i] != dst[i])
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int
|
||||
parse_entry2(char *str, t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
if (ft_strcmp("ss\n", str))
|
||||
{
|
||||
swap(a_stack);
|
||||
swap(b_stack);
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp("ra\n", str))
|
||||
{
|
||||
rotate(a_stack);
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp("rb\n", str))
|
||||
{
|
||||
rotate(b_stack);
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp("rr\n", str))
|
||||
{
|
||||
rotate(a_stack);
|
||||
rotate(b_stack);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
parse_entry(char *str, t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
if (ft_strcmp("pa\n", str))
|
||||
push(b_stack, a_stack);
|
||||
else if (ft_strcmp("pb\n", str))
|
||||
push(a_stack, b_stack);
|
||||
else if (ft_strcmp("sa\n", str))
|
||||
swap(a_stack);
|
||||
else if (ft_strcmp("sb\n", str))
|
||||
swap(b_stack);
|
||||
else if (ft_strcmp("rra\n", str))
|
||||
r_rotate(a_stack);
|
||||
else if (parse_entry2(str, a_stack, b_stack))
|
||||
{
|
||||
}
|
||||
else if (ft_strcmp("rrb\n", str))
|
||||
r_rotate(b_stack);
|
||||
else if (ft_strcmp("rrr\n", str))
|
||||
{
|
||||
r_rotate(a_stack);
|
||||
r_rotate(b_stack);
|
||||
}
|
||||
else if (ft_strcmp("\n", str))
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
28
srcs/push_swap/action2.c
Normal file
28
srcs/push_swap/action2.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* action2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 14:52:29 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 14:53:06 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void
|
||||
stack_dup(t_stack **res, t_stack **src)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
*res = NULL;
|
||||
ptr = *src;
|
||||
while (ptr)
|
||||
{
|
||||
add_to_stack(res, ptr->value, 0);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
110
srcs/push_swap/actions.c
Normal file
110
srcs/push_swap/actions.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* actions.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 15:57:06 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 14:53:36 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "../utils/utils.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
int
|
||||
add_to_stack(t_stack **stack, int value, int is_disp)
|
||||
{
|
||||
t_stack *new;
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
if (!(new = malloc(sizeof(t_stack))))
|
||||
return (1);
|
||||
new->value = value;
|
||||
new->next = NULL;
|
||||
new->disp = is_disp;
|
||||
new->part = 0;
|
||||
if (*stack == NULL)
|
||||
{
|
||||
*stack = new;
|
||||
return (0);
|
||||
}
|
||||
while (ptr->next != NULL)
|
||||
ptr = ptr->next;
|
||||
ptr->next = new;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
push(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
if (*a_stack == NULL)
|
||||
return ;
|
||||
ptr = *a_stack;
|
||||
*a_stack = ptr->next;
|
||||
ptr->next = *b_stack;
|
||||
*b_stack = ptr;
|
||||
}
|
||||
|
||||
void
|
||||
swap(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
*stack = b;
|
||||
a->next = b->next;
|
||||
b->next = a;
|
||||
}
|
||||
|
||||
void
|
||||
rotate(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
while (b->next != NULL)
|
||||
b = b->next;
|
||||
*stack = a->next;
|
||||
b->next = a;
|
||||
a->next = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
r_rotate(t_stack **stack)
|
||||
{
|
||||
t_stack *a;
|
||||
t_stack *b;
|
||||
|
||||
a = *stack;
|
||||
if (!a)
|
||||
return ;
|
||||
b = a->next;
|
||||
if (!b)
|
||||
return ;
|
||||
while (b->next != NULL)
|
||||
{
|
||||
a = a->next;
|
||||
b = b->next;
|
||||
}
|
||||
a->next = NULL;
|
||||
b->next = *stack;
|
||||
*stack = b;
|
||||
}
|
||||
42
srcs/push_swap/bubblesort.c
Normal file
42
srcs/push_swap/bubblesort.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* bubblesort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/11 15:30:32 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/26 15:13:01 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void
|
||||
bubble_sort(t_stack **stack, int ac)
|
||||
{
|
||||
t_stack *ptr1;
|
||||
t_stack *ptr2;
|
||||
t_stack *nill;
|
||||
int i;
|
||||
|
||||
nill = NULL;
|
||||
i = 0;
|
||||
while (check_stack(stack))
|
||||
{
|
||||
ptr1 = *stack;
|
||||
ptr2 = ptr1->next;
|
||||
if (ptr1->value > ptr2->value)
|
||||
swap(stack);
|
||||
if (check_stack(stack))
|
||||
rotate(stack);
|
||||
i++;
|
||||
if (i == (ac - 1))
|
||||
{
|
||||
rotate(stack);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
125
srcs/push_swap/check_stack.c
Normal file
125
srcs/push_swap/check_stack.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 16:50:27 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 17:42:34 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
#include "../utils/utils.h"
|
||||
|
||||
int
|
||||
check_stack(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int prev;
|
||||
|
||||
ptr = *stack;
|
||||
if (ptr == NULL)
|
||||
return (1);
|
||||
if (ptr->next == NULL)
|
||||
return (1);
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr != NULL)
|
||||
{
|
||||
if (prev > ptr->value)
|
||||
return (1);
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
r_check_stack(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int prev;
|
||||
|
||||
ptr = *stack;
|
||||
if (ptr == NULL)
|
||||
return (1);
|
||||
if (ptr->next == NULL)
|
||||
return (1);
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr != NULL)
|
||||
{
|
||||
if (prev < ptr->value)
|
||||
return (1);
|
||||
prev = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
biggest(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int val;
|
||||
|
||||
ptr = *stack;
|
||||
val = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->value > val)
|
||||
val = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (val);
|
||||
}
|
||||
|
||||
int
|
||||
val_pos(int val, t_stack **stack)
|
||||
{
|
||||
int i;
|
||||
t_stack *ptr;
|
||||
|
||||
i = 0;
|
||||
ptr = *stack;
|
||||
while (ptr)
|
||||
{
|
||||
if (val == ptr->value)
|
||||
return (i);
|
||||
i++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void
|
||||
move_top(int val, t_stack **stack)
|
||||
{
|
||||
int length;
|
||||
int pos;
|
||||
|
||||
length = part_length(stack, 0);
|
||||
if (pos < (length / 2))
|
||||
{
|
||||
print_val("val = ", val);
|
||||
while (pos <= (length / 2))
|
||||
{
|
||||
rotate(stack);
|
||||
ft_putstr("ra\n");
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (pos < length)
|
||||
{
|
||||
r_rotate(stack);
|
||||
ft_putstr("rra\n");
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
86
srcs/push_swap/chunked.c
Normal file
86
srcs/push_swap/chunked.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* chunked.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/26 15:26:29 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 18:03:55 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void
|
||||
chunked(t_stack **stack, int nbr)
|
||||
{
|
||||
int size;
|
||||
int i;
|
||||
int part;
|
||||
t_stack *ptr;
|
||||
int start_part;
|
||||
|
||||
size = part_length(stack, 0);
|
||||
size = size / nbr + 1;
|
||||
ptr = *stack;
|
||||
start_part = ptr->part;
|
||||
while (nbr > 0)
|
||||
{
|
||||
i = 0;
|
||||
ptr = NULL;
|
||||
part = new_part(stack, &ptr);
|
||||
while (i < size)
|
||||
{
|
||||
ptr = *stack;
|
||||
ptr->part = part;
|
||||
(ptr->next->part != start_part + 1) ? rotate(stack) : NULL;
|
||||
i++;
|
||||
}
|
||||
nbr--;
|
||||
}
|
||||
rotate(stack);
|
||||
}
|
||||
|
||||
static int
|
||||
find_next_val(t_stack **stack, int val)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
while (ptr->value != biggest(stack))
|
||||
ptr = ptr->next;
|
||||
while (val < ptr->value)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
if (!ptr)
|
||||
ptr = *stack;
|
||||
}
|
||||
return (ptr->value);
|
||||
}
|
||||
|
||||
void
|
||||
place_val(int val, t_stack **b_stack, t_stack **a_stack, int part)
|
||||
{
|
||||
if (*b_stack && val > smallest(b_stack) && val < biggest(b_stack))
|
||||
ft_goto(b_stack, find_next_val(b_stack, val), "b");
|
||||
else if (*b_stack)
|
||||
ft_goto(b_stack, biggest(b_stack), "b");
|
||||
push(a_stack, b_stack);
|
||||
ft_putstr("pb\n");
|
||||
}
|
||||
|
||||
static int
|
||||
is_last_in_part(t_stack **a_stack, int part)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *a_stack;
|
||||
while (ptr->next)
|
||||
ptr = ptr->next;
|
||||
if (ptr->part == part)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
104
srcs/push_swap/count.c
Normal file
104
srcs/push_swap/count.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* count.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 17:39:08 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 17:40:37 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
int
|
||||
part_length(t_stack **stack, int part)
|
||||
{
|
||||
int i;
|
||||
t_stack *ptr;
|
||||
|
||||
i = 0;
|
||||
ptr = *stack;
|
||||
while (ptr && ptr->part != part)
|
||||
ptr = ptr->next;
|
||||
while (ptr && ptr->part == part)
|
||||
{
|
||||
i++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int
|
||||
count_parts(t_stack **stack)
|
||||
{
|
||||
int i;
|
||||
int tmp;
|
||||
t_stack *ptr;
|
||||
|
||||
i = 0;
|
||||
tmp = -1;
|
||||
ptr = *stack;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->part != tmp)
|
||||
{
|
||||
i++;
|
||||
tmp = ptr->part;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void
|
||||
print_val(char *str, int val)
|
||||
{
|
||||
ft_puterror(str);
|
||||
ft_putnbr_fd(val, 2);
|
||||
ft_puterror("\n");
|
||||
}
|
||||
|
||||
int
|
||||
new_part(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
int tmp;
|
||||
t_stack *ptr;
|
||||
|
||||
tmp = -1;
|
||||
ptr = *a_stack;
|
||||
while (ptr)
|
||||
{
|
||||
if (tmp < ptr->part)
|
||||
tmp = ptr->part;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr = *b_stack;
|
||||
while (ptr)
|
||||
{
|
||||
if (tmp < ptr->part)
|
||||
tmp = ptr->part;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (tmp + 1);
|
||||
}
|
||||
|
||||
int
|
||||
smallest(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int val;
|
||||
|
||||
ptr = *stack;
|
||||
val = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->value < val)
|
||||
val = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (val);
|
||||
}
|
||||
93
srcs/push_swap/error.c
Normal file
93
srcs/push_swap/error.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 15:59:25 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 14:51:10 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void
|
||||
ft_free_stack(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *nxt;
|
||||
|
||||
if (!*stack)
|
||||
return ;
|
||||
tmp = *stack;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
nxt = tmp->next;
|
||||
free(tmp);
|
||||
tmp = nxt;
|
||||
}
|
||||
*stack = NULL;
|
||||
}
|
||||
|
||||
int
|
||||
ft_free_stacks(t_stack **a_stack, t_stack **b_stack, int ret)
|
||||
{
|
||||
ft_free_stack(a_stack);
|
||||
if (b_stack != NULL)
|
||||
ft_free_stack(b_stack);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
has_double(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr1;
|
||||
t_stack *ptr2;
|
||||
|
||||
ptr1 = *stack;
|
||||
ptr2 = ptr1->next;
|
||||
while (ptr1->next != NULL)
|
||||
{
|
||||
ptr2 = ptr1->next;
|
||||
while (ptr2 != NULL)
|
||||
{
|
||||
if (ptr1->value == ptr2->value)
|
||||
return (1);
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
ptr1 = ptr1->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
ft_puterror(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
write(2, &str[i], 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
is_part_in_stack(t_stack **stack, int part)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->part == part)
|
||||
return (1);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
61
srcs/push_swap/goto.c
Normal file
61
srcs/push_swap/goto.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* goto.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 17:34:57 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/04 17:54:41 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
static void
|
||||
ft_goto_rotate(t_stack **stack, int pos, char *name)
|
||||
{
|
||||
while (pos != 0)
|
||||
{
|
||||
rotate(stack);
|
||||
ft_putstr("r");
|
||||
ft_putstr(name);
|
||||
ft_putstr("\n");
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ft_goto_r_rotate(t_stack **stack, int pos, char *name)
|
||||
{
|
||||
while (pos != stack_len(stack))
|
||||
{
|
||||
r_rotate(stack);
|
||||
ft_putstr("rr");
|
||||
ft_putstr(name);
|
||||
ft_putstr("\n");
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ft_goto(t_stack **stack, int val, char *name)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int pos;
|
||||
|
||||
pos = 0;
|
||||
ptr = *stack;
|
||||
while (ptr && ptr->value != val)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
pos++;
|
||||
}
|
||||
if (pos == 0)
|
||||
return ;
|
||||
if (pos <= (int)(stack_len(stack) / 2))
|
||||
ft_goto_rotate(stack, pos, name);
|
||||
else
|
||||
ft_goto_r_rotate(stack, pos, name);
|
||||
}
|
||||
97
srcs/push_swap/last_max.c
Normal file
97
srcs/push_swap/last_max.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* last_max.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 17:41:48 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 17:42:37 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
is_val_in_part(t_stack **stack, int val, int part)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
while (ptr && ptr->part != part)
|
||||
ptr = ptr->next;
|
||||
while (ptr && ptr->part == part)
|
||||
{
|
||||
if (ptr->value == val)
|
||||
return (1);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
get_max(t_stack **b_stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int max;
|
||||
|
||||
if (!*b_stack)
|
||||
return (-1);
|
||||
ptr = *b_stack;
|
||||
max = ptr->value;
|
||||
ptr = ptr->next;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->value > max)
|
||||
max = ptr->value;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (max);
|
||||
}
|
||||
|
||||
void
|
||||
go_max(t_stack **b_stack, int part)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *b_stack;
|
||||
if (!*b_stack)
|
||||
return ;
|
||||
ft_putnbr_fd(get_max(b_stack), 2);
|
||||
return ;
|
||||
while (ptr->value != get_max(b_stack))
|
||||
{
|
||||
rotate(b_stack);
|
||||
ft_putstr("rb\n");
|
||||
ptr = *b_stack;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
get_last_val(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
while (ptr->next != NULL)
|
||||
ptr = ptr->next;
|
||||
return (ptr->value);
|
||||
}
|
||||
|
||||
int
|
||||
stack_len(t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
ptr = *stack;
|
||||
while (ptr)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
108
srcs/push_swap/push_swap.c
Normal file
108
srcs/push_swap/push_swap.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:01:57 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 16:47:37 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../utils/utils.h"
|
||||
#include <unistd.h>
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static int
|
||||
push_values_to_stack(t_stack **stack, char **av, int ac, int pos)
|
||||
{
|
||||
int i;
|
||||
t_stack *b_stack;
|
||||
t_stack *ptr;
|
||||
|
||||
b_stack = NULL;
|
||||
i = pos;
|
||||
while (i < ac)
|
||||
{
|
||||
if (add_to_stack(stack, ft_atoi(av[i]), pos))
|
||||
return (ft_free_stacks(stack, &b_stack, 1));
|
||||
i++;
|
||||
}
|
||||
ptr = *stack;
|
||||
if (has_double(stack))
|
||||
return (ft_free_stacks(stack, &b_stack, 1));
|
||||
sort(stack, &b_stack);
|
||||
write(1, "\x4", 1);
|
||||
return (ft_free_stacks(stack, &b_stack, 1) - 1);
|
||||
}
|
||||
|
||||
static int
|
||||
is_flag(char *str)
|
||||
{
|
||||
if (str[0] == '-' && str[1] == 'v')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
is_number(char *c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (c[i] != '\0')
|
||||
{
|
||||
if (c[i] < '0' || c[i] > '9')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int
|
||||
check_flags(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int x;
|
||||
|
||||
i = 0;
|
||||
if (ac > 2)
|
||||
{
|
||||
if (is_flag(av[1]))
|
||||
i++;
|
||||
}
|
||||
x = 1 + i;
|
||||
while (x < ac)
|
||||
{
|
||||
if (!is_number(av[x]))
|
||||
return (0);
|
||||
x++;
|
||||
}
|
||||
return (1 + i);
|
||||
}
|
||||
|
||||
int
|
||||
main(int ac, char **av)
|
||||
{
|
||||
t_stack *a_stack;
|
||||
int flags;
|
||||
|
||||
a_stack = NULL;
|
||||
flags = 1;
|
||||
if (ac > 1)
|
||||
{
|
||||
if (check_num(ac, av) || check_max(ac, av))
|
||||
{
|
||||
ft_putstr("Error\n");
|
||||
return (1);
|
||||
}
|
||||
if (push_values_to_stack(&a_stack, av, ac, flags))
|
||||
{
|
||||
ft_putstr("Error\n");
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
66
srcs/push_swap/push_swap.h
Normal file
66
srcs/push_swap/push_swap.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/11 14:24:17 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 18:20:53 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PUSH_SWAP_H
|
||||
# define PUSH_SWAP_H
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int value;
|
||||
int disp;
|
||||
int part;
|
||||
struct s_stack *next;
|
||||
} t_stack;
|
||||
|
||||
int add_to_stack(t_stack **stack, int value, int pos);
|
||||
int ft_free_stacks(t_stack **a_stack, t_stack **b_stack,
|
||||
int ret);
|
||||
void display_stack(t_stack **a_stack, t_stack **b_stack);
|
||||
int has_double(t_stack **stack);
|
||||
int entry(t_stack **a_stack, t_stack **b_stack);
|
||||
void push(t_stack **a_stack, t_stack **b_stack);
|
||||
void swap(t_stack **stack);
|
||||
void rotate(t_stack **stack);
|
||||
void r_rotate(t_stack **stack);
|
||||
int check_stack(t_stack **stack);
|
||||
void bubble_sort(t_stack **stack, int nbr);
|
||||
int part_length(t_stack **stack, int part);
|
||||
void quick_sort(t_stack **a_stack, t_stack **b_stack);
|
||||
int count_parts(t_stack **stack);
|
||||
void ft_puterror(char *str);
|
||||
int new_part(t_stack **a_stack, t_stack **b_stack);
|
||||
void debug_stack(t_stack **stack, char *str);
|
||||
int is_part_in_stack(t_stack **stack, int part);
|
||||
int r_check_stack(t_stack **stack);
|
||||
void sort(t_stack **a_stack, t_stack **b_stack);
|
||||
int smallest(t_stack **stack);
|
||||
int biggest(t_stack **stack);
|
||||
void move_top(int val, t_stack **stack);
|
||||
void print_val(char *str, int val);
|
||||
int val_pos(int val, t_stack **stack);
|
||||
int is_val_in_part(t_stack **stack, int val, int part);
|
||||
void stack_dup(t_stack **res, t_stack **src);
|
||||
void ft_free_stack(t_stack **stack);
|
||||
void chunked(t_stack **stack, int nbr);
|
||||
void sort_chunck(t_stack **a_stack, t_stack **b_stack,
|
||||
t_stack **test_stack, int nbr);
|
||||
void go_max(t_stack**b_stack, int part);
|
||||
int get_last_val(t_stack **stack);
|
||||
void ft_goto(t_stack **stack, int val, char *name);
|
||||
int stack_len(t_stack **stack);
|
||||
int check_max(int ac, char **av);
|
||||
int check_num(int ac, char **av);
|
||||
void pa(t_stack **a_stack, t_stack **b_stack);
|
||||
void ra(t_stack **a_stack);
|
||||
void place_val(int val, t_stack **b_stack, t_stack **a_stack,
|
||||
int part);
|
||||
#endif
|
||||
63
srcs/push_swap/push_swap2.c
Normal file
63
srcs/push_swap/push_swap2.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 15:03:15 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/02 13:31:16 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../utils/utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
check_num(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 1;
|
||||
while (i < ac)
|
||||
{
|
||||
j = 0;
|
||||
while (av[i][j] != '\0')
|
||||
{
|
||||
if (av[i][j] < '0' || av[i][j] > '9')
|
||||
{
|
||||
if (av[i][j] != '-')
|
||||
return (1);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
check_max(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
int i_tmp;
|
||||
|
||||
i = 1;
|
||||
while (i < ac)
|
||||
{
|
||||
if (av[i][0] == '\0')
|
||||
return (1);
|
||||
i_tmp = ft_atoi(av[i]);
|
||||
tmp = ft_itoa(i_tmp);
|
||||
if (i_tmp != 0 && my_strcmp(av[i], tmp))
|
||||
{
|
||||
free(tmp);
|
||||
return (1);
|
||||
}
|
||||
free(tmp);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
128
srcs/push_swap/sort.c
Normal file
128
srcs/push_swap/sort.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/23 13:32:28 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/04 18:06:18 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static void
|
||||
sort_3_two(t_stack **a_stack, t_stack *ptr)
|
||||
{
|
||||
if (ptr->next->value < ptr->next->next->value &&
|
||||
ptr->next->next->value < ptr->value)
|
||||
{
|
||||
rotate(a_stack);
|
||||
ft_putstr("ra\n");
|
||||
}
|
||||
else if (ptr->value < ptr->next->next->value &&
|
||||
ptr->next->next->value < ptr->next->value)
|
||||
{
|
||||
swap(a_stack);
|
||||
ft_putstr("sa\n");
|
||||
rotate(a_stack);
|
||||
ft_putstr("ra\n");
|
||||
}
|
||||
else if (ptr->next->next->value < ptr->value &&
|
||||
ptr->value < ptr->next->value)
|
||||
{
|
||||
r_rotate(a_stack);
|
||||
ft_putstr("rra\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sort_3(t_stack **a_stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *a_stack;
|
||||
if (ptr->next->value < ptr->value && ptr->value < ptr->next->next->value)
|
||||
{
|
||||
swap(a_stack);
|
||||
ft_putstr("sa\n");
|
||||
}
|
||||
else if (ptr->value > ptr->next->value &&
|
||||
ptr->next->value > ptr->next->next->value)
|
||||
{
|
||||
swap(a_stack);
|
||||
ft_putstr("sa\n");
|
||||
r_rotate(a_stack);
|
||||
ft_putstr("rra\n");
|
||||
}
|
||||
else
|
||||
sort_3_two(a_stack, ptr);
|
||||
}
|
||||
|
||||
static int
|
||||
greater_than(int val, t_stack **stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = *stack;
|
||||
while (ptr->value != smallest(stack))
|
||||
ptr = ptr->next;
|
||||
while (ptr->value < val)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
if (!ptr)
|
||||
ptr = *stack;
|
||||
}
|
||||
return (ptr->value);
|
||||
}
|
||||
|
||||
static void
|
||||
sort_5(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
while (stack_len(a_stack) != 3)
|
||||
{
|
||||
ft_goto(a_stack, smallest(a_stack), "a");
|
||||
push(a_stack, b_stack);
|
||||
ft_putstr("pb\n");
|
||||
}
|
||||
sort_3(a_stack);
|
||||
ft_goto(a_stack, smallest(a_stack), "a");
|
||||
while (*b_stack)
|
||||
{
|
||||
push(b_stack, a_stack);
|
||||
ft_putstr("pa\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sort(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
t_stack *test_stack;
|
||||
int parts;
|
||||
|
||||
if (check_stack(a_stack) == 0 || stack_len(a_stack) == 1)
|
||||
return ;
|
||||
parts = (int)((ft_sqrt(part_length(a_stack, 0))) / 2.0);
|
||||
if (part_length(a_stack, 0) > 3 && part_length(a_stack, 0) < 51)
|
||||
sort_3(a_stack);
|
||||
else if (part_length(a_stack, 0) == 5)
|
||||
sort_5(a_stack, b_stack);
|
||||
else if (part_length(a_stack, 0) == 2)
|
||||
{
|
||||
swap(a_stack);
|
||||
ft_putstr("sa\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
stack_dup(&test_stack, a_stack);
|
||||
bubble_sort(&test_stack, part_length(&test_stack, 0));
|
||||
chunked(&test_stack, parts);
|
||||
sort_chunck(a_stack, b_stack, &test_stack, parts);
|
||||
ft_free_stack(&test_stack);
|
||||
}
|
||||
}
|
||||
111
srcs/push_swap/sort_chunk.c
Normal file
111
srcs/push_swap/sort_chunk.c
Normal file
@@ -0,0 +1,111 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_chunk.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/01 18:02:43 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 18:03:58 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
static void
|
||||
put_prev_chunk_down(t_stack **a_stack, t_stack **test_stack, int part)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int last;
|
||||
|
||||
ptr = *a_stack;
|
||||
if (part == 1)
|
||||
return ;
|
||||
while (ptr && ptr->part != part - 1)
|
||||
ptr = ptr->next;
|
||||
while (ptr && ptr->part == part - 1)
|
||||
ptr = ptr->next;
|
||||
if (ptr)
|
||||
ft_goto(a_stack, ptr->value, "a");
|
||||
else
|
||||
{
|
||||
ptr = *a_stack;
|
||||
ft_goto(a_stack, ptr->value, "a");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
pa(t_stack **a_stack, t_stack **b_stack)
|
||||
{
|
||||
push(b_stack, a_stack);
|
||||
ft_putstr("pa\n");
|
||||
}
|
||||
|
||||
void
|
||||
ra(t_stack **a_stack)
|
||||
{
|
||||
rotate(a_stack);
|
||||
ft_putstr("ra\n");
|
||||
}
|
||||
|
||||
static void
|
||||
put_in_last(t_stack **a_stack, t_stack **b_stack, t_stack **test_stack,
|
||||
int part)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
t_stack *ptr;
|
||||
t_stack *a_ptr;
|
||||
|
||||
ptr = *b_stack;
|
||||
if (!ptr)
|
||||
return ;
|
||||
len = part_length(b_stack, part);
|
||||
i = 0;
|
||||
if (len != part_length(test_stack, part))
|
||||
return ;
|
||||
a_ptr = *a_stack;
|
||||
put_prev_chunk_down(a_stack, test_stack, part);
|
||||
while (i < len)
|
||||
{
|
||||
pa(a_stack, b_stack);
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
ra(a_stack);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sort_chunck(t_stack **a_stack, t_stack **b_stack, t_stack **test_stack,
|
||||
int nbr)
|
||||
{
|
||||
t_stack *a_ptr;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (i < nbr)
|
||||
{
|
||||
j = 0;
|
||||
while (j < part_length(test_stack, i + 1))
|
||||
{
|
||||
a_ptr = *a_stack;
|
||||
while (is_val_in_part(test_stack, a_ptr->value, i + 1) == 0)
|
||||
{
|
||||
ra(a_stack);
|
||||
a_ptr = *a_stack;
|
||||
}
|
||||
a_ptr->part = i + 1;
|
||||
place_val(a_ptr->value, b_stack, a_stack, i + 1);
|
||||
j++;
|
||||
}
|
||||
ft_goto(b_stack, biggest(b_stack), "b");
|
||||
put_in_last(a_stack, b_stack, test_stack, i + 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
46
srcs/utils/ft_atoi.c
Normal file
46
srcs/utils/ft_atoi.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:06:04 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 13:38:37 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
static int ft_isspace(char c)
|
||||
{
|
||||
if ((c == '\f') || (c == '\t') || (c == '\n') || (c == '\r')
|
||||
|| (c == '\v') || (c == ' '))
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int nbr;
|
||||
int sign;
|
||||
int index;
|
||||
|
||||
nbr = 0;
|
||||
sign = 1;
|
||||
index = 0;
|
||||
while ((ft_isspace(str[index]) == 1) && str[index] != '\0')
|
||||
index++;
|
||||
if ((str[index] == '+') || (str[index] == '-'))
|
||||
{
|
||||
if (str[index] == '-')
|
||||
sign = -1;
|
||||
index++;
|
||||
}
|
||||
while ((str[index] >= '0') && (str[index] <= '9'))
|
||||
{
|
||||
nbr = (nbr * 10) + (str[index] - '0');
|
||||
index++;
|
||||
}
|
||||
return (sign * nbr);
|
||||
}
|
||||
62
srcs/utils/ft_itoa.c
Normal file
62
srcs/utils/ft_itoa.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/31 16:22:36 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/31 16:22:41 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static char *ft_itoa2(int iszero, unsigned int nu, int i)
|
||||
{
|
||||
char *res;
|
||||
|
||||
if (!(res = malloc((i + 1) * sizeof(char))))
|
||||
return (0);
|
||||
res[i] = '\0';
|
||||
while (nu > 0)
|
||||
{
|
||||
i--;
|
||||
res[i] = nu % 10 + '0';
|
||||
nu = nu / 10;
|
||||
}
|
||||
i--;
|
||||
if (iszero)
|
||||
{
|
||||
res[i] = nu + '0';
|
||||
i--;
|
||||
}
|
||||
if (i == 0)
|
||||
res[i] = '-';
|
||||
return (res);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
unsigned int nu;
|
||||
int i;
|
||||
int iszero;
|
||||
|
||||
i = 0;
|
||||
if (n <= 0)
|
||||
{
|
||||
nu = -n;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
nu = n;
|
||||
iszero = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / 10;
|
||||
i++;
|
||||
}
|
||||
if (nu == 0)
|
||||
iszero++;
|
||||
return (ft_itoa2(iszero, nu, i));
|
||||
}
|
||||
38
srcs/utils/ft_putnbr_fd.c
Normal file
38
srcs/utils/ft_putnbr_fd.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:05:29 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/05 13:05:39 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
static void ft_putchar(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
||||
void ft_putnbr_fd(int nb, int fd)
|
||||
{
|
||||
unsigned int dnb;
|
||||
|
||||
if (nb < 0)
|
||||
{
|
||||
ft_putchar('-', fd);
|
||||
dnb = nb * (-1);
|
||||
}
|
||||
else
|
||||
dnb = nb;
|
||||
if (dnb >= 10)
|
||||
{
|
||||
ft_putnbr_fd(dnb / 10, fd);
|
||||
ft_putchar(dnb % 10 + '0', fd);
|
||||
}
|
||||
else
|
||||
ft_putchar(dnb + '0', fd);
|
||||
}
|
||||
26
srcs/utils/ft_putstr.c
Normal file
26
srcs/utils/ft_putstr.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:08:09 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/05 13:09:54 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
void
|
||||
ft_putstr(const char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
write(1, &str[i], 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
28
srcs/utils/ft_sqrt.c
Normal file
28
srcs/utils/ft_sqrt.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sqrt.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/08/12 13:38:59 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/29 17:40:39 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
float ft_sqrt(float n)
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float e;
|
||||
|
||||
x = n;
|
||||
y = 1;
|
||||
e = 0.00001;
|
||||
while (x - y > e)
|
||||
{
|
||||
x = (x + y) / 2;
|
||||
y = n / x;
|
||||
}
|
||||
return (x);
|
||||
}
|
||||
28
srcs/utils/ft_strcmp.c
Normal file
28
srcs/utils/ft_strcmp.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/31 15:46:04 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/01 13:38:20 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int my_strcmp(char *s1, char *s2)
|
||||
{
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
while (s2[index] && s1[index])
|
||||
{
|
||||
if (s1[index] == s2[index])
|
||||
index++;
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
if (s1[index] == '\0' && s2[index] == '\0')
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
22
srcs/utils/utils.h
Normal file
22
srcs/utils/utils.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:06:31 by lejulien #+# #+# */
|
||||
/* Updated: 2021/03/31 16:01:35 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
|
||||
int ft_atoi(const char *str);
|
||||
char *ft_itoa(int n);
|
||||
void ft_putnbr_fd(int nb, int fd);
|
||||
void ft_putstr(const char *str);
|
||||
float ft_sqrt(float nb);
|
||||
int my_strcmp(char *s1, char *s2);
|
||||
#endif
|
||||
124
test.sh
Executable file
124
test.sh
Executable file
@@ -0,0 +1,124 @@
|
||||
#!/bin/bash
|
||||
make re
|
||||
|
||||
printf "### CHECKER TEST ###\n" # checker
|
||||
|
||||
printf "1 non-numeric case\n"
|
||||
export ARG="a"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "3 non-numeric case\n"
|
||||
export ARG="a b c"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "dup case\n"
|
||||
export ARG="1 1"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "I_max case\n"
|
||||
export ARG="1 2147483649"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "no arg case\n"
|
||||
./checker
|
||||
printf "\n\n"
|
||||
|
||||
printf "non existing instructions case\n"
|
||||
export ARG="14 7 9"
|
||||
printf "patate\n" | ./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "space before instructions case\n"
|
||||
export ARG="14 7 9"
|
||||
printf " pb\n" | ./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "space after instructions case\n"
|
||||
export ARG="14 7 9"
|
||||
printf "pb \n" | ./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "manual entry case (please enter some instruction then ctrl-D)\n"
|
||||
export ARG="14 7 9"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "manual entry case (please enter some instruction then ctrl-D)\n"
|
||||
export ARG="0 1 2 3"
|
||||
./checker $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "### PUSH_SWAP TEST ###\n" # push_swap test
|
||||
|
||||
printf "one case\n"
|
||||
export ARG="42"
|
||||
./push_swap $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "ordered case\n"
|
||||
export ARG="0 1 2 3 4"
|
||||
./push_swap $ARG
|
||||
printf "\n\n"
|
||||
|
||||
printf "longer ordered case\n"
|
||||
export ARG="0 1 2 3 4 5 6 7 8 9"
|
||||
./push_swap $ARG
|
||||
printf "\n\n"
|
||||
|
||||
|
||||
printf "### PUSH_SWAP AND CHECKER TEST ###\n" # push_swap and checker test
|
||||
|
||||
printf "one case\n"
|
||||
export ARG="200"
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "two case\n"
|
||||
export ARG="500 200"
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "three case\n"
|
||||
export ARG="200 0 500"
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "five case\n"
|
||||
export ARG="200 0 88 -42 100"
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "one hundred case\n"
|
||||
export ARG=`ruby -e "puts (1...100).to_a.shuffle.join(' ')"`
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "five hundred case\n"
|
||||
export ARG=`ruby -e "puts (1...500).to_a.shuffle.join(' ')"`
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
printf "all-ready solve case\n"
|
||||
export ARG="0 1 2 3 4 5 6 7 8"
|
||||
./push_swap $ARG | ./checker $ARG
|
||||
printf "len-->"
|
||||
./push_swap $ARG | grep -c ""
|
||||
printf "\n\n"
|
||||
|
||||
make fclean
|
||||
Reference in New Issue
Block a user