- Level: University Degree
- Subject: Mathematical and Computer Sciences
- Word count: 4712
Programming a calculator in Delphi.
Extracts from this document...
Introduction
Programming a Calculator in Delphi
By Wayne Broadley
Introduction
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.
Design
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.
Middle
Okay, so now when one of the arithmetic buttons is pressed, a block of code needs to be executed. The first thing in this block of code should be to check whether a valid value has been entered into the Display Screen. As this will need to be done for each operator's On Click event, it's worthwhile implementing the validity check as a separate procedure. We'll call it CheckIfValid. The procedure is declared in the normal way (added to Public Declarations), and the definition of the procedure is as following:
procedure TForm1.CheckIfValid;
begin
if DisplayScreen.Text = '' then DisplayScreen.Text := '0';
end;
Basically, all this does is check to make sure that a number has been entered before the code for the operator's On Click event is carried out. If a number hasn't been entered (i.e. the string is left empty) then it's set to 0 by default. This prevents a run-time error occur and also makes the calculator run more realistically.
The implementation of each operator button's On Click event is then as follows:
procedure TForm1.AddBtnClick(Sender: TObject);
begin
CheckIfValid;
x := x + StrToFloat(DisplayScreen.Text);
Arithmetic := 'Add';
end;
procedure TForm1.SubtractBtnClick(Sender: TObject);
begin
CheckIfValid;
x := x + StrToFloat(DisplayScreen.Text);
Arithmetic := 'Subtract';
end;
procedure TForm1.MultiplyBtnClick(Sender: TObject);
begin
CheckIfValid;
x := x + StrToFloat(DisplayScreen.Text);
Arithmetic := 'Multiply';
end;
procedure TForm1.DivideBtnClick(Sender: TObject);
begin
CheckIfValid;
x := x + StrToFloat(DisplayScreen.Text);
Arithmetic := 'Divide';
end;
Let's take a close look at this code, starting with the first block. The first step in each block calls the CheckIfValid procedure to make sure that a valid number is stored.
Conclusion
Now, I'll take a look at the more advanced features of the calculator that I could implement, but won't be spending too long on this section due to lack of time.
Adding pi as a function button
This is really easy to tell the truth. The pi button has been added to the form and the OnClick event has been filled with the following:
procedure TForm1.PiBtnClick(Sender: TObject);
begin
ClearDisplay;
UpdateDisplay('3.141592654');
end;
Simply, the current display is cleared, and the value of pi is entered, allowing the user to interact with the value of pi as they so wish.
Squaring a number
Again, really straightforward:
procedure TForm1.xsqdBtnClick(Sender: TObject);
begin
x := StrToFloat(DisplayScreen.Text);
Answer := x*x;
DisplayScreen.Text := FloatToStr(Answer);
ResetValues;
end;
Square rooting a number
procedure TForm1.sqrtBtnClick(Sender: TObject);
begin
x := StrToFloat(DisplayScreen.Text);
Answer := sqrt(x);
DisplayScreen.Text := FloatToStr(Answer);
ResetValues;
end;
Adding sin, cos and tan
This is a difficult part of the coding, not in implementing the features but in converting radians to degrees as needed. For now, I have left the answers as radians. If I had more time, I'd be able to convert them to degrees and may even consider doing so in a future version of the calculator. But for now, here's the final snippets of code:
procedure TForm1.SinBtnClick(Sender: TObject);
begin
ClearDisplay;
Arithmetic := 'sin';
end;
procedure TForm1.CosBtnClick(Sender: TObject);
begin
ClearDisplay;
Arithmetic := 'cos';
end;
procedure TForm1.TanBtnClick(Sender: TObject);
begin
ClearDisplay;
Arithmetic := 'tan';
end;
and planted in the EqualsBtnClick event procedure:
if Arithmetic = 'sin' then
begin
Answer := sin(StrToFloat(DisplayScreen.Text));
end;
if Arithmetic = 'cos' then
begin
Answer := cos(StrToFloat(DisplayScreen.Text));
end;
if Arithmetic = 'tan' then
begin
Answer := sin(StrToFloat(DisplayScreen.Text)) / cos(StrToFloat(DisplayScreen.Text));
end;
To convert to degrees, I would use the fact that 2pi is equal to 360 degrees, but for now radians will have to do!
Wayne Broadley
03:16 October 31 2000
This student written piece of work is one of many that can be found in our University Degree Software Engineering section.
Found what you're looking for?
- Start learning 29% faster today
- 150,000+ documents available
- Just £6.99 a month