IT'S WORKING, only norminette left!
This commit is contained in:
parent
0b60f1f9e5
commit
c8afa2fd39
|
@ -1,5 +1,6 @@
|
|||
SRC = main.c \
|
||||
ft_strlib.c \
|
||||
ft_strlib2.c \
|
||||
ft_io.c \
|
||||
argparse.c \
|
||||
dictparse.c \
|
||||
|
@ -8,8 +9,11 @@ SRC = main.c \
|
|||
printing.c \
|
||||
ft_convert.c \
|
||||
ft_ntow.c \
|
||||
ft_array.c \
|
||||
ft_math.c \
|
||||
|
||||
HEADERS = ft_strlib.h \
|
||||
ft_strlib2.h \
|
||||
ft_io.h \
|
||||
colors.h \
|
||||
argparse.h \
|
||||
|
@ -19,6 +23,8 @@ HEADERS = ft_strlib.h \
|
|||
printing.h \
|
||||
ft_convert.h \
|
||||
ft_ntow.h \
|
||||
ft_array.h \
|
||||
ft_math.h \
|
||||
|
||||
OBJDIR = obj
|
||||
INCDIR = include
|
||||
|
@ -87,6 +93,8 @@ CLR_RST = && : [m
|
|||
|
||||
run:
|
||||
@clear
|
||||
$(YLW) ./$(NAME) "./dicts/numbers2.dict" 12345678 $(CLR_RST) $(SUCCESS)
|
||||
$(YLW) ./$(NAME) "./dicts/numbers2.dict" 123456 $(CLR_RST) $(SUCCESS)
|
||||
$(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS)
|
||||
$(YLW) ./$(NAME) 1 $(CLR_RST) $(SUCCESS)
|
||||
$(YLW) ./$(NAME) "./dicts/numbers2.dict" 1 $(CLR_RST) $(SUCCESS)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 15:12:53 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/01 21:58:44 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 21:23:58 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,7 +18,7 @@ int parse_args(int argc, char **argv, char **path, char **str_nbr)
|
|||
{
|
||||
if (argc > 3)
|
||||
return (0);
|
||||
*path = "./numbers.dict";
|
||||
*path = "./dicts/numbers.dict";
|
||||
if (argc < 2)
|
||||
{
|
||||
*str_nbr = "123";
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_array.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 22:34:51 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 22:37:10 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_array.h"
|
||||
#include "ft_strlib.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
char **partition_right(char *str, int part_size)
|
||||
{
|
||||
int strlen;
|
||||
int first_part_size;
|
||||
int idx;
|
||||
char **arr;
|
||||
|
||||
strlen = ft_strlen(str);
|
||||
arr = malloc(sizeof(char *) * (strlen + 1));
|
||||
first_part_size = strlen % part_size;
|
||||
idx = 0;
|
||||
if (first_part_size)
|
||||
{
|
||||
arr[idx] = malloc(sizeof(char) * (first_part_size + 1));
|
||||
ft_strlcpy(arr[idx], str, first_part_size + 1);
|
||||
str += first_part_size;
|
||||
++idx;
|
||||
}
|
||||
while (*str)
|
||||
{
|
||||
arr[idx] = malloc(sizeof(char) * (part_size + 1));
|
||||
ft_strlcpy(arr[idx], str, part_size + 1);
|
||||
str += part_size;
|
||||
++idx;
|
||||
}
|
||||
arr[idx] = NULL;
|
||||
return (arr);
|
||||
}
|
||||
|
||||
int arr_len(char **arr)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 0;
|
||||
while (*arr++)
|
||||
++size;
|
||||
return (size);
|
||||
}
|
||||
|
||||
void free_arr(char **arr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (arr[++i])
|
||||
free(arr[i]);
|
||||
free(arr);
|
||||
}
|
||||
|
||||
char *ft_strjoin(int size, char **strs, char *sep)
|
||||
{
|
||||
int total_len;
|
||||
int sep_len;
|
||||
int idx;
|
||||
char *joined_str;
|
||||
|
||||
total_len = 0;
|
||||
sep_len = ft_strlen(sep);
|
||||
idx = -1;
|
||||
while (++idx < size)
|
||||
total_len += ft_strlen(strs[idx]) + sep_len;
|
||||
total_len -= sep_len;
|
||||
if (total_len < 1)
|
||||
joined_str = malloc(sizeof(char) * 1);
|
||||
else
|
||||
joined_str = malloc(sizeof(char) * (total_len + 1));
|
||||
idx = -1;
|
||||
joined_str[0] = 0;
|
||||
while (++idx < size)
|
||||
{
|
||||
ft_strcat(joined_str, strs[idx]);
|
||||
if (idx < size - 1)
|
||||
ft_strcat(joined_str, sep);
|
||||
}
|
||||
return (joined_str);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_math.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 22:37:55 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 22:38:10 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_math.h"
|
||||
|
||||
int power(int nb, int exp)
|
||||
{
|
||||
if (exp < 1)
|
||||
return (exp == 0);
|
||||
return (nb * power(nb, exp - 1));
|
||||
}
|
127
ex00/ft_ntow.c
127
ex00/ft_ntow.c
|
@ -6,17 +6,134 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 20:26:44 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 20:29:01 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 22:37:52 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_ntow.h"
|
||||
#include "ft_linked_list.h"
|
||||
#include "ft_strlib.h"
|
||||
#include "dictparse.h"
|
||||
#include "ft_convert.h"
|
||||
#include "ft_array.h"
|
||||
#include "ft_math.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *num_to_words(t_map_entry *map)
|
||||
char *triplet_to_word(char *triplet, t_map_entry *map)
|
||||
{
|
||||
(void)map;
|
||||
printf("%s\n", ll_map_get_value(map, "17"));
|
||||
return ("one hundred");
|
||||
char *triplet_word;
|
||||
int triplet_len;
|
||||
char *map_value;
|
||||
int idx;
|
||||
int positional_value;
|
||||
int digit;
|
||||
char *digit_str;
|
||||
char *lookup_num;
|
||||
|
||||
triplet_word = malloc(sizeof(char) * MAX_LINE_LENGTH * 5);
|
||||
ft_strcpy(triplet_word, "");
|
||||
if (ft_atoi(triplet) < 20)
|
||||
{
|
||||
map_value = ll_map_get_value(map, triplet);
|
||||
if (map_value == NULL)
|
||||
return (ft_strcpy(triplet_word, "MISSING_KEY"));
|
||||
ft_strcat(triplet_word, map_value);
|
||||
return (triplet_word);
|
||||
}
|
||||
else
|
||||
{
|
||||
triplet_len = ft_strlen(triplet);
|
||||
idx = 0;
|
||||
while (triplet_len--)
|
||||
{
|
||||
lookup_num = malloc(sizeof(char) * MAX_DIGITS);
|
||||
digit = triplet[idx] - '0';
|
||||
positional_value = power(10, triplet_len) * digit;
|
||||
ft_itoa(lookup_num, positional_value);
|
||||
digit_str = malloc(sizeof(char) * 2);
|
||||
ft_itoa(digit_str, digit);
|
||||
map_value = ll_map_get_value(map, digit_str);
|
||||
free(digit_str);
|
||||
if (map_value == NULL)
|
||||
{
|
||||
free(lookup_num);
|
||||
return (ft_strcpy(triplet_word, "MISSING_KEY"));
|
||||
}
|
||||
if (triplet_len == 2)
|
||||
map_value = ll_map_get_value(map, "100");
|
||||
else
|
||||
map_value = ll_map_get_value(map, lookup_num);
|
||||
free(lookup_num);
|
||||
if (map_value == NULL)
|
||||
return (ft_strcpy(triplet_word, "MISSING_KEY"));
|
||||
if (triplet_len == 2)
|
||||
{
|
||||
digit_str = malloc(sizeof(char) * 2);
|
||||
ft_itoa(digit_str, digit);
|
||||
ft_strcat(triplet_word, ll_map_get_value(map, digit_str));
|
||||
ft_strcat(triplet_word, " ");
|
||||
free(digit_str);
|
||||
}
|
||||
ft_strcat(triplet_word, map_value);
|
||||
if (triplet_len != 0)
|
||||
ft_strcat(triplet_word, " ");
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
return (triplet_word);
|
||||
}
|
||||
|
||||
void num_to_words_free(char **triplets, char **parts, char *thousands_power)
|
||||
{
|
||||
free_arr(triplets);
|
||||
free_arr(parts);
|
||||
free(thousands_power);
|
||||
}
|
||||
|
||||
char *num_to_words(char *str_nbr, t_map_entry *map)
|
||||
{
|
||||
char **triplets;
|
||||
int arr_size;
|
||||
int size;
|
||||
int idx;
|
||||
char *thousands_power;
|
||||
char *thousands_power_word;
|
||||
char **parts;
|
||||
char *triplet_word;
|
||||
char *result;
|
||||
|
||||
(void)map;
|
||||
triplets = partition_right(str_nbr, 3);
|
||||
arr_size = arr_len(triplets);
|
||||
thousands_power = malloc(sizeof(char) * MAX_LINE_LENGTH);
|
||||
idx = 0;
|
||||
parts = malloc(sizeof(char *) * (arr_size + 1));
|
||||
size = arr_size;
|
||||
while (size--)
|
||||
{
|
||||
ft_itoa(thousands_power, power(1000, size));
|
||||
thousands_power_word = ll_map_get_value(map, thousands_power);
|
||||
if (!thousands_power_word)
|
||||
{
|
||||
num_to_words_free(triplets, parts, thousands_power);
|
||||
return (NULL);
|
||||
}
|
||||
triplet_word = triplet_to_word(triplets[idx], map);
|
||||
if (!ft_strcmp(triplet_word, "MISSING_KEY"))
|
||||
{
|
||||
num_to_words_free(triplets, parts, thousands_power);
|
||||
return (NULL);
|
||||
}
|
||||
parts[idx] = malloc(sizeof(char) * MAX_LINE_LENGTH * 6);
|
||||
ft_strcpy(parts[idx], triplet_word);
|
||||
if (size != 0)
|
||||
ft_strcat(ft_strcat(parts[idx], " "), thousands_power_word);
|
||||
free(triplet_word);
|
||||
++idx;
|
||||
}
|
||||
parts[idx] = NULL;
|
||||
result = ft_strjoin(arr_size, parts, " ");
|
||||
num_to_words_free(triplets, parts, thousands_power);
|
||||
return (result);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:13:31 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 18:38:52 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 21:17:42 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -33,31 +33,29 @@ char *ft_strcpy(char *dest, char *src)
|
|||
return (o_dest);
|
||||
}
|
||||
|
||||
int ft_strlen(char *str)
|
||||
char *ft_strcat(char *dest, char *src)
|
||||
{
|
||||
int i;
|
||||
char *orig_s1;
|
||||
|
||||
i = 0;
|
||||
while (*str++)
|
||||
++i;
|
||||
return (i);
|
||||
orig_s1 = dest;
|
||||
while (*dest)
|
||||
++dest;
|
||||
while (*src)
|
||||
*dest++ = *src++;
|
||||
*dest = 0;
|
||||
return (orig_s1);
|
||||
}
|
||||
|
||||
int ft_isspace(char c)
|
||||
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
|
||||
{
|
||||
return (c == ' '
|
||||
|| c == '\t'
|
||||
|| c == '\v'
|
||||
|| c == '\n'
|
||||
|| c == '\r'
|
||||
|| c == '\f');
|
||||
}
|
||||
char *orig_src;
|
||||
|
||||
char *last_non_whitespace(char *ptr)
|
||||
{
|
||||
while (*ptr)
|
||||
++ptr;
|
||||
while (ft_isspace(*ptr))
|
||||
--ptr;
|
||||
return (ptr);
|
||||
orig_src = src;
|
||||
while (size-- > 1 && *src)
|
||||
*dest++ = *src++;
|
||||
*dest = 0;
|
||||
size = 0;
|
||||
while (*orig_src++)
|
||||
++size;
|
||||
return (size);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlib2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 20:49:31 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 20:49:40 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_strlen(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (*str++)
|
||||
++i;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_isspace(char c)
|
||||
{
|
||||
return (c == ' '
|
||||
|| c == '\t'
|
||||
|| c == '\v'
|
||||
|| c == '\n'
|
||||
|| c == '\r'
|
||||
|| c == '\f');
|
||||
}
|
||||
|
||||
char *last_non_whitespace(char *ptr)
|
||||
{
|
||||
while (*ptr)
|
||||
++ptr;
|
||||
while (ft_isspace(*ptr))
|
||||
--ptr;
|
||||
return (ptr);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_array.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 22:35:29 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 22:35:45 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_ARRAY_H
|
||||
# define FT_ARRAY_H
|
||||
|
||||
char **partition_right(char *str, int part_size);
|
||||
int arr_len(char **arr);
|
||||
void free_arr(char **arr);
|
||||
char *ft_strjoin(int size, char **strs, char *sep);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_math.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 22:38:13 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 22:38:24 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_MATH_H
|
||||
# define FT_MATH_H
|
||||
|
||||
int power(int nb, int exp);
|
||||
|
||||
#endif
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 20:21:19 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 20:29:19 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 20:32:16 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,6 +14,6 @@
|
|||
# define FT_NTOW_H
|
||||
# include "ft_linked_list.h"
|
||||
|
||||
char *num_to_words(t_map_entry *map);
|
||||
char *num_to_words(char *str_nbr, t_map_entry *map);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:14:29 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 18:39:09 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 21:18:30 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,11 +14,11 @@
|
|||
# define FT_STRLIB_H
|
||||
# include <unistd.h>
|
||||
# include "colors.h"
|
||||
# include "ft_strlib2.h"
|
||||
|
||||
char *ft_strcpy(char *dest, char *src);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
int ft_strlen(char *str);
|
||||
char *last_non_whitespace(char *ptr);
|
||||
int ft_isspace(char c);
|
||||
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size);
|
||||
char *ft_strcat(char *dest, char *src);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlib2.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 20:50:20 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 20:50:35 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_STRLIB2_H
|
||||
# define FT_STRLIB2_H
|
||||
|
||||
char *last_non_whitespace(char *ptr);
|
||||
int ft_strlen(char *str);
|
||||
int ft_isspace(char c);
|
||||
|
||||
#endif
|
11
ex00/main.c
11
ex00/main.c
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/01 07:11:58 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/02 20:21:54 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/02 22:33:19 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
|||
#include "ft_convert.h"
|
||||
#include "ft_ntow.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
@ -30,14 +31,18 @@ int main(int argc, char **argv)
|
|||
map = ll_map_new_entry("about", "Dict for the data from *.dict file", NULL);
|
||||
if (!parse_dict(path, map))
|
||||
return (puterr("Dict Error\n", 2));
|
||||
numberwords = num_to_words(map);
|
||||
if (*numberwords)
|
||||
numberwords = num_to_words(str_nbr, map);
|
||||
if (numberwords != NULL)
|
||||
{
|
||||
ft_putstr(numberwords);
|
||||
ft_putchar('\n');
|
||||
free(numberwords);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(numberwords);
|
||||
return (puterr("Dict Error\n", 3));
|
||||
}
|
||||
ll_clear(map, 1);
|
||||
return (0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue