-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_check.c
More file actions
104 lines (95 loc) · 2.32 KB
/
utils_check.c
File metadata and controls
104 lines (95 loc) · 2.32 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 20:18:46 by uyilmaz #+# #+# */
/* Updated: 2023/03/07 05:43:15 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void print_error(char *str)
{
int i;
i = 0;
while (str[i])
{
write(2, &str[i], 1);
i++;
}
}
int check_doubles(int **arr, int size)
{
int i;
int j;
i = 0;
while (i < size)
{
j = i + 1;
while (j < size)
{
if (*(arr[i]) == *(arr[j]))
{
print_error("Error\n*** The arguments must not repeat!\n");
return (1);
}
j++;
}
i++;
}
return (0);
}
int **to_arr(char *str, int *size)
{
char **d_str;
int **arr;
int i;
d_str = ft_split(str, ' ');
*size = 0;
while (d_str[*size])
(*size)++;
if (*size == 0)
return (NULL);
arr = malloc(sizeof(int *) * (*size));
if (!arr)
return (NULL);
i = 0;
while (d_str[i])
{
arr[i] = malloc(sizeof(int));
if (!arr[i])
return (NULL);
*(arr[i]) = ft_atoi(d_str[i]);
i++;
}
free_double_char(d_str);
return (arr);
}
int char_check(char *d)
{
int result;
result = 1;
while (*d)
{
if ((*d < '0' || *d > '9') && (*d != ' '
&& result % 2 != 0) && (*d != '+' && *d != '-'))
result *= 2;
if ((*d >= '0' && *d <= '9') && ((*(d + 1) != ' ')
&& (*(d + 1) < '0' || *(d + 1) > '9')))
result *= 3;
if ((*d == '-' || *d == '+') && (*(d + 1) <= '0'
|| *(d + 1) > '9') && result % 3 != 0)
result *= 3;
d++;
}
if (result == 1)
return (0);
print_error("Error\n");
if (result % 2 == 0)
print_error("*** All arguments must be integer!\n");
if (result % 3 == 0)
print_error("*** Sign of arguments must be valid!\n");
return (1);
}