Skip to content

LMsgSendNilSelf/AlgebraicEngine-Fraction

Repository files navigation

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:

  1. Tokenizer: Tokens:1, +, 2, *, (, 3, -, 4, )

  2. Interpreter: Specify the vague tokens,i.e '+' means add or positive ,the same as '-'.

  3. 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:

  1. 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

  2. because of object-c without function operator,we need to convert operators to function name, i.e @"+" to "add"

Todo :processing more symbol