[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionHere's a simple 4 function calculator program that was written to meet some specific requirements. It's an interesting story: The calculator was written for the psychology department of a university for use in testing primary school children. Since is to be used on screen with a PowerPoint presentation (PowerPoint is Microsoft's slide show program), the requirement was that it be kind of wide and short. That way it will fit across the bottom of the screen. Also, since the kiddies may not key very well, they wanted the keystrokes to be displayed as well as the answers. Version 2 of this program turned out to be considerably more sophisticated, including ability to show or hide buttons, no * or / buttons for example if the problems were all + and - type. Also we needed a log file to record keystrokes and the time between entries (don't ask me why, I'm just the programmer). And I'm currently adding the ability to control the slideshow from within the program, by letting the kids click on a Next button to go to the next slide. But for now, let's work on the simple version. Background & TechniquesWe're going to build a normal left to right calculator that processes numbers as they are entered. We'll discuss below how the requirement to display the entries makes things a little trickier. I just spent 30 minutes documenting the algorithm here, but erased it all because I realized that the program is the algorithm. 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 is not much difference in processing the numeric keys, so I wrote an AddDigit procedure to display the digit and add in on to the string version of the number being built. Similarly, most of the operation handling is similar, as long as we remember to add, subtract, multiply or divide when the time comes. So we have a HandleOp procedure for those things. Note that we can't really do anything when the user enters the operation because we don't have the second value yet. So, we'll just hold on to it until we need it. We also know the the user has finished entering a value when we see the operation code, so we can process it according the operator that preceded it and get ready to build the next number. 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.
I think that should explain enough of the program logic, so go ahead and take look. Running/Exploring the Program
Suggestions for Further Explorations
Have fun and send me any interesting results!
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |