sync
This commit is contained in:
BIN
C_Piscine_C_04/c-04.tar
Normal file
BIN
C_Piscine_C_04/c-04.tar
Normal file
Binary file not shown.
@@ -6,7 +6,7 @@
|
|||||||
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
|
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/08/13 12:46:22 by gbaconni #+# #+# */
|
/* 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;
|
int i;
|
||||||
|
|
||||||
nb = 0;
|
nb = 0;
|
||||||
s = 0;
|
s = 1;
|
||||||
i = 0;
|
i = 0;
|
||||||
|
while (str[i] != '\0' && (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r')))
|
||||||
|
i++;
|
||||||
while (str[i] != '\0')
|
while (str[i] != '\0')
|
||||||
{
|
{
|
||||||
if (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r'))
|
if (str[i] == '+')
|
||||||
s += 0;
|
s += 0;
|
||||||
else if (str[i] == '+')
|
|
||||||
s++;
|
|
||||||
else if (str[i] == '-')
|
else if (str[i] == '-')
|
||||||
s--;
|
s *= -1;
|
||||||
else if (str[i] >= '0' && str[i] <= '9')
|
else if (str[i] >= '0' && str[i] <= '9')
|
||||||
nb = nb * 10 + str[i] - '0';
|
nb = nb * 10 + str[i] - '0';
|
||||||
else
|
else
|
||||||
break ;
|
break ;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (s < 0)
|
return (nb * s);
|
||||||
nb *= -1;
|
|
||||||
return (nb);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
|
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/08/13 12:46:41 by gbaconni #+# #+# */
|
/* 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);
|
scanf("%s", str);
|
||||||
result = ft_atoi(str);
|
result = ft_atoi(str);
|
||||||
printf("str=%s result=%d (ft_atoi)\n", str, result);
|
printf("str=%s result=%d (ft_atoi)\n", str, result);
|
||||||
|
result = atoi(str);
|
||||||
|
printf("str=%s result=%d (atoi)\n", str, result);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
42
C_Piscine_C_04/git/ex04/ft_putnbr_base.c
Normal file
42
C_Piscine_C_04/git/ex04/ft_putnbr_base.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
C_Piscine_C_04/git/ex04/main.c
Normal file
33
C_Piscine_C_04/git/ex04/main.c
Normal 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);
|
||||||
|
}
|
||||||
8
C_Piscine_C_04/git/ex04/main.sh
Executable file
8
C_Piscine_C_04/git/ex04/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
|
||||||
57
C_Piscine_C_04/git/ex05/ft_atoi_base.c
Normal file
57
C_Piscine_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_Piscine_C_04/git/ex05/main.c
Normal file
33
C_Piscine_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_Piscine_C_04/git/ex05/main.sh
Executable file
8
C_Piscine_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
|
||||||
12
C_Piscine_C_04/tests_C04/ex00.c
Normal file
12
C_Piscine_C_04/tests_C04/ex00.c
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex00/ft_strlen.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *a = "12345678";
|
||||||
|
char b[1];
|
||||||
|
|
||||||
|
b[0] = 0;
|
||||||
|
printf("Test\nexpected : 8 | result : %d\n", ft_strlen(a));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_strlen(b));
|
||||||
|
}
|
||||||
15
C_Piscine_C_04/tests_C04/ex01.c
Normal file
15
C_Piscine_C_04/tests_C04/ex01.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex01/ft_putstr.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char b[1];
|
||||||
|
char *a = "12345678";
|
||||||
|
|
||||||
|
b[0] = 0;
|
||||||
|
printf("\nTest\nexpected : %s | result : \n", a);
|
||||||
|
ft_putstr(a);
|
||||||
|
printf("\nTest\nexpected : %s | result : \n", b);
|
||||||
|
ft_putstr(b);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
15
C_Piscine_C_04/tests_C04/ex02.c
Normal file
15
C_Piscine_C_04/tests_C04/ex02.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex02/ft_putnbr.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
printf("\nTest\nexpected : 1230234 | result : \n");
|
||||||
|
ft_putnbr(1230234);
|
||||||
|
printf("\nTest\nexpected : 0 | result : \n");
|
||||||
|
ft_putnbr(0);
|
||||||
|
printf("\nTest\nexpected : -253 | result : \n");
|
||||||
|
ft_putnbr(-253);
|
||||||
|
printf("\nTest\nexpected : -2147483648 | result : \n");
|
||||||
|
ft_putnbr(-2147483648);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
17
C_Piscine_C_04/tests_C04/ex03.c
Normal file
17
C_Piscine_C_04/tests_C04/ex03.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex03/ft_atoi.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char a[] = " \n \r \v +---+-+-42";
|
||||||
|
char b[] = " \n --+37523093";
|
||||||
|
char c[] = " 0";
|
||||||
|
char d[] = " \f \t -+2147483648";
|
||||||
|
char e[] = " \f\f asd345";
|
||||||
|
|
||||||
|
printf("Test\nexpected : -42 | result : %d\n", ft_atoi(a));
|
||||||
|
printf("Test\nexpected : 37523093 | result : %d\n", ft_atoi(b));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_atoi(c));
|
||||||
|
printf("Test\nexpected : -2147483648 | result : %d\n", ft_atoi(d));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_atoi(e));
|
||||||
|
}
|
||||||
31
C_Piscine_C_04/tests_C04/ex04.c
Normal file
31
C_Piscine_C_04/tests_C04/ex04.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex04/ft_putnbr_base.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char poney[] = "poneyvif";
|
||||||
|
char deci[] = "0123456789";
|
||||||
|
char hexa[] = "0123456789abcdef";
|
||||||
|
char bin[] = "01";
|
||||||
|
char strange[] = "102310";
|
||||||
|
char strange2[] = "1234+56";
|
||||||
|
char strange3[] = "";
|
||||||
|
|
||||||
|
printf("\nTest\nexpected : 128 | result : \n");
|
||||||
|
ft_putnbr_base(128, deci);
|
||||||
|
printf("\nTest\nexpected : -2b | result : \n");
|
||||||
|
ft_putnbr_base(-43, hexa);
|
||||||
|
printf("\nTest\nexpected : -80000000 | result : \n");
|
||||||
|
ft_putnbr_base(-2147483648, hexa);
|
||||||
|
printf("\nTest\nexpected : 1101 | result : \n");
|
||||||
|
ft_putnbr_base(13, bin);
|
||||||
|
printf("\nTest\nexpected : opoooyyf | result : \n");
|
||||||
|
ft_putnbr_base(2134823, poney);
|
||||||
|
printf("\nTest\nexpected : | result : \n");
|
||||||
|
ft_putnbr_base(2134823, strange);
|
||||||
|
printf("\nTest\nexpected : | result : \n");
|
||||||
|
ft_putnbr_base(4325, strange2);
|
||||||
|
printf("\nTest\nexpected : | result : \n");
|
||||||
|
ft_putnbr_base(21348, strange3);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
43
C_Piscine_C_04/tests_C04/ex05.c
Normal file
43
C_Piscine_C_04/tests_C04/ex05.c
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ex05.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tkondrac <tkondrac@student.42lausan> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/08/11 14:32:06 by tkondrac #+# #+# */
|
||||||
|
/* Updated: 2021/08/11 17:34:21 by tkondrac ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../ex05/ft_atoi_base.c"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char a[] = " \n \r \v +---+-+-42";
|
||||||
|
char b[] = " \n --+2b";
|
||||||
|
char c[] = " 0";
|
||||||
|
char d[] = " \f \t -+2147483648";
|
||||||
|
char e[] = " \f\f asd345";
|
||||||
|
char f[] = " \f\f poneyvif";
|
||||||
|
char g[] = " \f\f -0123456789";
|
||||||
|
|
||||||
|
char poney[] = "poneyvif";
|
||||||
|
char deci[] = "0123456789";
|
||||||
|
char hexa[] = "0123456789abcdef";
|
||||||
|
char bin[] = "01";
|
||||||
|
char strange[] = "1b231b";
|
||||||
|
// char strange2[] = "1234+56";
|
||||||
|
// char strange3[] = "";
|
||||||
|
|
||||||
|
printf("Test\nexpected : -42 | result : %d\n", ft_atoi_base(a, deci));
|
||||||
|
printf("Test\nexpected : 43 | result : %d\n", ft_atoi_base(b, hexa));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_atoi_base(c, bin));
|
||||||
|
printf("Test\nexpected : -2147483648 | result : %d\n", ft_atoi_base(d, deci));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_atoi_base(e, deci));
|
||||||
|
printf("Test\nexpected : 2 | result : %d\n", ft_atoi_base(b, deci));
|
||||||
|
printf("Test\nexpected : 342391 | result : %d\n", ft_atoi_base(f, poney));
|
||||||
|
printf("Test\nexpected : -123456789 | result : %d\n", ft_atoi_base(g, deci));
|
||||||
|
printf("Test\nexpected : 0 | result : %d\n", ft_atoi_base(a, strange));
|
||||||
|
}
|
||||||
8
C_Piscine_C_04/tests_C04/main.sh
Executable file
8
C_Piscine_C_04/tests_C04/main.sh
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
test=$1
|
||||||
|
set -e
|
||||||
|
norminette -R CheckForbiddenSourceHeader ../$test/ft_*.c
|
||||||
|
gcc -Wall -Wextra -Werror -o a.out $test.c
|
||||||
|
#gcc -o a.out $test.c
|
||||||
|
./a.out
|
||||||
|
rm -f a.out
|
||||||
Reference in New Issue
Block a user