Wednesday, December 10, 2008

C++ Program to convert number to spoken string

[Divide and Conquer Approach]
1). int varies from -2147483648 to +2147483647

2). e.g. 2,147,483,648 : 4 groups of three (out of which one group will contain only one digit)

3). Once we break the number in groups we can then represent it in, [w billion x million y thousand z], where billion, million and thousand are prepositions.

4). Each group will contain max upto 3 digits

5). Having basic knowledge of number to word will help us.
e.g. digit[0] = "zero";
digit[1] = "one";
digit[2] = "two"; and so on...

6). Preposition will help us merging back the solution.
e.g.
places[2] = "thousand";
places[3] = "million";
places[4] = "billion";

7). Go through the code for details :)

  
#include <string>
#include <vector>
#include <map>
using namespace std;

#include <math.h>

string numberToSpokenFormString(int n) {
// assuming int is 4 byte

map<int,string> digit;
map<int,string> places;

digit[0] = "zero";
digit[1] = "one";
digit[2] = "two";
digit[3] = "three";
digit[4] = "four";
digit[5] = "five";
digit[6] = "six";
digit[7] = "seven";
digit[8] = "eight";
digit[9] = "nine";
digit[10] = "ten";
digit[11] = "eleven";
digit[12] = "twelve";
digit[13] = "thirteen";
digit[14] = "fourteen";
digit[15] = "fifteen";
digit[16] = "sixteen";
digit[17] = "seventeen";
digit[18] = "eighteen";
digit[19] = "nineteen";
digit[20] = "twenty";
digit[30] = "thirty";
digit[40] = "fourty";
digit[50] = "fifty";
digit[60] = "sixty";
digit[70] = "seventy";
digit[80] = "eighty";
digit[90] = "ninety";
digit[100] = "hundred";

places[2] = "thousand";
places[3] = "million";
places[4] = "billion";

string str = "";
int nNumOfDigits = 1;
int nPairs = 1;

if(n < 0)
{
n = abs(n);
str += "minus ";
}

if(n == 0)
{
str = digit[0] ;
}
else
{
// [Number of digits in the integer]
// For example log10 of number 34250000 is [ 7 + log10(3.425) ] = 7.xxxxxxx
// take only integer part and you will have 7. Add 1 to it and number of digits = 8

nNumOfDigits = (int)(log10((double)n))+1;

// [Groups of three]
// int varies between -2147483648 to +2147483647
// 2,147,483,648 : 4 groups of three (out of which one group will contain only one digit)
// once we break the number in groups we can then represent it in,
// w billion x million y thousand z, where billion, million and thousand are preposition.
// [Divide and Conquer approch]

nPairs = ceil(((double)nNumOfDigits/3));

int temp, num;
for(int i = 0; i <= nPairs; i++) // Complexity O(n), n: number of digits.
{
num = n/(int)pow((double)1000, (double)nPairs-i);

for(int j = 100; j >= 1; j=j/10)
{
temp = num / j;
if(temp != 0)
{
if(j == 100)
{
str += digit[temp] + " " + digit[j] + " " ;
}
else if(j == 10)
{
if(num <= 20) // Check if number is less than or equal to 20
{
str += digit[num]+ " " ;
break;
}
else
{
str += digit[temp*j] + " " ;
}
}
else
{
str += digit[num] + " ";
}
num %= j;
}
}
str += places[nPairs - i + 1] + " " ;

if(nPairs > 1)
{
n %= (int)pow((double)1000, (double)nPairs-i);
}
}
}

return str;
}