S
S
SvetlyiAkaPro2014-01-30 11:54:50
Algorithms
SvetlyiAkaPro, 2014-01-30 11:54:50

How to design calculators that count expressions like 2+4*(4-3)?

I would like to know if there is any literature about such algorithms, or manuals that allow you to understand how to do things like such calculators. Any language.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
Codebaker, 2014-01-30
@SvetlyiAkaPro

I'm surprised by the comments! How is it not met with literature, if there are fundamental works on this topic. In addition, there are a lot of topics on Habré (I give the first three, you can promote all the rest by tags):
habrahabr.ru/post/140058
habrahabr.ru/post/108062
habrahabr.ru/post/133780

S
SvetlyiAkaPro, 2014-01-30
@SvetlyiAkaPro

But there are books with algorithms, rules for their construction, something like that?

S
Sergey, 2014-01-30
@begemot_sun

You can use a parser generator to create an AST tree and then iterate over it, evaluating the expression.
Here is an example of such a generator in JS.
Here is an example calculator.
And here is a working port of the generator in PHP with a complex example.

A
Alexander, 2014-01-30
@nwb

JS (never use!!)
alert(eval(prompt('Enter an expression')));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question