diff --git a/c_piscine_rush_02/ex00/Makefile b/c_piscine_rush_02/ex00/Makefile new file mode 100644 index 0000000..78c7663 --- /dev/null +++ b/c_piscine_rush_02/ex00/Makefile @@ -0,0 +1,11 @@ +NAME = rush-02 +SRC = *.c +FLAGS = -Wall -Wextra -Werror +all: $(NAME) +RM = /bin/rm -f +$(NAME): + gcc $(FLAGS) -o $(NAME) $(SRC) +clean: + $(RM) *.o +fclean: clean + $(RM) $(NAME) \ No newline at end of file diff --git a/c_piscine_rush_02/ex00/header.h b/c_piscine_rush_02/ex00/header.h new file mode 100644 index 0000000..88a50b5 --- /dev/null +++ b/c_piscine_rush_02/ex00/header.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* header.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/07 21:02:26 by apuchill #+# #+# */ +/* Updated: 2019/12/08 22:19:59 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HEADER_H +# define HEADER_H +# include +# include +# include +# include + +void ft_putchar(char c); +void ft_putstr(char *str); +int ft_strlen(char *str); +int ft_atoi_uni(char nbr); +int ft_msg_error(void); + +int ft_ver_cond(int argc, char **argv); +int ft_ver_nbr(char *argv); +int ft_ver_dict(char *argv); +void ft_get_nbr(int argc, char **argv, char **nbr); +void ft_free(char *filename, char *dict, char *nbr); + +char *ft_dict_file(int argc, char **argv); +char *ft_read_dict(char *filename); +void ft_write_nbr(char *nbr, char *dict); +void ft_write_trio(char *nbr, int len, char *dict); + +void ft_write_uni(char dig, char *dict); +void ft_write_dez(char dig, char *dict); +void ft_write_teens(char dig, char *dict); + +int ft_if_uni(char dig, char *dict, int d); +int ft_if_dez(char dig, char *dict, int d); +int ft_if_teens(char dig, char *dict, int d); + +#endif diff --git a/c_piscine_rush_02/ex00/main.c b/c_piscine_rush_02/ex00/main.c new file mode 100644 index 0000000..e11ced9 --- /dev/null +++ b/c_piscine_rush_02/ex00/main.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/07 15:11:09 by apuchill #+# #+# */ +/* Updated: 2019/12/08 18:44:10 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" +#include + +int main(int argc, char **argv) +{ + char *filename; + char *dict; + char *nbr; + + filename = 0; + dict = 0; + nbr = 0; + if (ft_ver_cond(argc, argv) >= 1) + { + filename = ft_dict_file(argc, argv); + dict = ft_read_dict(filename); + ft_get_nbr(argc, argv, &nbr); + ft_write_nbr(nbr, dict); + ft_free(filename, dict, nbr); + } + else + ft_msg_error(); + write(1, "\n", 1); + return (0); +} diff --git a/c_piscine_rush_02/ex00/numbers.dict b/c_piscine_rush_02/ex00/numbers.dict new file mode 100644 index 0000000..d894010 --- /dev/null +++ b/c_piscine_rush_02/ex00/numbers.dict @@ -0,0 +1,41 @@ +0: zero +1: one +2: two +3: three +4: four +5: five +6: six +7: seven +8: eight +9: nine +10: ten +11: eleven +12: twelve +13: thirteen +14: fourteen +15: fifteen +16: sixteen +17: seventeen +18: eighteen +19: nineteen +20: twenty +30: thirty +40: forty +50: fifty +60: sixty +70: seventy +80: eighty +90: ninety +100: hundred +1000: thousand +1000000: million +1000000000: billion +1000000000000: trillion +1000000000000000: quadrillion +1000000000000000000: quintillion +1000000000000000000000: sextillion +1000000000000000000000000: septillion +1000000000000000000000000000: octillion +1000000000000000000000000000000: nonillion +1000000000000000000000000000000000: decillion +1000000000000000000000000000000000000: undecillion diff --git a/c_piscine_rush_02/ex00/rshbas.c b/c_piscine_rush_02/ex00/rshbas.c new file mode 100644 index 0000000..b36f225 --- /dev/null +++ b/c_piscine_rush_02/ex00/rshbas.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rshbas.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/07 15:22:08 by apuchill #+# #+# */ +/* Updated: 2019/12/08 18:29:22 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putstr(char *str) +{ + int c; + + c = 0; + while (str[c] != '\0') + { + ft_putchar(str[c]); + c++; + } +} + +int ft_strlen(char *str) +{ + int c; + + c = 0; + while (str[c] != '\0') + c++; + return (c); +} + +int ft_atoi_uni(char nbr) +{ + int n; + + n = nbr - ('0'); + return (n); +} + +int ft_msg_error(void) +{ + ft_putstr("Error"); + return (0); +} diff --git a/c_piscine_rush_02/ex00/rshdict.c b/c_piscine_rush_02/ex00/rshdict.c new file mode 100644 index 0000000..3038699 --- /dev/null +++ b/c_piscine_rush_02/ex00/rshdict.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rshdict.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/08 00:38:46 by apuchill #+# #+# */ +/* Updated: 2019/12/08 22:53:04 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +char *ft_dict_file(int argc, char **argv) +{ + char *filename; + char *filestd; + int c; + + filestd = "numbers.dict"; + c = 0; + filename = (char*)malloc(100 * sizeof(char)); + if (argc == 2) + { + while (filestd[c] != '\0') + { + filename[c] = filestd[c]; + c++; + } + filename[c] = '\0'; + } + else + { + while (argv[1][c] != '\0') + { + filename[c] = argv[1][c]; + c++; + } + } + return (filename); +} + +char *ft_read_dict(char *filename) +{ + int dict_open; + int dict_read; + char *dict_aux; + char *dict; + int i; + + i = 0; + dict_aux = (char*)malloc(2000 * sizeof(char)); + dict_open = open(filename, O_RDONLY); + if (dict_open < 0) + { + ft_msg_error(); + return (0); + } + dict_read = read(dict_open, dict_aux, 2000); + dict = (char*)malloc((dict_read + 1) * sizeof(char)); + while (i < dict_read) + { + dict[i] = dict_aux[i]; + i++; + } + dict[i] = '\0'; + free(dict_aux); + close(dict_open); + return (dict); +} + +void ft_write_nbr(char *nbr, char *dict) +{ + int c; + int len; + int div; + + c = 0; + len = ft_strlen(nbr); + ft_write_trio(nbr, len, dict); + while (nbr[c] != '\0') + { + div = len / 3; + c++; + } +} + +void ft_write_trio(char *nbr, int len, char *dict) +{ + int c; + int mod; + + c = 0; + mod = 0; + while (nbr[c] != '\0') + { + mod = len % 3; + if (mod == 2 && nbr[c] == '1') + { + printf("*ft_write_trio: 1º if\n"); + ft_write_teens(nbr[c], dict); + break ; + } + else if (mod == 2 && nbr[c] != '1' && nbr[c + 1] != '0') + { + ft_write_dez(nbr[c], dict); + write(1, " ", 1); + } + if (mod == 1) + ft_write_uni(nbr[c], dict); + c++; + len--; + } +} diff --git a/c_piscine_rush_02/ex00/rshdig.c b/c_piscine_rush_02/ex00/rshdig.c new file mode 100644 index 0000000..d437b3e --- /dev/null +++ b/c_piscine_rush_02/ex00/rshdig.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rshdig.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/08 19:21:14 by apuchill #+# #+# */ +/* Updated: 2019/12/08 23:04:23 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +void ft_write_uni(char dig, char *dict) +{ + int d; + int i; + char write_dig[20]; + + d = 0; + i = 0; + while (dict[d] != '\0') + { + if (ft_if_uni(dig, dict, d) == 1) + { + while (dict[d] != '\n') + { + if (dict[d] != ' ' && dict[d] != ':' && + !(dict[d] >= '0' && dict[d] <= '9')) + { + write_dig[i] = dict[d]; + i++; + } + d++; + } + write_dig[i] = '\0'; + } + d++; + } + ft_putstr(write_dig); +} + +void ft_write_dez(char dig, char *dict) +{ + int d; + int i; + char write_dig[20]; + + d = 0; + i = 0; + while (dict[d] != '\0') + { + if (ft_if_dez(dig, dict, d) == 1) + { + while (dict[d] != '\n') + { + if (dict[d] != ' ' && dict[d] != ':' && + !(dict[d] >= '0' && dict[d] <= '9')) + { + write_dig[i] = dict[d]; + i++; + } + d++; + } + write_dig[i] = '\0'; + } + d++; + } + ft_putstr(write_dig); +} + +void ft_write_teens(char dig, char *dict) +{ + int d; + int i; + char write_dig[200]; + + d = 0; + i = 0; + while (dict[d] != '\0') + { + if (ft_if_teens(dig, dict, d) == 1) + { + while (dict[d] != '\n') + { + if (dict[d] != ' ' && dict[d] != ':' && + !(dict[d] >= '0' && dict[d] <= '9')) + { + write_dig[i] = dict[d]; + i++; + } + d++; + } + write_dig[i] = '\0'; + } + d++; + } + ft_putstr(write_dig); +} diff --git a/c_piscine_rush_02/ex00/rshifs.c b/c_piscine_rush_02/ex00/rshifs.c new file mode 100644 index 0000000..53dc8b1 --- /dev/null +++ b/c_piscine_rush_02/ex00/rshifs.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rshifs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/08 21:52:23 by apuchill #+# #+# */ +/* Updated: 2019/12/08 22:58:54 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +int ft_if_uni(char dig, char *dict, int d) +{ + if (dict[d] == dig && !(dict[d + 1] >= '0' && dict[d + 1] <= '9') && + !(dict[d - 1] >= '0' && dict[d - 1] <= '9')) + return (1); + else + return (0); +} + +int ft_if_dez(char dig, char *dict, int d) +{ + if (dict[d] == dig && (dict[d + 1] == '0') && + !(dict[d + 2] >= '0' && dict[d + 2] <= '9') && + !(dict[d - 1] >= '0' && dict[d - 1] <= '9')) + return (1); + else + return (0); +} + +int ft_if_teens(char dig, char *dict, int d) +{ + if (dict[d] == dig && dict[d] == '1' && + (dict[d + 1] >= '0' && dict[d + 1] <= '9') && + !(dict[d + 2] >= '0' && dict[d + 2] <= '9') && + !(dict[d - 1] >= '0' && dict[d - 1] <= '9')) + return (1); + else + return (0); +} diff --git a/c_piscine_rush_02/ex00/rshini.c b/c_piscine_rush_02/ex00/rshini.c new file mode 100644 index 0000000..8be76b8 --- /dev/null +++ b/c_piscine_rush_02/ex00/rshini.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rshini.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apuchill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/07 15:25:29 by apuchill #+# #+# */ +/* Updated: 2019/12/08 15:03:41 by apuchill ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "header.h" + +int ft_ver_cond(int argc, char **argv) +{ + if (argc == 2 && ft_ver_nbr(argv[1]) == 1) + return (1); + else if (argc == 3 && ft_ver_dict(argv[1]) == 1 && ft_ver_nbr(argv[2]) == 1) + return (2); + else + return (0); +} + +int ft_ver_nbr(char *argv) +{ + int c; + + c = 0; + while (argv[c] != '\0') + { + if (argv[c] >= '0' && argv[c] <= '9') + c++; + else + return (0); + } + return (1); +} + +int ft_ver_dict(char *argv) +{ + int c; + + c = 0; + while (argv[c] != '\0') + { + if ((argv[c] >= '0' && argv[c] <= '9') || + (argv[c] >= 'A' && argv[c] <= 'Z') || (argv[c] == '.') || + (argv[c] >= 'a' && argv[c] <= 'z') || (argv[c] == '_')) + c++; + else + return (0); + } + return (1); +} + +void ft_get_nbr(int argc, char **argv, char **nbr) +{ + int i; + int j; + + j = 0; + *nbr = (char*)malloc(40 * sizeof(char)); + if (ft_ver_cond(argc, argv) == 1) + i = 1; + else + i = 2; + while (argv[i][j] != '\0') + { + nbr[0][j] = argv[i][j]; + j++; + } + nbr[0][j] = '\0'; +} + +void ft_free(char *filename, char *dict, char *nbr) +{ + free(filename); + free(dict); + free(nbr); +} diff --git a/c_piscine_rush_02/ex00/toto.dict b/c_piscine_rush_02/ex00/toto.dict new file mode 100644 index 0000000..e23d808 --- /dev/null +++ b/c_piscine_rush_02/ex00/toto.dict @@ -0,0 +1,41 @@ +0: zéro +1: un +2: deux +3: trois +4: quatre +5: cinq +6: six +7: sept +8: huit +9: neuf +10: dix +11: onze +12: douze +13: treize +14: quatorze +15: quinze +16: seize +17: dix-sept +18: dix-huit +19: dix-neuf +20: vingt +30: trente +40: quarante +50: cinquante +60: soixante +70: soixante-dix +80: quatre-vingts +90: quatre-vingt-dix +100: cent +1000: mille +1000000: million +1000000000: milliard +1000000000000: billion +1000000000000000: billiard +1000000000000000000: trillion +1000000000000000000000: trilliard +1000000000000000000000000: quadrillion +1000000000000000000000000000: quadrilliard +1000000000000000000000000000000: quintillion +1000000000000000000000000000000000: quintilliard +1000000000000000000000000000000000000: sextillion