Programming a Calculator in Delphi

By Wayne Broadley


The aim is to produce a fully working calculator program that incorporates as many mathematical features as possible. The program is to be created using Delphi, an IDE based on the Pascal language. I personally prefer to program in C++ as I have quite a lot of experience with it. However, this seems a worthwhile opportunity to develop my skills in Delphi, of which I have used in the past, and have found it to be a very useful language especially for creating small utilities (such as calculators) and database-orientated applications.

At first, it’s unusual programming in Delphi after using C++ for so many of my past programming projects (and also Java, which has fairly similar syntax to C++). However, it’s been pretty easy to get back into familiarity with the language. Pascal is quite a bit simpler than C++ and so it hasn’t been much of a problem at all. It’s just a matter of getting some of the small differences correct, such as assigning variables and syntax order. In fact, I have enjoyed using Delphi quite a lot; I especially like the ease of use of ‘procedures’. They are quite a lot easier to implement than some of the complex ‘functions’ used in the C/C++ programming languages.


This project can be as simple or as complicated as I’d like to make it. Sometimes keeping it simple is best – a nice, clean interface with a fully working, yet simple, calculator is much better than a bug-ridden calculator with a messy interface, which includes numerous advanced features that don’t work properly. I’ll have to try and get the correct balance between each. Obviously the more features I can implement into the calculator (for example, square rooting, trigonometric ratios, memory features) then the better, but I need to make sure that they work properly and that they don’t hinder the reliability of the program in any way.

The first thing I need to do is get the simplest form of a calculator up and running: a calculator that can add, subtract, multiply and divide. This in itself can take some time, but once I have accomplished this then I have a firm base from which to expand and include further features. My main aim (apart from getting it to actually work!) is to make the programming as tidy and efficient as possible. Instead of having messy, hard to read code, I want to try to make the code progressive and easy to follow. To do this, I intend to comment my code thoroughly where needed. The calculations involved may well get rather complicated hence I will try and organise the code to the best of my ability.

The first thing that comes to light when designing the calculator is the layout. I need to decide what I’d like on the calculator, and specify what each button is to do. Designing the calculator like this beforehand will pay off later when it comes round to the coding. Included overleaf is an initial sketch of how I’d like the calculator to appear and work in its simplest form. Obviously, the further I get into the project, the more likely my initial design is to change as I implement further features, or change things that don’t fit for some reason etc.

Now that I’ve got a small idea in my head of how I want the calculator to look, I can get on with the initial implementation phase. This will basically involve creating the layout of the calculator in Delphi. At that point, I will take a screenshot of the calculator and then advance from there.

Also, I’ve decided to call the calculator ‘Easy Calculator v1.0’. This is a good, simple name, and I’ve included a version number in case I’d like to produce a newer version of the finished program in the future (yeah right ).

Annotated sketch of Simple Calculator (1st Draft)


Below is a screenshot of an early stage in the development of the calculator:

So far, all that has been considered is the layout and preparation for the proper coding that is to come to add functionality to the calculator. In the screenshot, there is a ‘basic’ layout of the calculator, which includes all the features that you would expect in any standard calculator. Also included is a Main Menu at the top of the page, which is empty at the moment, but will be filled later for further interactivity and ease of use. Already, the initial design has changed slightly.

All the objects on the form have been given suitable names so that they are easily identifiable and so that it’s easier to work with.

Now it’s time to get dirty and into some proper programming! I would like to start off by getting the basic features working first. After analysing the problems presented, I’ve managed to put together a list of the problems that need to be solved in order to get the basic calculator up and running on the following page.

Writing the Program

  1. The default value in the display screen should be ‘0’

This is a simple problem to start off with. All I need to do is simply set the ‘Text’ property of ‘ValueDisplay’ to equal ‘0’. This can be achieved by using the Object Inspector to edit the property. It could also be done at run-time, but for simplicity it is done at the design stage (see left).  

  1. By clicking a number, the number needs to appear in the display screen.

This problem is the first real task that needs to be solved. When a user clicks on one of the buttons on the form, the number needs to be added to the string in the Display Screen. “DisplayScreen” is an edit box and has a property called ‘Text’. The text property is what is modified when we want to change the value seen in the display.

Join now!

For example, the following piece of code would simply empty the text in the Display Screen:

DisplayScreen.Text := '';

However, what we want to do is add a number to the current string of text stored in the text property. This is done by using the '+' symbol to concatenate the two separate strings.

The best thing to do is to create a user-created procedure that handles updating the display screen when a number is clicked. This procedure will be called simply 'UpdateDisplay'. The first thing to do is add the declaration of this procedure to the <public declarations> section ...

This is a preview of the whole essay