-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_validation.c
96 lines (88 loc) · 1.45 KB
/
args_validation.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
#include "push_swap.h"
void sign_error(char *str, int *i, int *sign)
{
if (str[*i] == '-' || str[*i] == '+')
{
if (str[*i] == '-')
*sign = -1;
if (str[*i] == '-' && ft_issign(str[*i + 1]))
print_error();
if (ft_issign(str[*i]) && !str[*i + 1])
print_error();
if (ft_issign(str[*i]) && !ft_isdigit(str[*i + 1]))
print_error();
(*i)++;
}
if (ft_isdigit(str[*i]) && ft_issign(str[*i + 1]))
print_error();
}
void num_error(char *str)
{
int i;
i = 0;
while (str[i])
{
if (str[i + 1] && !ft_isdigit(str[i + 1]))
print_error();
i++;
}
}
void fake_atoi(char *str)
{
int i;
int sign;
long num;
i = 0;
sign = 1;
num = 0;
while (str[i] && ft_isspace(str[i]))
i++;
sign_error(str, &i, &sign);
if (ft_isdigit(str[i]))
{
num_error(str);
while (str[i] && ft_isdigit(str[i]))
{
num = (num * 10) + (str[i] - '0');
if (num * sign > 2147483647)
print_error();
else if (num * sign < -2147483648)
print_error();
i++;
}
}
else
print_error();
}
void check_duplicates(char **str, int len)
{
int i;
int j;
int *nums;
i = 0;
nums = malloc(sizeof(int) * len);
while (str[i])
{
nums[i] = ft_atoi(str[i]);
i++;
}
i = 0;
while (i < len)
{
j = i + 1;
while (j < len)
{
if (nums[i] == nums[j])
print_error();
j++;
}
i++;
}
free(nums);
}
int is_sorted(t_stack *top)
{
if (top == NULL || top->next == NULL)
return (1);
return (top->data < top->next->data && is_sorted(top->next));
}