Untitled
unknown
plain_text
a year ago
2.1 kB
13
Indexable
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
typedef struct s_dict
{
char *key;
char *value;
} t_dict;
t_dict g_dict[] = {
{"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"}};
char *get_number_name(int nbr)
{
char key[12];
sprintf(key, "%d", nbr);
for (int i = 0; i < sizeof(g_dict) / sizeof(t_dict); i++)
{
if (strcmp(g_dict[i].key, key) == 0)
{
return g_dict[i].value;
}
}
return NULL;
}
void print_space()
{
write(1, " ", 1);
}
void print_number(int n)
{
write(1, get_number_name(n), strlen(get_number_name(n)));
}
void ft_print(int n);
void handle_thousands(int n)
{
if (n >= 1000)
{
ft_print(n / 1000);
print_space();
print_number(1000);
if (n % 1000 != 0)
{
print_space();
ft_print(n % 1000);
}
}
}
void handle_hundreds(int n)
{
if (n >= 100)
{
ft_print(n / 100);
print_space();
print_number(100);
if (n % 100 != 0)
{
print_space();
ft_print(n % 100);
}
}
}
void handle_tens(int n)
{
if (n >= 20)
{
print_number((n / 10) * 10);
if (n % 10)
{
print_space();
print_number(n % 10);
}
}
}
void ft_print(int n)
{
if (n >= 1000)
{
handle_thousands(n);
}
else if (n >= 100)
{
handle_hundreds(n);
}
else if (n >= 20)
{
handle_tens(n);
}
else if (n >= 0)
{
print_number(n);
}
}
int main(int ac, char **av)
{
if (ac != 2)
{
write(1, "Error\n", 6);
return 0;
}
int number = atoi(av[1]);
if (number < 0)
{
write(1, "Error\n", 6);
return 0;
}
ft_print(number);
write(1, "\n", 1);
return 0;
}
Editor is loading...
Leave a Comment