-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.c
101 lines (92 loc) · 2.25 KB
/
parsing.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abazerou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/26 12:41:18 by abazerou #+# #+# */
/* Updated: 2023/06/06 18:06:31 by abazerou ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_size(t_v *v)
{
v->i = 0;
while (v->s[v->size])
v->size++;
return (v->size);
}
int *swap_arr(int *arr, int j, int tmp)
{
if (arr[j] > arr[j + 1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
return (arr);
}
int *check_dub(int *arr, int size)
{
int i;
int j;
int tmp;
i = 0;
tmp = 0;
while (i < size - 1)
{
j = 0;
while (j < size - 1 - i)
{
arr = swap_arr(arr, j, tmp);
if (arr[j] == arr[j + 1])
{
ft_puterror("Error: Duplicate number found!\n");
}
j++;
}
i++;
}
return (arr);
}
int get_value(char *str, int i, int sign)
{
long long value;
value = 0;
while (str[i] && str[i] >= '0' && str[i] <= '9')
{
value = value * 10 + (str[i] - '0');
i++;
}
if (!ft_isdigit(str[i]) && str[i])
ft_puterror("Error:the arg is not digit!\n");
if (value * sign > 2147483647 || value * sign < -2147483648)
ft_puterror("Error: Value > INT_MAX or < INT_MIN!\n");
return (value * sign);
}
int check_num(char *str)
{
long long value;
int i;
int sign;
sign = 1;
i = 0;
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-')
{
sign = -1;
i++;
if (str[i] == '\0')
ft_puterror("Error:the arg is not digit!\n");
}
else if (str[i] == '+')
{
i++;
if (str[i] == '\0')
ft_puterror("Error:the arg is not digit!\n");
}
value = get_value(str, i, sign);
return (value);
}