Nice job for a week-end =)
finished checker programm tested actions functions all normed and sexy ;)
This commit is contained in:
parent
f8c0e70d9d
commit
87261ffddb
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */
|
/* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/28 16:10:45 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 18:21:11 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -51,4 +51,8 @@ void ps_rrot(t_psdata *data);
|
|||||||
void ps_rerot(t_psdata *data, char c);
|
void ps_rerot(t_psdata *data, char c);
|
||||||
void ps_rrerot(t_psdata *data);
|
void ps_rrerot(t_psdata *data);
|
||||||
|
|
||||||
|
int is_op(char *buff);
|
||||||
|
int get_params(t_psdata *data, int ac, char **av);
|
||||||
|
int check_input(char *in, int *ret);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
129
srcs/checker.c
129
srcs/checker.c
@ -6,95 +6,102 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/28 17:02:28 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 18:28:08 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
int check_input(char *in, int *ret)
|
void exec_actions(t_psdata *data)
|
||||||
{
|
{
|
||||||
int i;
|
char *buff;
|
||||||
|
t_list *nop;
|
||||||
|
|
||||||
i = 0;
|
nop = data->op;
|
||||||
if (in[i] && ft_issign(in[i]))
|
while (nop && (buff = (char*)nop->content))
|
||||||
i++;
|
{
|
||||||
while (in[i] && ft_isdigit(in[i]))
|
if (ft_strlen(buff) == 2)
|
||||||
i++;
|
{
|
||||||
if (in[i] == '\0' && ft_hasdigit(in) && ft_atois(in, ret))
|
if (buff[0] == 's' && buff[1] != 's')
|
||||||
return (1);
|
ps_swap(data, buff[1]);
|
||||||
|
else if (buff[0] == 's' && buff[1] == 's')
|
||||||
|
ps_sswap(data);
|
||||||
|
else if (buff[0] == 'r' && buff[1] != 'r')
|
||||||
|
ps_rot(data, buff[1]);
|
||||||
|
else if (buff[0] == 'r' && buff[1] == 'r')
|
||||||
|
ps_rrot(data);
|
||||||
|
else if (buff[0] == 'p')
|
||||||
|
ps_push(data, buff[1]);
|
||||||
|
}
|
||||||
|
else if (buff[2] != 'r')
|
||||||
|
ps_rerot(data, buff[2]);
|
||||||
|
else if (buff[2] == 'r')
|
||||||
|
ps_rrerot(data);
|
||||||
|
nop = nop->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_ops(t_psdata *data)
|
||||||
|
{
|
||||||
|
t_list *nop;
|
||||||
|
char buff[5];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ft_memset(buff, '\0', 5);
|
||||||
|
while ((ret = read(1, buff, 4)) > 0)
|
||||||
|
{
|
||||||
|
buff[ft_strlen(buff) - 1] = '\0';
|
||||||
|
if (ret > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL)
|
||||||
|
{
|
||||||
|
ft_putendl_fd("Error", 2);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
ft_lstaddend(&(data->op), nop);
|
||||||
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_params(t_psdata *data, int ac, char **av)
|
void check_stack(t_psdata *data)
|
||||||
{
|
{
|
||||||
t_stack *new;
|
t_stack *ind;
|
||||||
char **tab;
|
|
||||||
int nb;
|
int nb;
|
||||||
int i;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
i = ac;
|
ind = data->a;
|
||||||
nb = 0;
|
nb = ind->nb;
|
||||||
while (--i > 0 && !(j = 0))
|
while (ind)
|
||||||
if ((tab = ft_strsplitwhitespace(av[i])) != NULL)
|
{
|
||||||
|
if (ind->nb < nb)
|
||||||
{
|
{
|
||||||
while (tab[j])
|
ft_putendl("KO");
|
||||||
j++;
|
return ;
|
||||||
while (--j >= 0)
|
|
||||||
if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL)
|
|
||||||
ps_stkpsh(data, 'a', new);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ft_putendl_fd("Error", 2);
|
|
||||||
ps_stkclean(data);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
ft_del_words_tables(&tab);
|
|
||||||
}
|
}
|
||||||
return (1);
|
nb = ind->nb;
|
||||||
|
ind = ind->next;
|
||||||
|
}
|
||||||
|
if (!ind)
|
||||||
|
ft_putendl("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_op(char *buff)
|
int main(int ac, char **av)
|
||||||
{
|
|
||||||
static char ops[11][3] = {"sa", "sb", "ss", "pa", "pb", "ra",
|
|
||||||
"rb", "rr", "rra", "rrb", "rrr"};
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (i < 11 && ft_strcmp(buff, ops[i]) != 0)
|
|
||||||
i++;
|
|
||||||
return (i < 11 && ft_strcmp(buff, ops[i]) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
|
||||||
{
|
{
|
||||||
t_psdata data;
|
t_psdata data;
|
||||||
t_stack *new;
|
t_stack *new;
|
||||||
t_list *nop;
|
|
||||||
char buff[5];
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ps_initdata(&data);
|
ps_initdata(&data);
|
||||||
ft_memset(buff, '\0', 5);
|
ret = 0;
|
||||||
if (!get_params(&data, ac, av))
|
if (!get_params(&data, ac, av))
|
||||||
return (0);
|
return (0);
|
||||||
new = data.a;
|
new = data.a;
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
ft_putendl("Empty");
|
ft_putendl("Empty");
|
||||||
else
|
if (new == NULL || read_ops(&data))
|
||||||
while ((ret = read(1, buff, 4)) > 0)
|
{
|
||||||
{
|
ps_stkclean(&data);
|
||||||
buff[ft_strlen(buff) - 1] = '\0';
|
return (0);
|
||||||
if (ret > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL)
|
}
|
||||||
{
|
exec_actions(&data);
|
||||||
ft_putendl_fd("Error", 2);
|
check_stack(&data);
|
||||||
ps_stkclean(&data);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
printf("%d\n", ret);
|
|
||||||
ft_lstaddend(&(data.op), nop);
|
|
||||||
}
|
|
||||||
ps_stkclean(&data);
|
ps_stkclean(&data);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,15 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/21 16:32:04 by tmaze #+# #+# */
|
/* Created: 2019/02/21 16:32:04 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/24 13:58:12 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 17:37:07 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
void ps_rerot(t_psdata *data, char c)
|
void ps_rerot(t_psdata *data, char c)
|
||||||
{
|
{
|
||||||
t_stack *s;
|
t_stack *s;
|
||||||
t_stack *prec;
|
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
@ -29,21 +28,20 @@ void ps_rerot(t_psdata *data, char c)
|
|||||||
size = data->size_b;
|
size = data->size_b;
|
||||||
if (s != NULL && size > 2)
|
if (s != NULL && size > 2)
|
||||||
{
|
{
|
||||||
prec = s;
|
|
||||||
tmp = s->next;
|
tmp = s->next;
|
||||||
while (tmp->next != NULL)
|
while (tmp->next != NULL)
|
||||||
{
|
{
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
prec = prec->next;
|
s = s->next;
|
||||||
}
|
}
|
||||||
prec->next = NULL;
|
s->next = NULL;
|
||||||
ps_stkpsh(data, c, tmp);
|
ps_stkpsh(data, c, tmp);
|
||||||
}
|
}
|
||||||
else if (s != NULL && size == 2)
|
else if (s != NULL && size == 2)
|
||||||
ps_swap(data, c);
|
ps_swap(data, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ps_rrerot(t_psdata *data)
|
void ps_rrerot(t_psdata *data)
|
||||||
{
|
{
|
||||||
ps_rerot(data, 'a');
|
ps_rerot(data, 'a');
|
||||||
ps_rerot(data, 'b');
|
ps_rerot(data, 'b');
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/21 16:07:55 by tmaze #+# #+# */
|
/* Created: 2019/02/21 16:07:55 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/24 13:56:33 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 16:32:04 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,26 +16,27 @@ void ps_rot(t_psdata *data, char c)
|
|||||||
{
|
{
|
||||||
t_stack *s;
|
t_stack *s;
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
t_stack *ind;
|
size_t *size;
|
||||||
size_t size;
|
|
||||||
|
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
s = data->a;
|
s = data->a;
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
size = data->size_a;
|
size = &(data->size_a);
|
||||||
else if (c == 'b')
|
else if (c == 'b')
|
||||||
s = data->b;
|
s = data->b;
|
||||||
if (c == 'b')
|
if (c == 'b')
|
||||||
size = data->size_b;
|
size = &(data->size_b);
|
||||||
if (s != NULL && size > 2)
|
if (s != NULL && *size > 2)
|
||||||
{
|
{
|
||||||
|
s = s->next;
|
||||||
tmp = ps_stkpop(data, c);
|
tmp = ps_stkpop(data, c);
|
||||||
ind = s;
|
tmp->next = NULL;
|
||||||
while (ind->next != NULL)
|
while (s->next != NULL)
|
||||||
ind = ind->next;
|
s = s->next;
|
||||||
ind->next = tmp;
|
s->next = tmp;
|
||||||
|
(*size)++;
|
||||||
}
|
}
|
||||||
else if (s != NULL && size == 2)
|
else if (s != NULL && *size == 2)
|
||||||
ps_swap(data, c);
|
ps_swap(data, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/22 14:41:27 by tmaze #+# #+# */
|
/* Created: 2019/02/22 14:41:27 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/25 15:11:44 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 15:23:43 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ void ps_stkpsh(t_psdata *data, char c, t_stack *new)
|
|||||||
{
|
{
|
||||||
t_stack *ind;
|
t_stack *ind;
|
||||||
t_stack **s;
|
t_stack **s;
|
||||||
size_t size;
|
size_t *size;
|
||||||
|
|
||||||
s = NULL;
|
s = NULL;
|
||||||
if (data != NULL && new != NULL)
|
if (data != NULL && new != NULL)
|
||||||
@ -36,17 +36,17 @@ void ps_stkpsh(t_psdata *data, char c, t_stack *new)
|
|||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
s = &(data->a);
|
s = &(data->a);
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
size = data->size_a;
|
size = &(data->size_a);
|
||||||
if (c == 'b')
|
if (c == 'b')
|
||||||
s = &(data->b);
|
s = &(data->b);
|
||||||
if (c == 'b')
|
if (c == 'b')
|
||||||
size = data->size_b;
|
size = &(data->size_b);
|
||||||
if (s != NULL)
|
if (s != NULL)
|
||||||
{
|
{
|
||||||
ind = *s;
|
ind = *s;
|
||||||
*s = new;
|
*s = new;
|
||||||
new->next = ind;
|
new->next = ind;
|
||||||
size++;
|
(*size)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,16 +55,16 @@ t_stack *ps_stkpop(t_psdata *data, char c)
|
|||||||
{
|
{
|
||||||
t_stack *ret;
|
t_stack *ret;
|
||||||
t_stack **s;
|
t_stack **s;
|
||||||
size_t size;
|
size_t *size;
|
||||||
|
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
s = &(data->a);
|
s = &(data->a);
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
size = data->size_a;
|
size = &(data->size_a);
|
||||||
if (c == 'b')
|
if (c == 'b')
|
||||||
s = &(data->b);
|
s = &(data->b);
|
||||||
if (c == 'b')
|
if (c == 'b')
|
||||||
size = data->size_b;
|
size = &(data->size_b);
|
||||||
else if (c != 'a' && c != 'b')
|
else if (c != 'a' && c != 'b')
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ret = NULL;
|
ret = NULL;
|
||||||
@ -72,7 +72,7 @@ t_stack *ps_stkpop(t_psdata *data, char c)
|
|||||||
{
|
{
|
||||||
ret = *s;
|
ret = *s;
|
||||||
*s = ret->next;
|
*s = ret->next;
|
||||||
size--;
|
(*size)--;
|
||||||
}
|
}
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */
|
/* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/25 12:48:34 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 18:20:51 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,3 +20,58 @@ void ps_initdata(t_psdata *data)
|
|||||||
data->size_b = 0;
|
data->size_b = 0;
|
||||||
data->op = NULL;
|
data->op = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int check_input(char *in, int *ret)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (in[i] && ft_issign(in[i]))
|
||||||
|
i++;
|
||||||
|
while (in[i] && ft_isdigit(in[i]))
|
||||||
|
i++;
|
||||||
|
if (in[i] == '\0' && ft_hasdigit(in) && ft_atois(in, ret))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_params(t_psdata *data, int ac, char **av)
|
||||||
|
{
|
||||||
|
t_stack *new;
|
||||||
|
char **tab;
|
||||||
|
int nb;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = ac;
|
||||||
|
nb = 0;
|
||||||
|
while (--i > 0 && !(j = 0))
|
||||||
|
if ((tab = ft_strsplitwhitespace(av[i])) != NULL)
|
||||||
|
{
|
||||||
|
while (tab[j])
|
||||||
|
j++;
|
||||||
|
while (--j >= 0)
|
||||||
|
if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||||
|
ps_stkpsh(data, 'a', new);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putendl_fd("Error", 2);
|
||||||
|
ps_stkclean(data);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
ft_del_words_tables(&tab);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_op(char *buff)
|
||||||
|
{
|
||||||
|
static char ops[11][4] = {"sa\0", "sb\0", "ss\0", "pa\0", "pb\0", "ra\0",
|
||||||
|
"rb\0", "rr\0", "rra\0", "rrb\0", "rrr\0"};
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < 11 && ft_strcmp(buff, ops[i]) != 0)
|
||||||
|
i++;
|
||||||
|
return (i < 11 && ft_strcmp(buff, ops[i]) == 0);
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/21 14:06:40 by tmaze #+# #+# */
|
/* Created: 2019/02/21 14:06:40 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/02/24 13:59:48 by tmaze ### ########.fr */
|
/* Updated: 2019/03/02 15:34:54 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user