Algebraic Engine (Fraction)
Why write this engine?
The reason is I coded a supercalculator last year in Appstore, which is based on one more completed parser.
This parser is just a simplified prototype. It calculates not only fraction but normal expression, so you can enrich this library in normal calculation. Detail illustration will be added in the near future.
Parser Process:
For example:1+2*(3-4) parsing process:
-
Tokenizer: Tokens:1, +, 2, *, (, 3, -, 4, )
-
Interpreter: Specify the vague tokens,i.e '+' means add or positive ,the same as '-'.
-
Parser
AST(抽象树):
In fact, the math expression is parsed into one custom nested cluster in coding,and then resolve sub-expression on each level from inside to outside one.
Tips:
-
adjust number, which is regarded as fraction through process,such as
2 convert to 2/1 ; 0.2 convert to 2/10; 0.9998 convert to 9998/10000
-
because of object-c without function operator,we need to convert operators to function name, i.e @"+" to "add"
Todo :processing more symbol