Simple Delphi Calculator

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only





Problem Description

Here'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 & Techniques

We'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.  

bulletWe need to keep the user from entering more than one decimal point in the number.
bulletWe 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.  
bulletTo 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.  

I think that should explain enough of the program logic, so go ahead and take look.

 Running/Exploring the Program 

bulletBrowse source extract
bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

Logical extensions include adding a "power" key (usually shown as Exp or ^ on calculator keys).  Other common keys you could add include Sqrt and 1/x, Memory buttons, plus any others you can think of.  Isn't it great?  You could have the  first calculator with a Fibonacci key, or a "Get the next larger prime number" key or a "Factor this number" key.   
You'll probably will want to rearrange the buttons to a more normal configuration.  Hint:  Hold down the shift key while you click a bunch of buttons and they will all get selected.  Then if you right click and select the "Align" menu, to can line up buttons, space them equally, etc.
There's no range checking in here, so it's probably possible to make bad things happen if you divide by 0 or  try very large results.    We'll talk about error handling in the future but if you want to look ahead, check out Try/Except structures.

Have fun and send me any interesting results!  


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.