Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMercile55

Posted 21 January 2013 - 06:07 PM

There are parsers, i.e. lex and yacc that would help you to create proper parser, but if you want to do it from scratch, all by yourself, then go ahead.

Instead of doing it step by step / token by token, allow user to type if entire formula, i.e. 5+5/2
Then tokenize entered text, by splitting it into functional entities, and verify its structure, rejecting when suspicious.
i.e.
1. 5
2. +
3. 5
4. /
5. 2
(if you want to handle proper operation order, like *, /, +, -, then make sure that you order entities properly. RPNis the best choice of notation, which orders entities in a simple stack format)
then do the operations in the proper order, between the entities.
i.e.
3,4,5: 5 / 2 => 2.5 and gives:
1. 5
2. +
3. 2.5
... etc

#1Mercile55

Posted 21 January 2013 - 06:06 PM

There are parsers, i.e. lex and yacc that would help you to create proper parser, but if you want to do it from scratch, all by yourself, then go ahead.

 

Instead of doing it step by step / token by token, allow user to type if entire formula, i.e. 5+5/2

Then tokenize entered text, by splitting it into functional entities,

i.e.

1. 5

2. +

3. 5

4. /

5. 2

(if you want to handle proper operation order, like *, /, +, -, then make sure that you order entities properly. RPN is the best choice of notation, which orders entities in a simple stack format)

then do the operations in the proper order, between the entities.

i.e.

3,4,5: 5 / 2 => 2.5 and gives:

1. 5

2. +

3. 2.5

... etc


PARTNERS