Skip to content

Commit

Permalink
C Piscine Rush 02 - fail 10% (2019-12-08) - not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanda Puchille pinha committed Dec 13, 2019
1 parent e4aa77b commit e047d63
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 0 deletions.
11 changes: 11 additions & 0 deletions c_piscine_rush_02/ex00/Makefile
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions c_piscine_rush_02/ex00/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* header.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
# include <stdlib.h>
# include <fcntl.h>
# include <stdio.h>

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
37 changes: 37 additions & 0 deletions c_piscine_rush_02/ex00/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 15:11:09 by apuchill #+# #+# */
/* Updated: 2019/12/08 18:44:10 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include "header.h"
#include <unistd.h>

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);
}
41 changes: 41 additions & 0 deletions c_piscine_rush_02/ex00/numbers.dict
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions c_piscine_rush_02/ex00/rshbas.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rshbas.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 15:22:08 by apuchill #+# #+# */
/* Updated: 2019/12/08 18:29:22 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <unistd.h>

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);
}
115 changes: 115 additions & 0 deletions c_piscine_rush_02/ex00/rshdict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rshdict.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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--;
}
}
Loading

0 comments on commit e047d63

Please sign in to comment.