Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Numbers to Word

  1. #1
    Join Date
    Jul 2005
    Posts
    27
    Rep Power
    0

    Default Numbers to Word

    Here is a simple challenge
    Write a programme that accepts a number from the user and displays the words eg. 999 = Nine Hundred and Ninety Nine.

    You can put a limit on the size of the number. My limit was 999,999. However i plan to put it to a million. I did it in 146 lines of code in C. I am positive someone is going to put me to shame, i don't mind.

    Well have fun.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Cool problem for trying for someone like me with nothing to do.

    I do not exactly have nothing to do except when it comes to programming. I want to do a program on any and everything to be well rounded.

    The problem is longer than it seems, well for me.

    I must be doing someting wrong because I cleared over 300 lines and it still incomplete.

    Things to do:
    add presentation commas
    simplify code
    solid testing

    To test this program use:
    1
    10
    11
    100
    1001
    1010
    1100
    1100
    and so on

    No zero case implemented!

    Code:
    /*
    NumbersToWord.cpp
    Copyright © 2005 JaGameDevelopers®
    by Cross Wire
    */
    
    
    
    
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    
    
    namespace utility
    {
    	//Eg 2 to Two
    	void DigitToDigitWord(int Digit, char * DigitWord)
    	{
    		strcpy(DigitWord, "");
    
    		switch(Digit)
    		{
    		case 1:
    			strcpy(DigitWord, "One");
    			break;
    		case 2:
    			strcpy(DigitWord, "Two");
    			break;
    		case 3:
    			strcpy(DigitWord, "Three");
    			break;
    		case 4:
    			strcpy(DigitWord, "Four");
    			break;
    		default:
    			printf("Error\nDigitToDigitWord %d failed\n", Digit);
    		}
    	}
    
    	//Eg 2 to Twenty
    	void DigitentyToDigitentyWord(int Digitenty, char * DigitentyWord)
    	{
    		strcpy(DigitentyWord, "");
    
    		switch(Digitenty)
    		{
    		case 0:
    			printf("no case");
    			break;
    		case 1:
    			printf("special case: eg thirteen");
    			break;
    		case 2:
    			strcpy(DigitentyWord, "Twenty");
    			break;
    		case 3:
    			strcpy(DigitentyWord, "Thirty");
    			break;
    		default:
    			printf("Error\nDigitentyToDigitentyWord %d failed\n", Digitenty);
    		}
    	}
    
    	//Eg 2 to Twelve
    	void DigiteenToDigiteenWord(int Digiteen, char * DigiteenWord)
    	{
    		strcpy(DigiteenWord, "");
    
    		switch(Digiteen)
    		{
    		case 0:
    			strcpy(DigiteenWord, "Ten");
    			break;
    		case 1:
    			strcpy(DigiteenWord, "Eleven");
    			break;
    		case 2:
    			strcpy(DigiteenWord, "Twelve");
    			break;
    		case 3:
    			strcpy(DigiteenWord, "Thirteen");
    			break;
    		}
    	}
    
    	//Eg 2 to Hundred
    	void TensPowerToTensPowerWord(int TensPower, char * TensPowerWord)
    	{
    		strcpy(TensPowerWord, "");
    
    		switch(TensPower)
    		{
    		//case 0:
    		//	printf("special case: eg ones");
    		//	break;
    		//case 1:
    		//	printf("special case: eg twenty");
    		//	break;
    		case 2:
    			strcpy(TensPowerWord, "Hundred");
    			break;
    		case 3:
    			strcpy(TensPowerWord, "Thousand");
    			break;
    		case 6:
    			strcpy(TensPowerWord, "Million");
    			break;
    		default:
    			printf("Error\nTensPowerToTensPowerWord %d failed\n", TensPower);
    		}
    	}
    
    	//Eg return 2 when Number = 12345 and DigitIndex = 3
    	int GetDigitFromNumber(int Number, int DigitIndex)
    	{
    		//<optimise>Lazy implementation
    		char	NumberAsString[128];
    		ltoa(Number, NumberAsString, 10);
    		strrev(NumberAsString);
    		char	DigitAsString[128]	=	{NumberAsString[DigitIndex], NULL};
    		return	atol(DigitAsString);
    
    	}
    
    	//Eg return 4 when Number = 2356
    	int	GetLenghtOfNumber(int Number)
    	{
    		//<optimise>Lazy implementation
    		char	NumberAsString[128];
    		ltoa(Number, NumberAsString, 10);
    		return	(int)strlen(NumberAsString);
    	}
    
    	//Eg Word returns "Nine Hundred and Ninety Nine" when Number = 999
    	void NumberToWord(int Number, char * Word)
    	{
    		if(Number >= 1000000)
    		{
    			printf("No conversion made on '%d'\n", Number);
    			printf("Reason : the number it too big\n");
    			return;
    		}
    
    		bool	TeenWasJustWritten		=	false;		//Flag not to print "Two" in 12 after printing "Twelve"
    		bool	HundredWasJustWritten	=	false;		//Flag to add "and" after printing "Hundred"
    		bool	SomethingWasJustWritten	=	false;		//Flag to print a space " "
    		bool	SQuantityWasJustWritten	=	false;		//Flag to add "," after printing "Thousand", "Million"
    		bool	TSetIsWritten			=	false;		//Some numbere in directly in front of a "Thousand", "Million" was written
    
    		strcpy(Word, "");
    
    		for(int DigitIndex = utility::GetLenghtOfNumber(Number) - 1; DigitIndex >= 0; DigitIndex--)
    		{
    			/*
    			Write stuff for this DigitIndex position if digit is non-zero, eg Two Thousand
    			*/
    
    			char catBuffer[128];
    
    			int Digit	=	utility::GetDigitFromNumber(Number, DigitIndex);
    			if(Digit)//There is a non zero digit, so something will be written
    			{
    
    				/*
    				Write any presentation stuff
    				*/
    				if(HundredWasJustWritten)
    				{
    					strcat(Word, " and");
    					HundredWasJustWritten	=	false;
    				}
    				if(SomethingWasJustWritten)
    				{
    					strcat(Word, " ");
    					SomethingWasJustWritten	=	false;
    				}
    
    
    				switch(DigitIndex%3)	//For the pattern of ...hundreds, x-ties, teens...hundreds, x-ties, teens
    				{
    				case 0:	//The digit is in the ones
    
    					/*
    					Write the digit, eg Two,... except for in eleven, twelve, thirteen, ... ie a teen was just written
    					*/
    					if(!TeenWasJustWritten)//Teen was not written
    					{
    						utility::DigitToDigitWord(Digit, catBuffer);
    						strcat(Word, catBuffer);
    
    						TeenWasJustWritten		=	false;
    						SomethingWasJustWritten	=	true;
    						TSetIsWritten			=	true;
    					}
    
    
    					break;
    
    				case 1:	//The digit is in the x-ties or the teens
    
    					if(Digit == 1)//The digit is in the teens
    					{
    						/*
    						Write for 2 digits, eg Twevle, Thirteen, Forteen
    						*/
    						utility::DigiteenToDigiteenWord(utility::GetDigitFromNumber(Number, DigitIndex - 1), catBuffer);
    						strcat(Word, catBuffer);
    
    						TeenWasJustWritten		=	true;
    						SomethingWasJustWritten	=	true;
    						TSetIsWritten			=	true;
    					}
    					else//The digit is in the x-ties
    					{
    						/*
    						Write the Twenty, Thirty, ...
    						*/
    						utility::DigitentyToDigitentyWord(Digit, catBuffer);
    						strcat(Word, catBuffer);
    
    						SomethingWasJustWritten	=	true;
    						TSetIsWritten			=	true;
    					}
    
    					break;
    
    				case 2:	//The digit is in the hundreds
    					/*
    					Write the digit, eg Two, NO Twenty or Twelve
    					*/
    					utility::DigitToDigitWord(Digit, catBuffer);
    					strcat(Word, catBuffer);
    					strcat(Word, " ");
    
    					/*
    					Write the quantity amount ie Hundred
    					*/
    					utility::TensPowerToTensPowerWord(2, catBuffer);
    					strcat(Word, catBuffer);
    
    					/*
    					Pluralize the quantity if necessary
    					*/
    					if(Digit > 1)//The digit is plural
    					{
    						//Add a 's'
    						strcat(Word, "s");
    					}
    
    					HundredWasJustWritten	=	true;
    					SomethingWasJustWritten	=	true;
    					TSetIsWritten			=	true;
    
    					break;
    				}
    			}
    
    
    			if((DigitIndex%3)==0)//TSet end, eg done write "one hundred and twelve" in 111,112,111
    			{
    				/*
    				Write the super quantity eg thousand, millions, billions
    				*/
    				if((TSetIsWritten	)&&(DigitIndex != 0))//There is any super quantity
    				{
    					strcat(Word, " ");
    					utility::TensPowerToTensPowerWord(DigitIndex, catBuffer);
    					strcat(Word, catBuffer);
    					SQuantityWasJustWritten	=	true;
    					TSetIsWritten			=	false;
    				}
    
    				//Clear flags
    				TeenWasJustWritten		=	false;		//Flag not to print "Two" in 12 after printing "Twelve"
    				HundredWasJustWritten	=	false;		//Flag to add "and" after printing "Hundred"
    				SQuantityWasJustWritten	=	false;		//Flag to add "," after printing "Thousand", "Million"
    				TSetIsWritten			=	false;		//Some numbere in directly in front of a "Thousand", "Million" was written
    
    			}
    
    		}
    	}
    }
    
    
    
    int main(int argc, char* argv[])
    {
    	while(true)
    	{
    		printf("Please enter your number (without commas)\n");
    
    		int		Number;
    		scanf("%d", &Number);
    
    		if(Number == 0)
    			return	0;
    
    		if(Number >= 1000000)
    		{
    			printf("No conversion made on '%d'\n", Number);
    			printf("Reason : the number it too big\n");
    		}
    		else
    		{
    			char	Word[128];
    			utility::NumberToWord(Number, Word);
    
    			printf("\n'%d' = %s\n", Number, Word);
    		}
    	}
    	return 0;
    
    }
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default good attempt

    Properly structured.
    modular
    long
    hope you finish/optimise it

  4. #4
    Join Date
    Jul 2005
    Posts
    27
    Rep Power
    0

    Default

    Well crosswire, i am looking at you approach and it is a very interesting one. Good try, i am sure your finish product will be better.

  5. #5
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I was thinking of another was of doing it. I would replace words for numbers in one pass, then insert 'and's then ','s. And teens would be used in the replace. That may be a shorter and more sturdy way of doing it. It still on the drawing board.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Aaaaha,
    I could seperated the program logic by using ranges, eg if the number is in the range 0-19, I directly implement the conversion to words using 20 lines. This simplifies the logic used for the rest of the program which handles more constrained numbers.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Jul 2005
    Posts
    27
    Rep Power
    0

    Default

    LOL i like the enthusiasm. Well the idea can work, that is the beauty of programming. Can't lie though i was hoping more people would have responded to the challenge.

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Maybe you should post more problems. I will try to post a tough one myself.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Well i wasn't going to attempt it, but i had to because i'll give you people some challenges i hope you all attempt.

    next time i'll comment my code.

    Code:
    #include <stdio.h>
    
    ////////////////////////////////////////////////////////////////////////////////
    // ANDREW HALL a.k.a Kodeci                                                   //
    // icymint3@yahoo.com | icymint3@gmail.com | alphanexxus@yahoo.com            //
    ////////////////////////////////////////////////////////////////////////////////
    
    const char *smalls[] = {"zero","one","two","three","four","five","six","seven",
    								"eight","nine","ten","eleven","twelve","thirteen",
                            "fourteen","fifteen","sixteen","seventeen","eighteen",
                            "nineteen"};
    
    const char *tens[] = {"fill","fill","twenty","thirty","forty","fifty",
    								"sixty","seventy","eighty","ninety"};
    
    const char *seperator[] = {".","thousand","million"};
    
    const int seperationFactor[] = {1,1000,1000000};
    
    
    void handle9Digits(int);
    void handle3Digits(int,int);
    void handle2Digits(int);
    
    int main()
    {
       int num;
    
       printf( " Enter a Number : " );
       scanf( "%d", &num );
    
       handle9Digits( num );
    
       return 1;
    }
    
    void handle9Digits(int value)
    {
    	if( value > 999999 ) handle3Digits(value,2);
    	if( value > 999 ) handle3Digits(value,1);
    	handle3Digits(value,0);
    }
    
    void handle3Digits(int value,int level)
    {
    	int the3Digits = value / seperationFactor[level] % 1000;
       int the2Digits = the3Digits % 100;
       bool hasHundred = the3Digits > 99;
    
       if( hasHundred ) printf( "%s hundred", smalls[ the3Digits / 100 ] );
       if( hasHundred || ( !level && the2Digits ) ) printf( " and " );
       if( the2Digits || ( !level && the2Digits )  ) handle2Digits( the2Digits );
    
       printf( " %s ", seperator[ level ] );
    
    }
    
    void handle2Digits(int value)
    {
       if( value < 20 ) printf( smalls[ value ] );
       else printf( "%s-%s", tens[ value / 10 ], smalls[ value % 10 ] );
    }

  10. #10
    Join Date
    Jul 2005
    Posts
    27
    Rep Power
    0

    Default

    Hey icymint i am very impressed. Your code even goes to millions. Just need to review my code and see if i can come better.

    I look forward your challenges...................

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •