0
0
This commit is contained in:
Guy Baconniere
2021-08-16 16:51:24 +02:00
parent 0909750997
commit ef1c6b5e75
16 changed files with 332 additions and 10 deletions

View File

@@ -6,7 +6,7 @@
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/13 12:46:22 by gbaconni #+# #+# */
/* Updated: 2021/08/13 14:58:30 by gbaconni ### ########.fr */
/* Updated: 2021/08/16 13:49:34 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,23 +17,21 @@ int ft_atoi(char *str)
int i;
nb = 0;
s = 0;
s = 1;
i = 0;
while (str[i] != '\0' && (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r')))
i++;
while (str[i] != '\0')
{
if (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r'))
if (str[i] == '+')
s += 0;
else if (str[i] == '+')
s++;
else if (str[i] == '-')
s--;
s *= -1;
else if (str[i] >= '0' && str[i] <= '9')
nb = nb * 10 + str[i] - '0';
else
break ;
i++;
}
if (s < 0)
nb *= -1;
return (nb);
return (nb * s);
}

View File

@@ -6,7 +6,7 @@
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/13 12:46:41 by gbaconni #+# #+# */
/* Updated: 2021/08/13 13:58:09 by gbaconni ### ########.fr */
/* Updated: 2021/08/16 13:44:04 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,5 +26,7 @@ int main(void)
scanf("%s", str);
result = ft_atoi(str);
printf("str=%s result=%d (ft_atoi)\n", str, result);
result = atoi(str);
printf("str=%s result=%d (atoi)\n", str, result);
return (0);
}

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 13:51:16 by gbaconni #+# #+# */
/* Updated: 2021/08/16 14:25:07 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
void ft_putnbr_base(int nbr, char *base)
{
int size;
size = 0;
while (base[++size] != '\0')
continue ;
if (nbr < 0)
{
nbr *= -1;
write(1, "-", 1);
}
if (nbr >= 0 && nbr < size)
{
write(1, base + nbr, 1);
}
else if (nbr == -2147483648)
{
ft_putnbr_base((nbr / size) * -1, base);
ft_putnbr_base((nbr % size) * -1, base);
}
else
{
ft_putnbr_base(nbr / size, base);
ft_putnbr_base(nbr % size, base);
}
}

View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 13:53:07 by gbaconni #+# #+# */
/* Updated: 2021/08/16 13:55:59 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void ft_putnbr_base(int nbr, char *base);
int main(void)
{
char base[32];
int nb;
printf("Input Number: ");
scanf("%d", &nb);
printf("Input Base [31]: ");
scanf("%s", base);
ft_putnbr_base(nb, base);
write(1, "\n", 1);
printf("nb=%d base=%s (ft_putnbr_base)\n", nb, base);
return (0);
}

View File

@@ -0,0 +1,8 @@
#!/bin/sh
set -e
#norminette -R CheckForbiddenSourceHeader ft_*.c
norminette -R CheckForbiddenSourceHeader
gcc -Wall -Wextra -Werror -o a.out *.c
echo $(basename $PWD):
./a.out
rm -f a.out

View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 13:53:46 by gbaconni #+# #+# */
/* Updated: 2021/08/16 15:05:08 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_strlen(char *str)
{
int size;
size = 0;
while (str[++size] != '\0')
continue ;
return (size);
}
static int ft_nbase(char c, char *base)
{
int n;
n = 0;
while (base[n] != '\0' && base[n] != c)
n++;
return (n);
}
int ft_atoi_base(char *str, char *base)
{
int nb;
int s;
int i;
int size;
size = ft_strlen(base);
nb = 0;
s = 1;
i = 0;
while (str[i] != '\0' && (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r')))
i++;
while (str[i] != '\0')
{
if (str[i] == '+')
s += 0;
else if (str[i] == '-')
s *= -1;
else
nb = nb * size + ft_nbase(str[i], base);
i++;
}
return (nb * s);
}

View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/16 13:53:58 by gbaconni #+# #+# */
/* Updated: 2021/08/16 13:55:18 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int ft_atoi_base(char *str, char *base);
int main(void)
{
char str[32];
char base[32];
int result;
printf("Input String [31]: ");
scanf("%s", str);
printf("Input Base [31]: ");
scanf("%s", base);
result = ft_atoi_base(str, base);
printf("str=%s base=%s result=%d (ft_atoi_base)\n", str, base, result);
return (0);
}

View File

@@ -0,0 +1,8 @@
#!/bin/sh
set -e
#norminette -R CheckForbiddenSourceHeader ft_*.c
norminette -R CheckForbiddenSourceHeader
gcc -Wall -Wextra -Werror -o a.out *.c
echo $(basename $PWD):
./a.out
rm -f a.out