Re: Converting Numbers to Words in English by recursion.



On 9 Mar 2007 20:03:09 -0800
jedale@xxxxxxxxx wrote:

I am trying to convert numbers to there corresponding words but it
only works for numbers under 1,000 but need it to work for 1 billion.

It doesn't even work for numbers between 10 and 20 - you need to
special case those - and the handling above 1000 is completely off. Try
the code below (and if you switch to unsigned long long instead of int and
extend the places and place_sizes lists and the loop you can go much higher)

With regard to testing which has occupied most of this thread I
suggest a middle ground of making a test script that excercises the program
testing for expected results on spot checks in the range 0-9, 10-20, 20-100,
100-1000, 1000-100000, and so forth.
I didn't bother with a script I jyst
hand spot checked the results - but then I don't intend to keep this code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/* Prototype */
void print_num(int num);

char *smallnum[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sizteen", "seventeen",
"eighteen", "nineteen"
};

char *tens[] = {
"twenty", "thirty", "fourty", "fifty", "sixty",
"seventy", "eighty", "ninety"};

char *places[] = {"thousand", "million", "billion"};
int place_sizes[] = {1000, 1000000, 1000000000};

int main(int arcg, char **argv)
{
int num = atoi(argv[1]);

printf("%d\n", num); /* Print that number */
print_num(num);
}

/* Print out the number sequence */
void print_num(int num)
{
int i;
if (num < 20)
{
printf("%s ", smallnum[num]);
}
else if (num < 100)
{
printf("%s ", tens[num/10 - 2]);
if (num % 10)
print_num(num%10);
}
else if(num < 1000)
{
print_num(num/100);
printf("hundred ");
if (num % 100)
print_num(num%100);
}
else
{
for (i = 2; i >= 0; i--)
{
if (num / place_sizes[i])
{
print_num(num / place_sizes[i]);
printf ("%s ", places[i]);
num = num % place_sizes[i];
}
}
if (num)
print_num(num);
}
}


--
C:>WIN | Directable Mirror Arrays
The computer obeys and wins. | A better way to focus the sun
You lose and Bill collects. | licences available see
| http://www.sohara.org/
.



Relevant Pages

  • Re: Ex 7-5
    ... int getop; ... double num; ... void clearStack; ... not make sense if the stack has been cleared so a fall-though can't ...
    (comp.lang.c)
  • Re: code for two half diamond shapes
    ... > void space(int num); ... > int num; ... Quote what you're replying to -- even when replying to yourself, ...
    (comp.lang.c)
  • Re: pgm without std library functions
    ... Depending on your definition of read, ... int main (void) { ... read(STDIN_FILENO, &num, 1); ...
    (comp.lang.c)
  • Re: [RFC] New kernel-message logging API
    ... return buf; ... static char* put_dec(char *buf, unsigned long long num) ... static int skip_atoi ...
    (Linux-Kernel)
  • Re: Ex 7-5
    ... void clearStack; ... I don't interpret the suggestion to rewrite to use scanf ... // Tondo and Gimpel has the next declaration wrongly an int ... double num; ...
    (comp.lang.c)