Thread: this more of a general question than symbian specific

1. Hi
I want to do an intersting conversion , If someone inpts one hundred and thirty one , I would like to convert it to integer 131 , how hould i go with jit? i have successfully extraced the components into 1 100 30 and 1 as strings , not so close yet any one has any suggestions?-
Julie

2. The basic idea is that you have to track and compare the range ("magnitude"?) of the current number and the previously "built" one, and in the meantime you also have to track the magnitude of the current "component" you are building.

1 100 30 1
x=0
1<100 => x+=1*100
100>30 => x+=30
30>1 => x+=1
x=131

With these small numbers there was no need for tracking components, because they were a single "digit" only (100-200-...-900, 10-20-...-90, 1-2-...-9)
However in case of larger numbers, "components" has to be tracked, like in the case of 30 2 1000, 30 and 2 has to be added, and the entire result has to be multiplied with 1000 in order to get 32000.
Probably tracking the previous multiplier also makes sense. like in "1 1000000 2 100 30 8 1000 5 100 20 8" 100 is smaller than 1000000 (which is expected), but 1000 is suddenly larger than the 100, so the 1000 multiplier applies to 200, 30 and 8. But it does not apply to the 1 000000.
The previous "large" multiplier may have to be remember too.

3. so we cannot go with a simple rule to support all numbers right? have to do multiple passes over the input statement to figure out what is to be multiplied and what is to be added right?

4. This sort of problem is normally tackled with a grammar and parser to build an expression tree, then an evaluation of that tree. The grammar could be defined recursively, something like this (using | to mean alternative, [] to mean optional, () to mean evaluation):

Zero = "zero"(0) | "nothing"(0)
Units = "one"(1) | ... | "nine"(9)
Tens = Units(UnitsVal) | "ten"(10) | ... | "nineteen"(19) | "twenty" [Units](20 + UnitsVal) | ... | "ninety" [Units](90 + UnitsVal)
Hundreds = Units "hundred" ["and"] Tens(UnitsVal * 100 + TensVal) | Tens(TensVal)
Thousands = Hundreds "thousand" Hundreds(Hundreds1Val * 1000 + Hundreds2Val) | Hundreds(HundredsVal)
..and so on till you get to...
Number = Zero(ZeroVal) | Zillions(ZillionsVal)

Pick your favourite parsing and evaluation methods.

5. Number 123 456 789. Rewritten: 1 100 20 3 1000000 4 100 50 6 1000 7 100 80 9.
Expected evaluation (1*100+20+3)*1000000+(4*100+50+6)*1000+7*100+80+9
Relations: 1<100>20>3<1000000>4<100>50>6<1000>7<100>80>9
Apparently < has something to do with * and > has something to do with +. Just there are parentheses to make things worse.
Let us try building a stack.
As a first approach < will really mean *, we do the multiplication in place
1
1*100
> means push
100 20
100 20 3
now comes the 1000000, which is again a "<" step. However having 100 20 3000000 in the stack is not exactly what we want. So the rule should be completed with a stack collation step: we add the elements in the stack, as long as they are smaller than the new element (1000000), and after that we do the multiplication in place
123*1000000 is the new stack
123000000 4
here comes 100, we apply the new rule, though the sum is not visible, the 123000000 remains intact, since it is not smaller than 100
123000000 400
123000000 400 50
123000000 400 50 6
now comes the 1000, so 6, 50, 400 is added together and the result is multiplied with 1000
123000000 456000
123000000 456000 7
123000000 456000 700
123000000 456000 700 80
123000000 456000 700 80 9
and at the end, we add together all elements in the stack
123456789, done.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.