|
Number to English Equivalent
Heres some C code i need converted to Python:
#include <stdio.h>
#include <stdlib.h>
/* special names for values */
struct nx {
char name[20];
int value;
};
struct nx numarr[] = {
{ "zero", 0 },
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "four", 4 },
{ "five", 5 },
{ "six", 6 },
{ "seven", 7 },
{ "eight", 8 },
{ "nine", 9 },
{ "ten", 10 },
{ "eleven", 11 },
{ "twelve", 12 },
{ "thirteen", 13 },
{ "fourteen", 14 },
{ "fifteen", 15 },
{ "sixteen", 16 },
{ "seventeen", 17 },
{ "eighteen", 18 },
{ "nineteen", 19 },
{ "twenty", 20 },
{ "thirty", 30 },
{ "forty", 40 },
{ "fifty", 50 },
{ "sixty", 60 },
{ "seventy", 70 },
{ "eighty", 80 },
{ "ninety", 90 },
{ "", 999 }
};
/* denominations for large numbers */
char *denom[]=
{
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
"sextillion",
"septillion",
"octillion",
"nonillion",
"decillion",
"undecillion",
"duodecillion",
"tredecillion",
"quattuordecillion",
"sexdecillion",
"septendecillion",
"octodecillion",
"novemdecillion",
"vigintillion"
};
/* take a two-digit number and cvt to words. */
char *cvt2(val)
int val;
{
int i=0;
static char word[80];
while( numarr[++i].value <= val)
/* nothing */;
strcpy(word,numarr[i-1].name);
val -= numarr[i-1].value;
if (val > 0)
{
strcat(word,"-");
strcat(word,numarr[val].name);
}
return (word);
}
/* take a 3 digit number and cvt it to words */
char *cvt3(val)
int val;
{
int rem, mod;
static char word[80];
word[0] = '\0';
mod = val % 100;
rem = val / 100;
if ( rem > 0 )
{
strcat(word,numarr[rem].name);
strcat(word," hundred");
if (mod > 0)
strcat(word," ");
}
if ( mod > 0 )
{
strcat(word, cvt2(mod));
}
return(word);
}
/* here's the routine that does the rest */
char *itowords(val)
long val;
{
int tri; /* last three digits */
int place = 0; /* which power of 10 we are on */
int neg=0; /* sign holder */
char temp[255]; /* temporary string space */
static char word[255];
char phrase[100];
word[0] = '\0';
/* check for negative int */
if (val < 0 )
{
neg = 1;
val = -val;
}
if ( val == 0 )
{
strcpy(word,"zero");
return(word);
}
/* what we do now is break it up into sets of three, and add the */
/* appropriate denominations to each. */
while (val > 0 )
{
phrase[0] = '\0';
tri = val % 1000; /* last three digits */
val = val / 1000; /* base 10 shift by 3 */
if (tri > 0 )
{
strcat(phrase,cvt3(tri));
strcat(phrase," ");
}
if ((place > 0 ) && (tri > 0))
{
strcat(phrase,denom[place]);
}
place++;
/* got the phrase, now put it in the string */
strcpy(temp,word);
if ((val > 0) && (tri > 0))
{
strcpy(word,", ");
strcat(word,phrase);
}
else
strcpy(word,phrase);
strcat(word,temp);
}
/* remember that minus sign ? */
if (neg)
{
strcpy(temp,word);
strcpy(word,"negative ");
strcat(word,temp);
}
return(word);
}
main(argc,argv)
int argc;
char **argv;
{
int i=0;
while (++i < argc )
printf("%s\n",itowords(atol(argv[i])));
}
any help would be appreciated
Last edited by LuciWiz : 02-Nov-2006 at 12:59.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|