|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis is the first and theoretical part of the series of tutorials to develope a calculator. In this series I want to write a fully working calculator application in C++. It's planned to support different numeric systems, brackets and functions available on scientific calculators. Let's start. Last year in a sleepless night I asked my self, how is a calculator working? At this time I was developing a small stack based script engine for my Jump’n’Run framework. After finishing it, I thought about if a calculator could be done with a stack. Next day I searched the web to figure out how a calculator is working in general, but I didn’t find any good explanation(Actually till today I don't know if my way is the common way, or if there is something much better or easier). So, I decided to give it a try. Without knowing anything about it, I simply started by analysing the behaviour of my calculator. The ProblemThe question that I was interested in most, was what are the rules for the calculation of a term, if you don't know the whole term. Let me show you this problem: Analysis
I started to analyse the behaviour of my calculator(TI-30Xa) and figured out some points: At this point we just care about these operations +, -, *, /, (, ).
STRUMBER - STRing/nUMBERWhen we start to enter a number the displayed number behaves like a string. Let's try to enter 1024 on a calculator. We start pressing #1, #0, #2 and #5. What happened here? These numbers got concatenated. Hm, I think we mistyped a number. The display shows 1025 instead of 1024. No problem, we use the back key and the display shows 102. The digit 5 is cut off. Now press #4 and we see 1024. Pressing number keys never causes any calculationThere is nothing to say, try it. You can enter any number you want, the calculator will never calculates anything. Pressing one of the operator keys sometimes causes calculation.Write down some terms and enter them in your calculator. Whenever you see any calculation note a "|" at the corresponding position in your notes.
Calculation systemI developed this system by trial and error. I started with the simple idea to push the numbers and the operators on their stacks and try to calculate the term. If I saw the system failing, I figured out the point why it’s not working and changed it. After a while and some cups of tea it got better and better. And finally it worked. Try a calculationThis is the most interesting point of the whole system. The calculation itself is quite simple, it works with the following rules: var op:String = _opstack.pop().toString(); var p2:Number = Number(_numberStack.pop()); var p1:Number = Number(_numberStack.pop()); switch(op) { case "/": _numberStack.push( p1 / p2 ); break; case "*": _numberStack.push( p1 * p2 ); break; case "-": _numberStack.push( p1 - p2 ); break; case "+": _numberStack.push( p1 + p2 ); break; }
while (
getPriority( nop ) <= getPriority(_opstack[_opstack.length - 1])
&& _pol >= getPriority(_opstack[_opstack.length - 1])
&& _opstack[_opstack.length - 1] != "("
) {
... calculation
}
Points of InterestProbably you may want to know why I wrote this article. Last year I have written a calculator in Actionscript in Flash. But currently I am learning C++ and wxWidgets and for that, I needed a small project to realize. And that's the reason why I rewrite this calculator. On top of that, I thought it could be interesting to other people to see how it works and so, I wrote this article.If you know a different system to make a calculator working, please let me know. History2008-10-15I have got some poor ratings. I read the article and recognized that I formed the main question in a wrong way. (Can a calculator be done by using stacks). And I didn't point out what I am going to do in this series. Content didn't change much, but hopefully it's better now.2008-10-12First release.
|
||||||||||||||||||||||||||||||||||||||||