Save ft_convert_base.c

This commit is contained in:
tosu 2023-03-30 21:52:55 +02:00
parent 63562f4c1b
commit 1bccb37c68
1 changed files with 144 additions and 21 deletions

View File

@ -1,6 +1,138 @@
#include <unistd.h>
#include <limits.h>
unsigned int char_count(const char *str, char c)
{
unsigned int count;
count = 0;
while (*str)
{
if (*str == c)
++count;
++str;
}
return (count);
}
// Return 0 if the base is invalid or the length of the base otherwise
unsigned int is_valid_base(const char *base, int space_ok)
{
unsigned int size;
size = 0;
while (*base)
{
if (*base == '+' || *base == '-' || (*base == ' ' && !space_ok))
return (0);
if (char_count(base, *base) > 1)
return (0);
++size;
++base;
}
if (size < 2)
return (0);
return (size);
}
void putdigit_from_base(unsigned int digit_idx, const char *base)
{
write(1, &base[digit_idx], 1);
}
void _ft_putnbr_base(int nbr, const char *base, unsigned int base_len)
{
char digit_idx;
// unsigned int base_len will make signed nbr positive
digit_idx = nbr % base_len;
if (nbr > (int) base_len - 1)
{
_ft_putnbr_base(nbr / base_len, base, base_len);
putdigit_from_base(digit_idx, base);
}
else if (nbr == INT_MIN)
{
_ft_putnbr_base(-(nbr / base_len), base, base_len);
putdigit_from_base(digit_idx, base);
}
else if (nbr < 0)
{
write(1, "-", 1);
_ft_putnbr_base(-nbr, base, base_len);
}
else
putdigit_from_base(digit_idx, base);
}
int max_result_len(char *nbr, char *base_from, char *base_to)
{
return (100);
}
char *ft_to_base(int nbr, char *base_from, char *base_to)
{
unsigned int base_len;
char *result;
base_len = is_valid_base(base_to, 1);
if (!base_len)
return ((void *) 0);
result = malloc(sizeof(char) * max_result_len(nbr, base_from, base_to));
_ft_putnbr_base(nbr, base_to, base_len, result);
return (result);
}
int char_index(const char *str, const char c)
{
int idx;
idx = 0;
while (*str)
{
if (*str++ == c)
return (idx);
++idx;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int sign;
int abs;
int index_in_base;
unsigned int base_len;
abs = 0;
sign = 1;
base_len = is_valid_base(base, 0);
if (!base_len)
return (0);
while (*str == ' '
|| *str == '\t'
|| *str == '\n'
|| *str == '\r'
|| *str == '\f'
|| *str == '\v')
++str;
while (*str == '+'
|| *str == '-')
if (*str++ == '-')
sign *= -1;
index_in_base = char_index(base, *str);
while (index_in_base >= 0)
{
abs = abs * base_len + index_in_base;
++str;
index_in_base = char_index(base, *str);
}
return (sign * abs);
}
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
return (ft_to_base(ft_atoi_base(nbr, base_from), base_from, base_to));
}
#include <stdio.h>
@ -16,22 +148,23 @@ char *ft_convert_base(char *nbr, char *base_from, char *base_to)
#define FROM_BASE_2 "0123456789"
#define TO_BASE_2 "01"
#define FROM_BASE_2 "01"
#define TO_BASE_2 "0123456789"
#define FROM_BASE_3 "0123456789"
#define TO_BASE_3 "0123456789abcdef"
#define FROM_BASE_3 "01"
#define TO_BASE_3 "0123456789abcdef"
#define TO_BASE_3 "0123456789"
#define FROM_BASE_4 "0123456789abcdef"
#define TO_BASE_4 "01"
#define FROM_BASE_4 "0123456789"
#define TO_BASE_4 "0123456789abcdef"
#define FROM_BASE_5 "01"
#define TO_BASE_5 "0123456789abcdef"
#define FROM_BASE_6 "0123456789abcdef"
#define TO_BASE_6 "01"
int main(void)
{
char *from_base;
char *to_base;
char *output;
char *nbr;
from_base = FROM_BASE_2;
@ -39,17 +172,7 @@ int main(void)
printf("FROM BASE: %s, TO BASE: %s\n", from_base, to_base);
// nbr = "-2"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
// nbr = "-1"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "0"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "1"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "2"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "3"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "4"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "5"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "6"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "7"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "8"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "9"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "10"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "0"; output = ft_convert_base(nbr, from_base, to_base); printf("Input: <%s>, Output: <%s>\n", nbr, output); free(output);
printf("\n");
return (0);