-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_helper.c
151 lines (140 loc) · 3.11 KB
/
ft_helper.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bel-bouz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/13 20:12:27 by bel-bouz #+# #+# */
/* Updated: 2019/01/22 23:43:26 by bel-bouz ### ########.fr */
/* */
/* ************************************************************************** */
#include "swap.h"
int ajouter_index(t_pile **pile, int len_a)
{
t_pile *last;
t_pile *min;
t_var var;
var.i = 0;
last = *pile;
var.lena = len_a;
while (++var.i <= len_a)
{
var.lena = len_a + 1;
while (--var.lena && (*pile)->bl == 1)
*pile = (*pile)->prec;
min = (*pile);
while (var.lena-- > 0)
{
if (min->value > (*pile)->value && (*pile)->bl == 0)
min = (*pile);
*pile = (*pile)->prec;
}
*pile = min;
(*pile)->index = var.i;
(*pile)->bl = 1;
*pile = last;
}
return (var.i - 1);
}
void mark_up2(t_pile **pile, int len_a, int max_index, int i_max)
{
t_pile *last;
int i;
i = 0;
last = *pile;
while (i < len_a)
{
if (i > i_max && (*pile)->index == max_index)
{
len_a = i;
i = 0;
max_index--;
(*pile)->mark_up = 1;
*pile = last;
}
else
{
i++;
*(pile) = (*pile)->prec;
}
}
*pile = last;
}
void best_mark_up(t_pile **pile, int len_a, int max_index)
{
t_pile *last;
t_var var;
var.i = 0;
last = *pile;
var.nb = len_a;
var.max = len_a + 1;
while (var.i < len_a)
if ((*pile)->index == max_index)
{
len_a = var.i;
if (var.max == var.nb + 1)
var.max = var.i;
var.i = 0;
max_index--;
(*pile)->mark_up = 1;
*pile = last;
}
else
{
var.i++;
*(pile) = (*pile)->prec;
}
*pile = last;
mark_up2(&*pile, var.nb, max_index, var.max);
}
int ajout_lst(int argc, char **argv, t_pile **lst_a, t_var *var)
{
int tmp;
char **tab;
int a;
a = 0;
tmp = argc;
while (--argc >= var->i)
{
tab = ft_split_whitespaces(argv[argc]);
a = calcul_len(tab) - 1;
while (a >= 0)
{
if (ft_atoi_check(tab[a]) == 0)
return (error());
pile_push(lst_a, ft_atoi(tab[a]), var->len_a);
a--;
}
}
free(tab);
argc = tmp;
if (check(lst_a, var->lena) == 0)
return (error());
return (1);
}
int ft_atoi(const char *str)
{
int i;
int num;
int sign;
i = 0;
num = 0;
sign = 1;
while (*(str + i) == '\n' ||
*(str + i) == '\t' ||
*(str + i) == '\r' ||
*(str + i) == '\v' ||
*(str + i) == '\f' ||
*(str + i) == ' ')
i++;
if (*(str + i) == '-')
sign = -1;
if (*(str + i) == '-' || *(str + i) == '+')
i++;
while (str[i] && str[i] >= '0' && str[i] <= '9')
{
num = num * 10 + (str[i++] - '0');
}
return (num * sign);
}