Ex3 and Ex4 Finished
This commit is contained in:
@@ -6,23 +6,23 @@
|
|||||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/16 16:37:56 by lclerel- #+# #+# */
|
/* Created: 2026/03/16 16:37:56 by lclerel- #+# #+# */
|
||||||
/* Updated: 2026/03/16 18:16:42 by lclerel- ### ########.fr */
|
/* Updated: 2026/03/17 15:13:51 by lclerel- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int ft_strlen(char *str)
|
int ft_strlen(char *str)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_total_len(int size, char **strs, char *sep)
|
int get_total_len(int size, char **strs, char *sep)
|
||||||
@@ -44,24 +44,24 @@ int get_total_len(int size, char **strs, char *sep)
|
|||||||
return (total);
|
return (total);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ft_strcat(char *dest, char *src)
|
char *ft_strcat(char *dest, char *src)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
j = 0;
|
||||||
while (dest[i])
|
while (dest[i])
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
while (src[j])
|
while (src[j])
|
||||||
{
|
{
|
||||||
dest[i + j] = src[j];
|
dest[i + j] = src[j];
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
dest[i + j] = '\0';
|
dest[i + j] = '\0';
|
||||||
return (dest);
|
return (dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ft_strjoin(int size, char **strs, char *sep)
|
char *ft_strjoin(int size, char **strs, char *sep)
|
||||||
@@ -75,32 +75,25 @@ char *ft_strjoin(int size, char **strs, char *sep)
|
|||||||
{
|
{
|
||||||
res = malloc(1);
|
res = malloc(1);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
|
||||||
res[0] = '\0';
|
res[0] = '\0';
|
||||||
}
|
|
||||||
return (res);
|
return (res);
|
||||||
|
|
||||||
}
|
}
|
||||||
len = get_total_len(size, strs, sep);
|
len = get_total_len(size, strs, sep);
|
||||||
res = malloc(sizeof(char) * (len + 1));
|
res = malloc(sizeof(char) * (len + 1));
|
||||||
if (!res)
|
if (!res)
|
||||||
{
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
|
||||||
res[0] = '\0';
|
res[0] = '\0';
|
||||||
while (i < size)
|
while (i < size)
|
||||||
{
|
{
|
||||||
ft_strcat(res, strs[i]);
|
ft_strcat(res, strs[i]);
|
||||||
if (i < size - 1)
|
if (i < size - 1)
|
||||||
{
|
|
||||||
ft_strcat(res, sep);
|
ft_strcat(res, sep);
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_putchar(char chara)
|
/*void ft_putchar(char chara)
|
||||||
{
|
{
|
||||||
write(1, &chara, 1);
|
write(1, &chara, 1);
|
||||||
}
|
}
|
||||||
@@ -137,4 +130,4 @@ int main(void)
|
|||||||
free(result);
|
free(result);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}*/
|
||||||
|
|||||||
@@ -0,0 +1,201 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_convert_base.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/03/17 14:20:07 by lclerel- #+# #+# */
|
||||||
|
/* Updated: 2026/03/17 19:19:00 by lclerel- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void ft_putstr(char *str);
|
||||||
|
|
||||||
|
int ft_strlen(char *str);
|
||||||
|
|
||||||
|
int ft_nbrlen(long nbr, int base_size);
|
||||||
|
|
||||||
|
int check_base(char *base);
|
||||||
|
|
||||||
|
int ft_index(char c, char *base)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (base[i])
|
||||||
|
{
|
||||||
|
if (base[i] == c)
|
||||||
|
return (i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_atoi_base(char *str, char *base)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int sign;
|
||||||
|
long res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
i = 0;
|
||||||
|
sign = 1;
|
||||||
|
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
|
||||||
|
i++;
|
||||||
|
while (str[i] == '-' || str[i] == '+')
|
||||||
|
{
|
||||||
|
if (str[i++] == '-')
|
||||||
|
sign *= -1;
|
||||||
|
}
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] && ft_index(str[i], base) != -1)
|
||||||
|
{
|
||||||
|
res = (res * ft_strlen(base)) + ft_index(str[i], base);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (res * sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_populate_res(char *res, long n, char *base_to, int size)
|
||||||
|
{
|
||||||
|
int base_len;
|
||||||
|
|
||||||
|
base_len = ft_strlen(base_to);
|
||||||
|
res[size] = '\0';
|
||||||
|
if (n == 0)
|
||||||
|
res[0] = base_to[0];
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
res[0] = '-';
|
||||||
|
n = -n;
|
||||||
|
}
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
res[size - 1] = base_to[n % base_len];
|
||||||
|
n /= base_len;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
|
||||||
|
{
|
||||||
|
long n;
|
||||||
|
char *res;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
if (!nbr || !base_from || !base_to)
|
||||||
|
return (NULL);
|
||||||
|
if (!check_base(base_from) || !check_base(base_to))
|
||||||
|
return (NULL);
|
||||||
|
n = ft_atoi_base(nbr, base_from);
|
||||||
|
size = ft_nbrlen(n, ft_strlen(base_to));
|
||||||
|
res = malloc(sizeof(char) * (size + 1));
|
||||||
|
if (!res)
|
||||||
|
return (NULL);
|
||||||
|
ft_populate_res(res, n, base_to, size);
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
char *hex;
|
||||||
|
char *bin;
|
||||||
|
char *dec;
|
||||||
|
char *vif;
|
||||||
|
|
||||||
|
hex = "0123456789abcdef";
|
||||||
|
dec = "0123456789";
|
||||||
|
bin = "01";
|
||||||
|
vif = "poneyvif";
|
||||||
|
// Test 1 : 42 > Dec to Bin
|
||||||
|
ft_putstr("Test avec 42");
|
||||||
|
ft_putstr("Base From : Decimal (Base 10), Base To : Binary (Base 2)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base("42", dec, bin);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur.", 7);
|
||||||
|
}
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
// Test 2 : 2a (42) > Hex to Vif
|
||||||
|
ft_putstr("Test avec 42 (2a)");
|
||||||
|
ft_putstr("Base From : Hexadecimal (Base 16), Base To : poneyvif (Base 8)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base("2a", hex, vif);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur.", 7);
|
||||||
|
}
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
// Test 3 : 0 > Dec to Bin
|
||||||
|
ft_putstr("Test avec 0");
|
||||||
|
ft_putstr("Base From : Decimal (Base 10), Base To : Binary (Base 2)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base("0", dec, bin);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur.", 7);
|
||||||
|
}
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
// Test 4 : -2147483648 > Dec to Hex
|
||||||
|
ft_putstr("Test avec -2147483648")
|
||||||
|
ft_putstr("Base From : Decimal (Base 10), Base To : Hexadecimal (Base 16)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base("-2147483648", dec, hex);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur.", 7);
|
||||||
|
}
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
// Test 5 : Bordel Ultime > Binary to Dec
|
||||||
|
ft_putstr("Test avec ---+--101010");
|
||||||
|
ft_putstr("Base From : Binary (Base 2), Base To : Decimal (Base 10)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base(" ---+--101010", bin, dec);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur.", 7);
|
||||||
|
}
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
// Test 6 : NULL > Binary to Dec
|
||||||
|
ft_putstr("Test avec NULL.");
|
||||||
|
ft_putstr("Base From : Binary (Base 2), Base To : Decimal (Base 10)");
|
||||||
|
write(1, "\n", 1);
|
||||||
|
result = ft_convert_base(NULL, bin, dec);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
write(1, "Erreur. NULL est mis comme variable", 35);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_putstr("Resultat : ");
|
||||||
|
ft_putstr(result);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}*/
|
||||||
|
|||||||
82
ex4/ft_convert_base2.c
Normal file
82
ex4/ft_convert_base2.c
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_convert_base2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/03/17 14:22:20 by lclerel- #+# #+# */
|
||||||
|
/* Updated: 2026/03/17 18:16:30 by lclerel- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int ft_strlen(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putstr(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
write(1, &str[i], 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_nbrlen(long nbr, int base_size)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
if (nbr <= 0)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
if (nbr < 0)
|
||||||
|
nbr = -nbr;
|
||||||
|
}
|
||||||
|
while (nbr > 0)
|
||||||
|
{
|
||||||
|
nbr /= base_size;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_base(char *base)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!base[0] || !base[1])
|
||||||
|
return (0);
|
||||||
|
while (base[i])
|
||||||
|
{
|
||||||
|
if (base[i] == '+' || base[i] == '-'
|
||||||
|
|| (base[i] >= 9 && base[i] <= 13)
|
||||||
|
|| base[i] == 32)
|
||||||
|
return (0);
|
||||||
|
j = i + 1;
|
||||||
|
while (base[j])
|
||||||
|
{
|
||||||
|
if (base[i] == base[j])
|
||||||
|
return (0);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user