Calculator Design - YiZhang-Paul/Mock_Up_Calculator GitHub Wiki
Though calculators supported in this application vary in terms of behaviors and functionalities in some aspects, their core design idea and the data flow is largely similar:
input data -> expression builder -> expression parser (into a parse tree) -> evaluate result
To better illustrate the process, we will take the example of a series of input ["5", "+", "6", "log"];
- input data will be fed into an expression builder which builds up a valid math expression, e.g. above input stream will be transformed into expression: "5 + 6 log";
- the expression builder will proceed to parenthesize the expression which can be parsed into a parse tree, e.g. the above expression "5 + 6 log" will be parenthesized into "( 5 + ( 6 log ) )";
- the parenthesized expression will be passed into a parser, which will parse the expression into a parse tree, illustrated below:
- finally, the evaluator will traverse the parse tree recursively and calculate result of the expression.