-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_number.c
executable file
·130 lines (119 loc) · 2.95 KB
/
print_number.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
#include "main.h"
/**
* _isdigit - checks if character is digit
* @c: the character to check
*
* Return: 1 if digit, 0 otherwise
*/
int _isdigit(int c)
{
return (c >= '0' && c <= '9');
}
/**
* _strlen - returns the length of a string
* @s: the string whose length to check
*
* Return: integer length of string
*/
int _strlen(char *s)
{
int i = 0;
while (*s++)
i++;
return (i);
}
/**
* print_number - prints a number with options
* @str: the base number as a string
* @params: the parameter struct
*
* Return: chars printed
*/
int print_number(char *str, params_t *params)
{
unsigned int i = _strlen(str);
int neg = (!params->unsign && *str == '-');
if (!params->precision && *str == '0' && !str[1])
str = "";
if (neg)
{
str++;
i--;
}
if (params->precision != UINT_MAX)
while (i++ < params->precision)
*--str = '0';
if (neg)
*--str = '-';
if (!params->minus_flag)
return (print_number_right_shift(str, params));
else
return (print_number_left_shift(str, params));
}
/**
* print_number_right_shift - prints a number with options
* @str: the base number as a string
* @params: the parameter struct
*
* Return: chars printed
*/
int print_number_right_shift(char *str, params_t *params)
{
unsigned int n = 0, neg, neg2, i = _strlen(str);
char pad_char = ' ';
if (params->zero_flag && !params->minus_flag)
pad_char = '0';
neg = neg2 = (!params->unsign && *str == '-');
if (neg && i < params->width && pad_char == '0' && !params->minus_flag)
str++;
else
neg = 0;
if ((params->plus_flag && !neg2) ||
(!params->plus_flag && params->space_flag && !neg2))
i++;
if (neg && pad_char == '0')
n += _putchar('-');
if (params->plus_flag && !neg2 && pad_char == '0' && !params->unsign)
n += _putchar('+');
else if (!params->plus_flag && params->space_flag && !neg2 &&
!params->unsign && params->zero_flag)
n += _putchar(' ');
while (i++ < params->width)
n += _putchar(pad_char);
if (neg && pad_char == ' ')
n += _putchar('-');
if (params->plus_flag && !neg2 && pad_char == ' ' && !params->unsign)
n += _putchar('+');
else if (!params->plus_flag && params->space_flag && !neg2 &&
!params->unsign && !params->zero_flag)
n += _putchar(' ');
n += _puts(str);
return (n);
}
/**
* print_number_left_shift - prints a number with options
* @str: the base number as a string
* @params: the parameter struct
*
* Return: chars printed
*/
int print_number_left_shift(char *str, params_t *params)
{
unsigned int n = 0, neg, neg2, i = _strlen(str);
char pad_char = ' ';
if (params->zero_flag && !params->minus_flag)
pad_char = '0';
neg = neg2 = (!params->unsign && *str == '-');
if (neg && i < params->width && pad_char == '0' && !params->minus_flag)
str++;
else
neg = 0;
if (params->plus_flag && !neg2 && !params->unsign)
n += _putchar('+'), i++;
else if (params->space_flag && !neg2 && !params->unsign)
n += _putchar(' '), i++;
n += _puts(str);
while (i++ < params->width)
n += _putchar(pad_char);
return (n);
}