comment my code
Author Message
comment my code

Hi,

First, I'm sorry for my bad english...

Yesterday, I saw this post :

Quote:
>if the number is 10,172 should be printed as "tenthousand one hundred
>and sevetry two".

and I think that was a very good exercice for me. So I wrote the following
code.
It work well, but as I am a novice, I'm sure it could be better :)
I'll keep my eyes open for any of your suggestions and comments.

Thanx,
Xavier Nayrac

8<========== start of speak.c =========================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * T1[]=   {"","one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

char * Tten[]= {"ten","eleven", "twelve", "thir{*filter*}",
"four{*filter*}","fif{*filter*}","six{*filter*}",
"seven{*filter*}","eigh{*filter*}","nine{*filter*}"};

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

int speak ( char * part );
int istrtoen ( char * strNumber );

/* Works with any numbers of digits (I hope :-) */
int istrtoen ( char * strNumber )
{
char part[4];
int i;
int len = strlen ( strNumber );
int firstPartLen = len % 3;
int nbOtherPart = len / 3;

/* Simple special case */
if ( atoi ( strNumber ) == 0 )
{
printf ( "none\n" );
return ( 0 );
}

/* The first part */
if ( firstPartLen != 0 )
{
strncpy ( part, strNumber, firstPartLen );
speak ( part );
switch ( nbOtherPart )
{
case 0: break;
case 1: printf ( "thousand " ); break;
case 2: printf ( "million " ); break;
case 3: printf ( "billion " ); break;
default: printf ( "DON'T NO WHAT " );
}
}

/* The other parts*/
for ( i = 0; i < nbOtherPart; i++ )
{
strncpy ( part, strNumber + ( firstPartLen + i * 3 ), 3 );
if ( atoi ( part ) == 0 )
continue;
speak ( part );
switch ( nbOtherPart - i )
{
case 1: break;
case 2: printf ( "thousand " ); break;
case 3: printf ( "million " ); break;
case 4: printf ( "billion " ); break;
default: printf ( "DON'T NO WHAT " );
}
}
printf ( "\n" );
return ( 0 );

Quote:
}

int speak ( char * part )
{
int n1, n10, n100;
n1 = n10 = n100 = 0;

/* n1 is the least significant digit of the part.
n10 is the middle one and n100 is the most
significant one */
switch ( strlen ( part ) )
{
case 3: n100 = part[0] - '0';
n10  = part[1] - '0';
n1   = part[2] - '0';
break;
case 2: n10  = part[0] - '0';
n1   = part[1] - '0';
break;
case 1: n1   = part[0] - '0';
break;
case 0: return ( 0 );
}

if ( n100 > 0 )
printf ( "%s hundred ", T1[n100] );
if ( ( n100 > 0 ) && ( ( n1 != 0 ) || ( n10 != 0 ) ) )
printf ( "and " );

if ( n10 > 1 )
printf ( "%s ", T10[n10] );
/* ATTENTION : special case ! */
else if ( n10 == 1 )
{
printf ( "%s ", Tten[n1] );
return ( 0 );
}

if ( n1 != 0 )
printf ( "%s ", T1[n1] );

return ( 0 );

Quote:
}

int main ( int argc, char * argv[] )
{
/* WARNING: arg one must be a number of
any digits, otherwise the prog crashes.
Do the verif here.
*/
istrtoen ( argv[1] );
return ( 0 );
Quote:
}

8<========== end of speak.c =========================
--

Sun, 31 Mar 2002 03:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages