Pascal is readable enough that browsing the code will be a more understandable description of the process than anything I can write in natural language. Just recognize that Windows is a messaging system so typically programs just hang out doing nothing until they get a message. When you see routines like DigitBtnClick, PlusBtnClick, etc. that routine is the one that was called when that button was clicked.
There's a Reset procedure that's called to clear out the displays and reset internal flags, etc. We have to do this on initial input, or if the user presses Clear, or when the user presses the next digit after he has pressed the = key.
The program has 55 or so user written lines of code, more than I would have guessed. But there area few tricky things.
We need to keep the user from entering more than one decimal point in the number. | |
We need to handle the display intelligently when * or / are mixed with + or -. In your normal calculator and in this one, if you enter 1, +, 2, *, 3, the result is 9 because 1+2 = 3 and 3* 3 is 9. But because of the precedence rules of arithmetic, multiplication and division in expressions are performed before additions ,and subtractions, so 1+2*3 written as an expression should = 7. In this program, we solve the problem by inserting parentheses in the expression, so the above example is displayed as (1+2)*3=9. | |
To reduce the number of lines of code, I used a trick that I probably would not use in a more complex project: Rather than have 10 different routines to process the 0 to 9 buttons, I named them Btn0, Btn1, Btn2, etc and extract the 4th character of the name as the desired digit in a common routine. |
Running/Exploring the Program
Download source | |
Download executable |
0 comments: