-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
89 lines (80 loc) · 1.18 KB
/
init.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
#include "push_swap.h"
int error(void)
{
ft_putendl("Error");
return (1);
}
/*
* return 1 in case of error allocating
*/
int init_stack(t_pile *p, size_t len, char *argv[])
{
size_t i;
if (!(p->tab = ft_memalloc(len * sizeof(int))))
return (1);
len--;
if (argv != NULL)
{
p->len = len - 1;
if (ft_read(argv, p) == 1)
return (error());
p->min = p->tab[0];
p->max = p->tab[0];
i = 1;
while(i < len)
{
if (p->tab[i] > p->max)
p->max = p->tab[i];
if (p->tab[i] < p->min)
p->min = p->tab[i];
i++;
}
}
else
p->len = 0;
return (0);
}
/*
* 1 if digit
* 0 if not
*/
int ft_is_number(char *n)
{
int i;
i = 0;
while (n[i] != '\0' && ft_isdigit(n[i]))
i++;
if (n[i] == '\0')
return (1);
return (0);
}
int ft_isin(int *a, int n, int i)
{
while (i > 0)
{
i--;
if (a[i] == n)
return (1);
}
return (0);
}
int ft_read(char *argv[], t_pile *a)
{
size_t i;
int error;
int nb;
i = 0;
error = 0;
while (!error && i <= a->len)
{
nb = ft_atoi(argv[i + 1]);
if (ft_is_number(argv[i + 1]))
ft_isin(a->tab, nb, i + 1 ) ? (error = 1) : (a->tab[i] = nb);
else
error = 1;
i++;
}
if (error)
return (1);
return (0);
}