commit
This commit is contained in:
57
C_04/git/ex05/ft_atoi_base.c
Normal file
57
C_04/git/ex05/ft_atoi_base.c
Normal 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);
|
||||
}
|
||||
33
C_04/git/ex05/main.c
Normal file
33
C_04/git/ex05/main.c
Normal 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);
|
||||
}
|
||||
8
C_04/git/ex05/main.sh
Executable file
8
C_04/git/ex05/main.sh
Executable 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
|
||||
Reference in New Issue
Block a user