-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.c
99 lines (88 loc) · 2.39 KB
/
instructions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* instructions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <hbaddrul@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/09 21:53:25 by hbaddrul #+# #+# */
/* Updated: 2021/11/24 19:39:22 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
static void sx(t_list **stack_1, t_list **stack_2)
{
t_list *tmp;
(void)stack_2;
if (ft_lstsize(*stack_1) >= 2)
{
tmp = (*stack_1)->next;
(*stack_1)->next = tmp->next;
ft_lstadd_front(stack_1, tmp);
}
}
static void px(t_list **stack_1, t_list **stack_2)
{
t_list *tmp;
if (ft_lstsize(*stack_1))
{
tmp = *stack_1;
*stack_1 = (*stack_1)->next;
ft_lstadd_front(stack_2, tmp);
}
}
static void rx(t_list **stack_1, t_list **stack_2)
{
t_list *tmp;
(void)stack_2;
if (ft_lstsize(*stack_1) >= 2)
{
tmp = *stack_1;
*stack_1 = (*stack_1)->next;
tmp->next = 0;
ft_lstadd_back(stack_1, tmp);
}
}
static void rrx(t_list **stack_1, t_list **stack_2)
{
t_list *last;
t_list *tmp;
(void)stack_2;
if (ft_lstsize(*stack_1) >= 2)
{
last = *stack_1;
while (last->next)
{
tmp = last;
last = last->next;
}
tmp->next = 0;
last->next = *stack_1;
*stack_1 = last;
}
}
void run(char *cmd, t_list **stack_1, t_list **stack_2, int x)
{
int tmp;
void (*f)(t_list **, t_list **);
const int len = ft_strlen(cmd);
tmp = ft_abs(x);
if (!ft_strncmp(cmd, "sa", len) || !ft_strncmp(cmd, "sb", len)
|| !ft_strncmp(cmd, "ss", len))
f = &sx;
else if (!ft_strncmp(cmd, "pa", len) || !ft_strncmp(cmd, "pb", len))
f = &px;
else if (!ft_strncmp(cmd, "ra", len) || !ft_strncmp(cmd, "rb", len)
|| !ft_strncmp(cmd, "rr", len))
f = ℞
else
f = &rrx;
while (tmp--)
{
f(stack_1, stack_2);
if (stack_2 && f != px)
f(stack_2, 0);
if (x > 0)
ft_putendl_fd(cmd, 1);
}
}