change the way itoa handle minimum

This commit is contained in:
hugodu69
2020-02-13 21:34:08 +01:00
parent 52eaaddb0d
commit d9723712ae
2 changed files with 48 additions and 14 deletions

View File

@@ -15,9 +15,10 @@
*/
/*
** #include <libc.h>
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_itoa(int n);
** char *ft_itoa(long int n);
**
** int main(int ac, char **av)
** {
@@ -26,32 +27,65 @@
** else if (ac == 2)
** printf("%s\n",ft_itoa(atoi(av[1])));
** else
** printf("%s\n",ft_itoa(0));
** {
** long int i;
** i = 0;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 1234567;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -1234567;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 237683;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 2147483647;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = i ^ 0; // create the opposite of a signed '0', which
** // is 0 followed by 31 '1', the signed int max
** printf("* %li\n* %s\n\n",i,ft_itoa(i));
** i = i ^ 0;
** i = 1 << 31; // change the most lefted bit from '0' (positive)
** // to '1' (negative), the signed int min
** printf("* %li\n* %s\n\n",i,ft_itoa(i));
** i = 2147483646;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -2147483648;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = -2147483647;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 2147483648;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -2147483649;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 9223372036854775807;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = -9223372036854775807;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** }
** return 0;
** }
*/
#include "libft.h"
char *ft_itoa(int n)
char *ft_itoa(long int n)
{
char *str;
int len;
long int nbis;
long int cpy;
char rgt;
cpy = (n < 0) ? (n / 10) * -10 : (n / 10) * 10;
len = (n < 0) ? 2 : 1;
nbis = n;
while (nbis /= 10)
rgt = (n < 0) ? (n % 10) * -1 + '0' : n % 10 + '0';
while (n /= 10)
len++;
nbis = n;
nbis *= (nbis < 0) ? -1 : 1;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
str[--len] = nbis % 10 + '0';
while (nbis /= 10)
str[--len] = nbis % 10 + '0';
if (n < 0)
str[--len] = rgt;
while (cpy /= 10)
str[--len] = cpy % 10 + '0';
if (len)
str[0] = '-';
return (str);
}