-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_base.c
85 lines (75 loc) · 1.86 KB
/
ft_putnbr_base.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: danalvar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 22:54:19 by danalvar #+# #+# */
/* Updated: 2024/10/29 17:19:47 by danalvar ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int ft_strlen(char *str)
{
int contador;
contador = 0;
while (str[contador] != '\0')
contador++;
return (contador);
}
int check_base(char *base)
{
int i;
int j;
i = 0;
j = 0;
if (ft_strlen(base) == 0 || ft_strlen(base) == 1)
return (1);
while (base[i] != '\0')
{
j = i + 1;
while (base[j] != '\0')
{
if (base[i] == base[j] || base[i] == '-'
|| base[i] == '+')
return (1);
j++;
}
i++;
}
return (0);
}
void ft_putnbr_base(int nbr, char *base)
{
int len;
int dec;
int quot;
if (check_base(base) == 1)
return ;
len = ft_strlen(base);
dec = 1;
while (nbr / dec >= len)
dec = dec * len;
while (dec > 0)
{
quot = nbr / dec;
nbr = nbr - quot * dec;
dec = dec / len;
write(1, &base[quot], 1);
}
}
/*#include <stdio.h>
#include <stdlib.h>
int main(int argsc, char *argsv[])
{
int nb;
if (argsc != 3)
{
printf("Introduce parametros\n");
return (0);
}
nb = atoi(argsv[1]);
ft_putnbr_base(nb, argsv[2]);
return (0);
}*/