How I built a Step-By-Step Calculator
Bored of using the same old Android calculator? Which only gives the answer but no solution.
What if you are a middle-school student who has got homework from the mathematics teacher to solve arithmetic equations. Well, Android calculator can’t come handy here as you won’t know at which step you are going wrong.
No worries! Today I would like to share with you how I developed a desktop calculator application which not only would give answer but also provide step by step solution.
Here are some screenshots of the application.
As we can see the application is capable of performing basic arithmetic operations as well as trigonometric functions, factorials, square roots and other operations. The application also displays the Status and Number of Steps it took to solve the given equation.
Continuing I would like to discuss how the application works.
First, it works on the basic mathematics rule of BODMAS (which means, Bracket Of Division Multiplication Addition Subtraction).
Now, a detailed explanation.
Input is taken in the form of String from the input field. Then it is broken to nodes of data representing numbers and symbols.
Once we have achieved this level now its time to associate the numbers as positive and negative.
By which I mean that if the preceding node of a number is ‘+’ symbol then the number is a positive number. Similarly, if the preceding node of a number is ‘-’ symbol then its a negative number. In this step we remove preceding +/- symbols and associate these with the following numbers.
Once this step is done it becomes easier to see the equation.
Note: Example photos are not of the same equation. And the input equation is converted to doubly-linked list in first step itself.
Now, we will solve the problem using the rule of BODMAS. First we will look for a bracket. We will enclose entire equation inside a bracket.
From now the road becomes easier. We find brackets (opening and closing) and send it to a function to solve the equation inside the bracket. Here, in this example since there are no brackets we will send the entire equation (as it is enclosed within a bracket).
There the equation is taken and solved one symbol at a time. Firstly, DM of BODMAS is solved i.e; Division and Multiplication and new node is created which holds the answer. At last new links are made.
Left to right traversing is done. When a ‘/’ symbol is encountered then the values of the preceding and succeeding nodes are taken and computed. A new node is created (blue colored block containing 28)which holds the result of the calculation. At last, new links are created (deep blue arrow marks). And the traversing continues.
After traversing one time in search of ‘/’. Next, the doubly linked list is again traversed from the beginning in search of multiplication(‘*’).
Same steps as of division. Again the traversing is done in search of ‘*’. Once it’s done then part of the linked list which is within brackets is free of multiplication and division.
At the end we start adding the consecutive numbers.
Finally we have the result.
After every BODMAS operation, the current equation is printed in Solution layout of the app. And for the next time the doubly linked list is traversed from the beginning.
Note: The symbols pi, e, square root etc are solved first before +,-,/ and * but with the same procedure. Pi and e would be converted to their numerical values.
The doubly linked list will be traversed util NULL <- [ ‘value’ ] -> NULL is found!
One short note. If there are more than one brackets in the equation then one by one those equations would be solved and the result of the bracketed equation will be linked with the outer equation.
Click here to go to the GitHub link.
Have a great day ahead.
Thank you.