-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnumber_cmp.c
More file actions
41 lines (38 loc) · 1.61 KB
/
number_cmp.c
File metadata and controls
41 lines (38 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* number_cmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lilam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/15 08:24:57 by lilam #+# #+# */
/* Updated: 2018/01/31 02:37:56 by hiroshius ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int number_cmp(char *str1, char *str2)
{
char *cmp1;
char *cmp2;
cmp1 = ft_strdup(str1);
truncate_zeros(&cmp1);
cmp2 = ft_strdup(str2);
truncate_zeros(&cmp2);
if (cmp1[0] == '-' && (zeroed(cmp2) || ft_strcmp(cmp2, "0") > 0))
return (-1);
else if (cmp2[0] == '-' && (zeroed(cmp1) || ft_strcmp(cmp1, "0") > 0))
return (1);
if (cmp1[0] == '-' && cmp2[0] == '-')
{
if (ft_strlen(cmp2) > ft_strlen(cmp1))
return (1);
else if (ft_strlen(cmp1) > ft_strlen(cmp2))
return (-1);
return (-ft_strcmp(cmp1, cmp2));
}
if (ft_strcmp(cmp1, cmp2) > 0 && ft_strlen(cmp2) > ft_strlen(cmp1))
return (-1);
if (ft_strcmp(cmp1, cmp2) < 0 && ft_strlen(cmp2) < ft_strlen(cmp1))
return (1);
return (ft_strcmp(cmp1, cmp2));
}