• Join over 1.2 million students every month
  • Accelerate your learning by 29%
  • Unlimited access from just £6.99 per month

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.

...read more.

Middle

couldbe set up here, but in this case using a single string variable as the arithmetic identifier will suffice.

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.

...read more.

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!

image02.png

Wayne Broadley

03:16 October 31 2000

...read more.

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

Not the one? Search for your essay title...
  • Join over 1.2 million students every month
  • Accelerate your learning by 29%
  • Unlimited access from just £6.99 per month

See related essaysSee related essays

Related University Degree Software Engineering essays

  1. software program specification and code for a DVD store

    Display "===============" Display blank line Display "Starring: Arnold Schwarzenegger, Michael Biehn." Display "Director: James Cameron." Display "Genre: Sci-Fi & Fantasy." Display "Format: Widescreen." Display "Language: English." Display "Subtitles: None." Display 2 blank lines Display "2. Star wars: Episode 3(Revenge of the sith."

  2. Programming Concepts. Andora Video is a small shop selling videos. The owner, Raul, wishes ...

    of discount percentage given to those who buy 5-9 videos double constant disFor10To14 The decimal value of discount percentage given to those who buy 10-14 videos double constant disFor15Plus The decimal value of discount percentage given to those who buy 15 or more videos double constant VAT VAT rate multiplied

  1. Systems Development and Implementation

    In these terms, there are natural businesses formed by partitioning of the value chain as shown in the figure below The application software supplier typically bundles the analysis and development functions, working closely with the end-user organization to define requirements.

  2. MS Office solutions. Fantasy mobiles require: ...

    The list box lists all of the sales staff IDs, one of the two radio buttons should be selected to decide what calculation will be made on the sales data of the chosen salesperson. When the calculate command button is clicked then the calculation textbox will be updated according to the radio option.

  1. 3SFE504 - Object Oriented Programming - Linear and Independent Hash Table code

    { definition=""; next=NULL; } Definition ::~Definition() { } void Definition::setDefinition(string d) { definition = d; } void Definition::setNext(Definition* n) { next = n; } string Definition::getDefinition() { return definition; } Definition* Definition::getNext() { return next; } //List.h #include <string> #include "Definition.h" using namespace std; class List { public: List(); ~List(); void insertAtHead(const string); int getHead()

  2. Rich Internet Applications

    Further sections in this report focuses on this technology to use certain parts such as the action script 3.0 development to create a web application. This diagram shows where RIA's lies in the user interface experience: Figure: 2, where RIA lies [http:7] As explained there are different technologies which are

  1. Explain the Emergence of Markup Languages.

    It can be said this is one of the advantages of this language because "SGML is flexible enough to be able to describe any logical text structure, whether it be a form, memo, letter, report, book, encyclopaedia, dictionary or database."

  2. Evaluating Websites Usability - comparing 5 websites.

    It is very obvious that this page is about healthy eating, there is a picture menu with good pictures of different foods with appropriate headings. Hovering the cursor on top of these pictures enlarges the photos and form links.

  • Over 160,000 pieces
    of student written work
  • Annotated by
    experienced teachers
  • Ideas and feedback to
    improve your own work