An Update on the Calculator: Now with Parentheses



This second edition of the calculator took longer than expected. Originally, I thought it was going to be some complex, but easy to implement, code. However, it came to appear almost as an impossible task. Worse, for almost a month, our professor has not had the situation where he could come to class, so for most of the time since the last post we had to work on our own.

However, after several written up concepts and reviews from others students and failed attempts, I've coded up this new calculator. Here it is on Github.

The prior calculator was very basic, it did less than a calculator one could get for free at a convention. The code for it was too simple, the new calculator had to be mostly remade or greatly modified at several key places. However, I did get this completed only a few days ago.

The new calculator is quite close to a basic scientific calculator. It can now do order of operations. One can input several numbers and operators, and the calculator will follow the order of operations when calculating and returning a result. With the order of operations, and taking up most of the difficulty of this new version, the calculator now handles using parentheses. An equation like 3 * (2 ^ 2) + (5 - (9 / 2) * 3) would be solvable.

Here's a basic explanation of how the calculator works:
  • You enter numbers and operations as would on a regular calculator: enter a number then press an operator. When you're ready to calculate, press the enter button.
  • When you hit an operator number, the number displayed will be added to a list of numbers and the operator will be added to a list of ops.
  • When calculating the equation, the calculator will look for sets of parentheses, specifically the first closed pair of parentheses. Once found, it shall put all the operators and numbers between the parentheses into temporary number and operator lists.
  • The calculator computes the results of what is within the parentheses and then replaces all between the parentheses with the result.
  • The calculator shall repeat this process with all other sets of parentheses. When none are left or there were none to begin with, the calculator shall compute the results from the remaining operators and lists.
  • There are special functions like factorials and 1/x, e, and pi. These functions display a new number, as with e and pi, or the result of the displayed number going through these. 
The calculator is still rudimentary in design. I have yet to add proper error messages, so dividing by 0 is a great problem. I have to implement some more functions such as trigonometric functions and  logarithms. These future functions may require me to rebuild the process of doing equations again entirely.

I've also had to work on the display. On top of the display box would be where the entire equation be displayed, and it would update with each time you press an operator. It works with equations without parentheses only. Thus, I've disabled this feature as it would not work.

However, it seems I can put working on this calculator on hold for the time being. While the professor hasn't been around face-to-face, he has messaged us to do some programs. I may show one of these next time, so stick around for the next post. Goodbye.

Comments

Popular posts from this blog

Version 2:The Not-So Barebones Clicker

A Prime Factorization Algorithm that Uses a Text File

Aesthetico Ultimate Designer - A Python Turtle Program