-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putnbr_fd.c
More file actions
85 lines (77 loc) · 1.98 KB
/
ft_putnbr_fd.c
File metadata and controls
85 lines (77 loc) · 1.98 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mschumac <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/26 01:36:43 by mschumac #+# #+# */
/* Updated: 2017/06/28 23:25:27 by mschumac ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int integer_power(int base, int exponent)
{
int i;
int result;
if (exponent == 0)
return (1);
if (exponent < 0 || base < 0)
return (0);
if (exponent == 1)
return (base);
i = 1;
result = base;
while (i < exponent)
{
result *= base;
i++;
}
return (result);
}
static int get_number_of_digits(int number)
{
int counter;
counter = 1;
while (number /= 10)
counter++;
return (counter);
}
static int handle_max_and_min_int(int n, int fd)
{
if (n == -2147483648)
{
ft_putstr_fd("-2147483648", fd);
return (1);
}
if (n == 2147483647)
{
ft_putstr_fd("2147483647", fd);
return (1);
}
return (0);
}
void ft_putnbr_fd(int n, int fd)
{
int i;
int number_length;
int decimal_place;
int current_digit;
if (handle_max_and_min_int(n, fd))
return ;
i = 1;
number_length = get_number_of_digits(n);
if (n < 0)
{
ft_putchar_fd('-', fd);
n = -n;
}
while (i <= number_length)
{
decimal_place = integer_power(10, number_length - i);
current_digit = n / decimal_place;
n = n - current_digit * decimal_place;
ft_putchar_fd(current_digit + '0', fd);
i++;
}
}