piscine-c07/ex04/ft_convert_base.c

421 lines
13 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 22:35:01 by tosuman #+# #+# */
/* Updated: 2023/03/30 22:35:02 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include <stdlib.h>
#define MAX_OUTPUT_LEN 100
char *ft_strcat(char *dest, char *src);
int char_index(const char *str, const char c);
unsigned int char_count(const char *str, char c);
unsigned int is_valid_base(char *base, int space_ok);
void strcat_base_digit(unsigned int digit_idx, char *base, char *result)
{
char digit[2];
digit[0] = base[digit_idx];
digit[1] = 0;
ft_strcat(result, digit);
}
void _ft_to_base(int nbr, char *base, unsigned int base_len, char *result)
{
char digit_idx;
digit_idx = nbr % base_len;
if (nbr > (int) base_len - 1)
{
_ft_to_base(nbr / base_len, base, base_len, result);
strcat_base_digit(digit_idx, base, result);
}
else if (nbr == INT_MIN)
{
_ft_to_base(-(nbr / base_len), base, base_len, result);
strcat_base_digit(digit_idx, base, result);
}
else if (nbr < 0)
{
ft_strcat(result, "-");
_ft_to_base(-nbr, base, base_len, result);
}
else
strcat_base_digit(digit_idx, base, result);
}
char *ft_to_base(int nbr, char *base_to)
{
unsigned int base_len;
char *result;
int idx;
base_len = is_valid_base(base_to, 1);
if (!base_len)
return (NULL);
result = malloc(sizeof(char) * MAX_OUTPUT_LEN);
idx = -1;
while (++idx < MAX_OUTPUT_LEN)
result[idx] = 0;
_ft_to_base(nbr, base_to, base_len, result);
return (result);
}
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_to));
}
/* ////
#include <stdio.h>
#define INVALID_BASE_1 "01234567890"
#define INVALID_BASE_2 "010"
#define INVALID_BASE_3 "0123456789 "
#define INVALID_BASE_4 "01 "
#define BASE10 "0123456789"
#define BASE2 "01"
#define BASE16 "0123456789abcdef"
int main(void)
{
char *from_base;
char *to_base;
char *output;
char *nbr;
from_base = BASE10;
to_base = BASE10;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-2147483648";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "3";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "4";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "5";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
from_base = BASE10;
to_base = BASE2;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-2147483648";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "3";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "4";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "5";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
from_base = BASE2;
to_base = BASE10;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-10000000000000000000000000000000";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1111111111111111111111111111111";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-10";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "10";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "11";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "100";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "101";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1111111111111111111111111111111";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
from_base = BASE2;
to_base = BASE16;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-10000000000000000000000000000000";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1111111111111111111111111111111";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-10";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "10";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "11";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "100";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "101";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1111111111111111111111111111111";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
from_base = BASE16;
to_base = BASE2;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-80000000";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-7fffffff";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "3";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "4";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "5";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "7fffffff";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
from_base = BASE10;
to_base = BASE16;
printf("FROM BASE: '%s', TO BASE: '%s'\n", from_base, to_base);
nbr = "-2147483648";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "-1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "0";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "1";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "3";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "4";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "5";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
nbr = "2147483647";
output = ft_convert_base(nbr, from_base, to_base);
printf("Input: <%s>, Output: <%s>\n", nbr, output);
free(output);
printf("\n");
return (0);
}
*/ ////