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;
}