-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.c
154 lines (127 loc) · 3.19 KB
/
checker.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
152
153
154
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: osericol <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/05 12:18:15 by osericol #+# #+# */
/* Updated: 2023/07/06 18:20:51 by osericol ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
//TODO: is_sorted function that will check if the stack is sorted in ascending order
int is_sorted(t_node *stack_a)
{
t_node *temp;
temp = stack_a;
while(temp->next)
{
if(temp->content < temp->next->content) // Cambiato da > a <
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: is_sorted_desc function that will check if the stack_b is sorted in descending order
int is_sorted_desc(t_node *stack_b)
{
t_node *temp;
temp = stack_b;
while(temp->next)
{
if(temp->content > temp->next->content) // Cambiato da < a >
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: is_number function that will check if the stack is a number
int is_number(t_node *stack_a)
{
t_node *temp;
temp = stack_a;
while(temp->next)
{
if(!ft_isdigit(temp->content))
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: is_duplicate function that will check if the stack has duplicates
int is_duplicate(t_node *stack_a)
{
t_node *temp;
temp = stack_a;
while(temp->next)
{
if(temp->content == temp->next->content)
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: is_error function that will check if the stack has errors
int is_error(t_node *stack_a)
{
t_node *temp;
temp = stack_a;
while(temp->next)
{
if(temp->content == temp->next->content)
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: is_empty function that will check if the stack is empty
int is_empty(t_node *stack_a)
{
t_node *temp;
temp = stack_a;
while(temp->next)
{
if(temp->content == temp->next->content)
{
return (0);
}
temp = temp->next;
}
return (1);
}
//TODO: Checker that will check for possible errors in code
//TODO: and return -1 if there are any
int checker(t_node *stack_a, t_node *stack_b)
{
//TODO: check if stack_a is empty, is sorted, is a number, has duplicates, has no errors
if(!stack_a || !is_sorted(stack_a) || !is_number(stack_a) ||
!is_duplicate(stack_a) || !is_error(stack_a) || !is_empty(stack_a) || !is_sorted_desc(stack_b))
return (-1);
return (0);
}
//+ If memory alloaction fails, exit
if (!stack_a || !stack_b)
{
return (1);
}
//+ If there are no arguments, exit
if (argc <= 1)
{
return (1);
}
//+ If there are arguments, check if they are numbers
if (!check_args(argc, argv))
{
return (1);
}