There is a task to implement a calculator which receives as input numbers and operators string format, converts them to numbers, and output the number. Example: **sixty-two** *plus* **forty-four**, and the answer received **104**. And if the calculator is all more or less clear, but as best (and as just) allow text data to numbers, it is not clear at all. Maybe there are some ready solutions? I will glad of any hint in what direction to dig.

asked March 23rd 20 at 19:34

2 answers

answered on

Solution

Here "fish" )).

Parilka one number. type 23, 42, 54, 35, etc. All the units, tens and hundreds of lazy writing.

But this is the easiest case, what about the billions , thousands, etc.?

Doing the split line for the regular season where the delimiters 'thousand' , 'million' and other complex cases. Get sets where the last element or multiplier or just a number.

You can then use the same mechanism where possible, is added to the multiplier. Example -

twenty-six million three hundred twenty-six

after the split we get two sets :

1) twenty six million

2) three hundred twenty-six

check the last element in each set.

the first is "million". A special case of the multiplier!

Drop the last element (multiplier) . and recognize 26 as the usual number, then "million" - gives us the multiplier 1000 000. the resulting number 26 * multiplier (1000 000). = . 26 000 000.

the second set at the end just a number. approx. standard conversion.

The last phase is the sum of two numbers.

It's the General idea. Of course , there are nuances , but even more interesting )))

For example , validation - "Two thousand five" - ok , but "two thousand Five" no. Although the above algorithm is eat. It is also possible to think of something, and I think even a fairly simple algorithm for checking the validity of the entered text number (is the idea). Space for creative ideas!

Good luck

Parilka one number. type 23, 42, 54, 35, etc. All the units, tens and hundreds of lazy writing.

But this is the easiest case, what about the billions , thousands, etc.?

Doing the split line for the regular season where the delimiters 'thousand' , 'million' and other complex cases. Get sets where the last element or multiplier or just a number.

You can then use the same mechanism where possible, is added to the multiplier. Example -

twenty-six million three hundred twenty-six

after the split we get two sets :

1) twenty six million

2) three hundred twenty-six

check the last element in each set.

the first is "million". A special case of the multiplier!

Drop the last element (multiplier) . and recognize 26 as the usual number, then "million" - gives us the multiplier 1000 000. the resulting number 26 * multiplier (1000 000). = . 26 000 000.

the second set at the end just a number. approx. standard conversion.

The last phase is the sum of two numbers.

It's the General idea. Of course , there are nuances , but even more interesting )))

For example , validation - "Two thousand five" - ok , but "two thousand Five" no. Although the above algorithm is eat. It is also possible to think of something, and I think even a fairly simple algorithm for checking the validity of the entered text number (is the idea). Space for creative ideas!

Good luck

answered on March 23rd 20 at 19:38

Parsing, parsing again and parsing...

Not so difficult to list all possible variants of words for numbers, for example...

1: one, one

2: two two

etc.

the most difficult thing wakes desyatkami and hundreds of...

But just understand the structure.

10:ten (on)dtsat and other forms...

100 - STO, STI, by analogy with 10

And there multiply the number of Yes change to the sign

Not so difficult to list all possible variants of words for numbers, for example...

1: one, one

2: two two

etc.

the most difficult thing wakes desyatkami and hundreds of...

But just understand the structure.

10:ten (on)dtsat and other forms...

100 - STO, STI, by analogy with 10

And there multiply the number of Yes change to the sign

Find more questions by tags JavaScript

ONE billion

One HUNDRED! THOUSAND! million.

I would think myself, they are repeated. - shayna.Kirl commented on March 23rd 20 at 19:44