• 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. 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

  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. 3ISE517 Internet Application Programming - CGI

    be explained: If a person wants to share his database with the world by connecting to the web, he will require a CGI program to transmit information between the web browser and the database engine. When a client requires some information from the database, his web browser sends request to the server.

  2. 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()

  1. 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

  2. software program specification and code for a DVD store

    Hours" "Total cost including VAT is totalcost+VAT" else totalcost=DVDcost*number Display "Total cost including VAT is totalcost+VAT" genre_types Display text messages "Genre types available:" "1. Sci-fi." "2. Crime." "3. Horror." "4. Romance." 2 blank lines DVD_list Call function genre_types Display "Please enter your type" Accept input of choice Switch case 1 Display "1.

  1. Evaluating Websites Usability - comparing 5 websites.

    The other links with photos are good and have good headings such as ?Basics?, News?, ?Market?, ?Science?, ?Recipes?, ?Tips? and ?Random? which are good and lead to relevant information except for 3 as discussed above which bring you to the home page.

  2. Abstract This report is dedicated to demonstrating and critically reviewing new features and developments ...

    In order to achieve the goal of running services only when they are absolutely required, the Windows SCM infrastructure was improved to enable: 1. Demand Start of a service when a specific event occurs. A service can register to be started or stopped on a supported event.

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