aboutsummaryrefslogtreecommitdiff
path: root/c07/ex04
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-15 08:15:37 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-15 08:15:37 +0200
commit3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (patch)
tree25b02c02f5140dbefbabd7720f292d8be3d5cc51 /c07/ex04
parentc2bf9fcefbb4453cee271ccd1af9674ad2f3a181 (diff)
downloadpiscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.gz
piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.bz2
piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.zip
c07 passed, c08 in progress, rush01(+ 6x6 try)
Diffstat (limited to 'c07/ex04')
-rw-r--r--c07/ex04/ft_convert_base.c130
-rw-r--r--c07/ex04/ft_convert_base2.c69
2 files changed, 102 insertions, 97 deletions
diff --git a/c07/ex04/ft_convert_base.c b/c07/ex04/ft_convert_base.c
index f30c3e5..0abdbe8 100644
--- a/c07/ex04/ft_convert_base.c
+++ b/c07/ex04/ft_convert_base.c
@@ -6,118 +6,60 @@
/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/08 16:16:53 by cacharle #+# #+# */
-/* Updated: 2019/07/10 06:37:09 by cacharle ### ########.fr */
+/* Updated: 2019/07/12 12:07:28 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-int check_base(char *base)
-{
- int i;
- int j;
-
- i = 0;
- while (base[i])
- {
- if (base[i] == '-' || base[i] == '+' || base[i] == ' '
- || base[i] == '\t' || base[i] == '\n' || base[i] == '\v'
- || base[i] == '\f' || base[i] == '\r')
- return (0);
- j = 0;
- while (base[j])
- {
- if (j != i && base[j] == base[i])
- return (0);
- j++;
- }
- i++;
- }
- if (i < 2)
- return (0);
- return (1);
-}
-
-int ft_pow(int base, int exponent)
-{
- int accumulator;
-
- accumulator = 1;
- while (exponent-- > 0)
- accumulator *= base;
- return (accumulator);
-}
-
-
-int position_in_base(char digit, char *base)
-{
- int i;
+#include <stdlib.h>
- i = 0;
- while (base[i] != digit)
- i++;
- return (i);
-}
+int check_base(char *base);
+int ft_pow(int base, int exponent);
+int position_in_base(char digit, char *base);
+int some_strlen(char *str);
-// CHECK THE STR NOT JUST THE BASE (you idiot)
-int ft_atoi_base(char *str, char *base)
+int from_base(char *str, char *base)
{
int radix;
int i;
int j;
int nb;
+ int is_negative;
- if (!check_base(base))
- return (0);
+ while (*str == ' ' || *str == '\t' || *str == '\n'
+ || *str == '\v' || *str == '\f' || *str == '\r')
+ str++;
+ is_negative = 0;
+ while (*str == '-' || *str == '+')
+ if (*str++ == '-')
+ is_negative = !is_negative;
nb = 0;
radix = 0;
while (base[radix])
radix++;
i = 0;
- while (str[i])
+ while (position_in_base(str[i], base) != -1)
i++;
j = 0;
while (--i >= 0)
- {
- nb += ft_pow(radix, i) * position_in_base(str[j], base);
- j++;
- }
- return (nb);
-}
-
-int some_strlen(char *str)
-{
- int counter;
-
- counter = 0;
- while (str[counter])
- counter++;
- return (counter);
+ nb += ft_pow(radix, i) * position_in_base(str[j++], base);
+ return (is_negative ? -nb : nb);
}
-char *to_base(int nbr, char *base)
+char *to_base(int nbr, char *base, int radix, int is_negative)
{
- int radix;
int i;
+ int j;
unsigned int nbu;
char rev_digits[1024];
char *ret;
- int j;
- int is_negative;
- /*if (!check_base(base))*/
- /*return (NULL);*/
- radix = some_strlen(base);
nbu = nbr;
- is_negative = 0;
- if (nbr < 0)
- {
- is_negative = 1;
+ if (is_negative)
nbu = -nbr;
- }
- i = 0;
- printf("%d %s %d\n", radix, base, nbr);
+ i = nbu == 0 ? 1 : 0;
+ rev_digits[0] = base[0];
while (nbu > 0)
{
- printf("%u\n", nbu % radix);
rev_digits[i] = base[nbu % radix];
nbu /= radix;
i++;
@@ -125,27 +67,21 @@ char *to_base(int nbr, char *base)
ret = malloc(sizeof(char) * i + (is_negative ? 1 : 0));
j = 0;
if (is_negative)
- {
ret[0] = '-';
- j++;
- }
- while (i-- > 0)
- ret[j++] = rev_digits[i];
+ while (i-- + (is_negative ? 1 : 0) > 0)
+ ret[j++ + (is_negative ? 1 : 0)] = rev_digits[i];
ret[j] = '\0';
- for (int i = 0; i < 4; i++)
- printf("%c ", rev_digits[i]);
- printf("\n");
- for (int i = 0; i < 4; i++)
- printf("%c ", ret[i]);
- printf("\n");
return (ret);
}
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
- int converted_nb;
-
- int nb;
- return NULL;
-
+ int nb;
+ char *parsed_nbr;
+
+ if (!check_base(base_from) || !check_base(base_to))
+ return (NULL);
+ parsed_nbr = nbr;
+ nb = from_base(parsed_nbr, base_from);
+ return (to_base(nb, base_to, some_strlen(base_to), nb < 0));
}
diff --git a/c07/ex04/ft_convert_base2.c b/c07/ex04/ft_convert_base2.c
index e69de29..be9a03e 100644
--- a/c07/ex04/ft_convert_base2.c
+++ b/c07/ex04/ft_convert_base2.c
@@ -0,0 +1,69 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_convert_base2.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/10 20:22:09 by cacharle #+# #+# */
+/* Updated: 2019/07/11 10:10:08 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+int check_base(char *base)
+{
+ int i;
+ int j;
+
+ i = 0;
+ while (base[i])
+ {
+ if (base[i] == '-' || base[i] == '+' || base[i] == ' '
+ || base[i] == '\t' || base[i] == '\n' || base[i] == '\v'
+ || base[i] == '\f' || base[i] == '\r')
+ return (0);
+ j = 0;
+ while (base[j])
+ {
+ if (j != i && base[j] == base[i])
+ return (0);
+ j++;
+ }
+ i++;
+ }
+ if (i < 2)
+ return (0);
+ return (1);
+}
+
+int ft_pow(int base, int exponent)
+{
+ int accumulator;
+
+ accumulator = 1;
+ while (exponent-- > 0)
+ accumulator *= base;
+ return (accumulator);
+}
+
+int position_in_base(char digit, char *base)
+{
+ int i;
+
+ i = 0;
+ while (digit != base[i] && base[i])
+ i++;
+ if (base[i] == '\0')
+ return (-1);
+ return (i);
+}
+
+int some_strlen(char *str)
+{
+ int counter;
+
+ counter = 0;
+ while (str[counter])
+ counter++;
+ return (counter);
+}