View Single Post
Old 10-20-2004, 06:11 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hmm, I thought you'd liked to figure out how it works on your own.

The concept
Lets simulate what your program should do.
Code:
User enters:   output:
0              null
1              one
1.01           one one
1.02           one two
...            ...
50.76          fifty seventysix
30.19          thirty nineteen
21.29          twenty twentynine
And so forth.
The first 4 entries gives us a hint already on how to solve our little problem. For various reasons.
1) I approach it in terms of models. So I stop thinking and implement the model already. A "model" is the world we are talking about basically (nevermind high tech analisys).
2) There is a mathematical relationship between the numbers and my model already visible. We talk about that later.

So off I go. I build the model first:
Code:
string Words[]={"null" ,"one", "two"};
I am not going to add more words for practical reasons. But you get the drift. Later more on this.

Now the mathematical relationship:
- Every number is a (human-like) word.
- The numbers can be used as an index for my model as it is right now. Using such an index on my model will access it's corresponding word!
Let's see the concept of that:
Code:
Number   Index   Model-usage                            Yields
0       0       MyModel[0]                             "null"
1       1       MyModel[1]                             "one"
1.01    1,1     Mymodel[1] MyModel[1]                  "one" "one"
2.15    2,1,5   MyModel[2] MyModel[10] MyModel[5]      "two" "ten" "five"
And so forth.
And this is the basis for the whole thing going on.
So if you add the word "three" to the end of the array, you'll be able to enter the number 1.03. Just try it out.

Basically I am just accessing my array by using two indexes. However, array subscripts can only be of some integer type. But my indexes are of type double. So I need to convert them first. That why it looks like that.

Why did I need the doubles, not integers straight away?
Well, because of the function modf().
Modf() splits a double into a whole part and a fractional part. But modf() requires doubles as parameters. So basically I had no choice but to make my dollars/cents variables doubles as well.

Trick
If you would add all the words for all the existing numbers, then you'll die young I'm affraid.
So we need to think twice:
What if I split my model into smaller models like this...
Code:
string sSingles[]={"one","two","three","four","five","six","seven","eight","nine"};
string sTens[]={"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string sHundreds[]={"hundred", "twohundred", "threehundred"};
And so forth.
But the sHundreds model can even be split up further, since we already defined the words "one", "two", "three" etcetera.
So this will suffice:
Code:
string sHundreds[]={"hundred"};
string sThousands[]={"thousand"};
string sMillions[]={"million"};
See?
A cool problem indeed. But quite possible to solve. Quit possible to solve it in a million ways .
See how far you can get. Such a program can end up quite complex. So start programming neat already. Make sure a stranger can actually read and understand your code. Saves me time .
__________________
Valmont is offline   Reply With Quote