The Program
In this first project we are going to build
a simple 4 function calculator using Delphi2.
Below is a view of the completed
project.
The first thig to do is create a new project
and then save it. I saved the project as PJcalc and the unit as
Ucalc.
Next place the 19 Button components on the
form and an Edit box then change the caption of each of the buttons
in the Caption property of the Properties Dialog box also change the
Font property to character size 10 and highlight Bold for the font
style.
Next in the Name property rename the number
buttons to reflect the number they contain for example the button
containing 9 would be called Num9 the button contining 8 would be
Num8 and so on.
Next change the Name property of the decimal
point button to Decimal, the C button to Cancel, the C E button to
CancelEntry, the % button to Percent, the Editbox to ReadOut, and
each of the operator buttons ie (+ - x / =) to Operator1, Operator2
etc.. Change the Caption Property of Form1 to Calculator and we are
now ready to start entering the source code.
First we need to enter some Global
Variables, (global variables are variables that can be viewed and
changed by any procedure in the program this is called the variables
scope which will be covered in later tutorials) enter the following
just bellow the {$R *.DFM} in the code generated by Delphi
var
op1, op2: Real;
Code, operator, decPoint:
Integer;
s : string;
Next we will enter code for each of the
number buttons, this is done by double clicking each buttons and then
entering code into the source code. After you double click a button
Delphi creates a procedure ready for you to enter your code into
(between the begin and
end markers) in each procedure enter the line below but
substitute the 9 for what ever number is displayed on the
button.
ReadOut.Text := ReadOut.Text +
'9';
This line begins with the name we gave to
the Editbox component (Readout) then a
decimal point which means we are going to call one of the Editbox
properties, then Text which is an
Editbox Property which displays text in an Editbox, then := which
means we are assigning the value that follows to Readout's Text
property, the next part of the statement assigns the contents of the
Editbox's Text property plus the character 9.
note:
the reason for having ReadOut.Text in the
second part of the statement is that each time we press a number key
we want that number to be added to the number already contained in
ReadOut's Text property eg if we want to enter 99 we would press the
9 button twice, but without the second ReadOut.Text
statement each time we pressed 9 the Text property would be cleared
and then 9 would be added to it).
Double click the Decimal point button and
insert the following statements
if decPoint = 0 then
ReadOut.Text := ReadOut.Text +
'.';
decPoint := 1;
The first line of code is using one of our
Global variables to see if the decimal point key has been pressed
while entering the current number, if we didn't use this check it
would be posible to place more than 1 decimal point in a number. The
next line is the same as the ReadOut.Text
statement we used earlier but this time it prints a decimal point.
The next line is another assignment statement and assigns 1 to our
Global variable decPoint this means
that if we press the decimal point key again the if statement at the
begining of this code will look at the decPoint variable
and as it now = 1 will not run the ReadOut.Text statement and return without displaying another
decimal point.
The next keys to assign statements to are
the operator keys as follows
the + key
Val(ReadOut.Text, op1,
Code);
ReadOut.Clear;
operator := 1;
decPoint := 0;
The first statement is to convert
ReadOut.Text which is a string value to op1 which is an
integer value (to find out what strings and integers are see the
manual) the Code variable at the end of the statement is not used in
this program but must still be added, ReadOut.Clear
clears the ReadOut Editbox. The next statement operator := 1 assigns
1 to our global variable operator this tell the calculator to add the
next number to the number already entered. The next statement
decPoint := 0; sets the decPoint variable back to 0 this is because
we now want to be able to enter another number which may include a
decimal point.
note: we
have now used decPoint in 2 procedures this is possible because it is
a Global variable which as previously stated can be viewed and
changed by any procedure.
the - key:
Val(ReadOut.Text, op1,
Code);
ReadOut.Clear;
operator := 2;
decPoint := 0;
These statement are the same as for the plus
key except operator is set to 2 which tells the calculator to
subtract the second number from the first.
the x key:
Val(ReadOut.Text, op1,
Code);
ReadOut.Clear;
operator := 3;
decPoint := 0;
Set operator to 3 multiply the first number
by the second.
the / key:
Val(ReadOut.Text, op1,
Code);
ReadOut.Clear;
operator := 4;
decPoint := 0;
Set operator to 4 divide the first number by
the second
the = key:
Val(ReadOut.Text, op2,
Code);
if operator = 1 then
op1 := op1 + op2
else if operator = 2
then
op1 := op1 - op2
else if operator = 3
then
op1 := op1 * op2
else if operator = 4
then
op1 := op1 / op2;
Str(op1:2:2, s);
ReadOut.Text := s;
decPoint := 0;
First the Val statement assigns the value in
ReadOut.Text to op2, then the If statement gets the value of operator
if it's value isn't 1 it moves to the else if statement these 2
statements work in partnership together to check for more than one
value (see the Delphi manual for more details), the Str statement
converts an integer to a string the oposite of the Val statement the
:2:2 tells Delphi to print the answer with a width of 2 and to 2
decimal places, the next statement assigns the string s which now
contains the answer to ReadOut's Text property, decPoint := 0 sets
decPoint so that we can print a decimal point again.
The final statement is for the C cancel
key:
ReadOut.Clear;
op1 := 0; op2 := 0;
decPoint := 0;
These statements clear the Editbox and set
the Global variables to 0.
Thats all the statements completed you can
now run the calculator and you should now have a working calculator
well almost there are a couple of things still needed to make it work
properly
1 We haven't assigned statements to the C E
and % keys.
2 The Previous calculation dosen't clear
when you start entering a new calculation so you need to press the C
key.
These will be added shortly to the
program.
The number of visitor to this page =
Return to index
page