Nice job for a week-end =)

finished checker programm
tested actions functions
all normed and sexy ;)
This commit is contained in:
Tanguy MAZE 2019-03-02 18:28:51 +01:00
parent f8c0e70d9d
commit 87261ffddb
7 changed files with 156 additions and 91 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_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

View File

@ -6,95 +6,102 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int check_input(char *in, int *ret)
void exec_actions(t_psdata *data)
{
int i;
char *buff;
t_list *nop;
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);
nop = data->op;
while (nop && (buff = (char*)nop->content))
{
if (ft_strlen(buff) == 2)
{
if (buff[0] == 's' && buff[1] != 's')
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 get_params(t_psdata *data, int ac, char **av)
int read_ops(t_psdata *data)
{
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][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_stack *new;
t_list *nop;
char buff[5];
int ret;
ps_initdata(&data);
ft_memset(buff, '\0', 5);
if (!get_params(&data, ac, av))
return (0);
new = data.a;
if (new == NULL)
ft_putendl("Empty");
else
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);
ps_stkclean(&data);
return (1);
}
printf("%d\n", ret);
ft_lstaddend(&(data.op), nop);
ft_lstaddend(&(data->op), nop);
}
return (0);
}
void check_stack(t_psdata *data)
{
t_stack *ind;
int nb;
ind = data->a;
nb = ind->nb;
while (ind)
{
if (ind->nb < nb)
{
ft_putendl("KO");
return ;
}
nb = ind->nb;
ind = ind->next;
}
if (!ind)
ft_putendl("OK");
}
int main(int ac, char **av)
{
t_psdata data;
t_stack *new;
int ret;
ps_initdata(&data);
ret = 0;
if (!get_params(&data, ac, av))
return (0);
new = data.a;
if (new == NULL)
ft_putendl("Empty");
if (new == NULL || read_ops(&data))
{
ps_stkclean(&data);
return (0);
}
exec_actions(&data);
check_stack(&data);
ps_stkclean(&data);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -15,7 +15,6 @@
void ps_rerot(t_psdata *data, char c)
{
t_stack *s;
t_stack *prec;
t_stack *tmp;
size_t size;
@ -29,14 +28,13 @@ void ps_rerot(t_psdata *data, char c)
size = data->size_b;
if (s != NULL && size > 2)
{
prec = s;
tmp = s->next;
while (tmp->next != NULL)
{
tmp = tmp->next;
prec = prec->next;
s = s->next;
}
prec->next = NULL;
s->next = NULL;
ps_stkpsh(data, c, tmp);
}
else if (s != NULL && size == 2)

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 *tmp;
t_stack *ind;
size_t size;
size_t *size;
if (c == 'a')
s = data->a;
if (c == 'a')
size = data->size_a;
size = &(data->size_a);
else if (c == 'b')
s = data->b;
if (c == 'b')
size = data->size_b;
if (s != NULL && size > 2)
size = &(data->size_b);
if (s != NULL && *size > 2)
{
s = s->next;
tmp = ps_stkpop(data, c);
ind = s;
while (ind->next != NULL)
ind = ind->next;
ind->next = tmp;
tmp->next = NULL;
while (s->next != NULL)
s = s->next;
s->next = tmp;
(*size)++;
}
else if (s != NULL && size == 2)
else if (s != NULL && *size == 2)
ps_swap(data, c);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 **s;
size_t size;
size_t *size;
s = NULL;
if (data != NULL && new != NULL)
@ -36,17 +36,17 @@ void ps_stkpsh(t_psdata *data, char c, t_stack *new)
if (c == 'a')
s = &(data->a);
if (c == 'a')
size = data->size_a;
size = &(data->size_a);
if (c == 'b')
s = &(data->b);
if (c == 'b')
size = data->size_b;
size = &(data->size_b);
if (s != NULL)
{
ind = *s;
*s = new;
new->next = ind;
size++;
(*size)++;
}
}
}
@ -55,16 +55,16 @@ t_stack *ps_stkpop(t_psdata *data, char c)
{
t_stack *ret;
t_stack **s;
size_t size;
size_t *size;
if (c == 'a')
s = &(data->a);
if (c == 'a')
size = data->size_a;
size = &(data->size_a);
if (c == 'b')
s = &(data->b);
if (c == 'b')
size = data->size_b;
size = &(data->size_b);
else if (c != 'a' && c != 'b')
return (NULL);
ret = NULL;
@ -72,7 +72,7 @@ t_stack *ps_stkpop(t_psdata *data, char c)
{
ret = *s;
*s = ret->next;
size--;
(*size)--;
}
return (ret);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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->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);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */