GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2006, 19:38
briand briand is offline
New Member
 
Join Date: Nov 2006
Posts: 2
briand is on a distinguished road

Number to English Equivalent


Heres some C code i need converted to Python:
CPP / C++ / C Code:
#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
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 12:38
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 13:15
D3D error when running fullscreen games/programs - help?! daa709 Computer Hardware Forum 4 01-Jul-2005 09:03
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.