home *** CD-ROM | disk | FTP | other *** search
- unit Calc3;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TCalcForm = class(TForm)
- DisplayEd: TEdit;
- Btn8: TButton;
- Btn6: TButton;
- Btn4: TButton;
- Btn2: TButton;
- Btn0: TButton;
- Btn9: TButton;
- Btn7: TButton;
- Btn5: TButton;
- Btn3: TButton;
- Btn1: TButton;
- BtnEquals: TButton;
- BtnDiv: TButton;
- BtnMult: TButton;
- BtnMinus: TButton;
- BtnPlus: TButton;
- BtnDot: TButton;
- ClearBtn: TButton;
- procedure Btn0Click(Sender: TObject);
- procedure Btn1Click(Sender: TObject);
- procedure Btn2Click(Sender: TObject);
- procedure Btn3Click(Sender: TObject);
- procedure Btn4Click(Sender: TObject);
- procedure Btn5Click(Sender: TObject);
- procedure Btn6Click(Sender: TObject);
- procedure Btn7Click(Sender: TObject);
- procedure Btn8Click(Sender: TObject);
- procedure Btn9Click(Sender: TObject);
- procedure ClearBtnClick(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure BtnPlusClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure BtnEqualsClick(Sender: TObject);
- procedure BtnDivClick(Sender: TObject);
- procedure BtnMultClick(Sender: TObject);
- procedure BtnMinusClick(Sender: TObject);
- procedure BtnDotClick(Sender: TObject);
- private
- { Private declarations }
- EnterNewFigures: boolean;{ flag if new number is being entered }
- { The following Methods aren't event-handlers and are
- not bound to specific visual objects }
- procedure UpdateResult( newOp : char );
- procedure AppendNumber( numCh : char );
- procedure ReInit;
-
- public
- { Public declarations }
-
- end;
-
- { TMemory is a non-visual class which simply stores the previous value
- which appeared in the calculator's edit box. Say, for example, we had
- a TMemory object called PrevVal, we can now pick and operator, such
- as '+' and then enter a new value, called NewVal. When we press the
- '=' button (or another operator button such as '+' or '-'), the edit
- box can be updated to show the total of PrevVal + NewVal ). }
- TMemory = class(TObject)
- total : real;
- function gettotal : real;
- procedure settotal( r : real );
- end;
-
-
- { Stores the currently selected operator such as '+' or '-' }
- TOperation = class(TObject)
- op : char;
- function getop : char;
- procedure setop( c : char );
- end;
-
-
- var
- CalcForm: TCalcForm;
- LastResult : TMemory;
- LastOp : TOperation;
-
- implementation
-
- {$R *.DFM}
-
- { Methods of the TMemory class. Set and read the internal variable }
- function TMemory.gettotal : real;
- begin
- gettotal := total;
- end;
-
- procedure TMemory.settotal( r : real );
- begin
- total := r;
- end;
-
-
-
- { Methods of the TOperation class }
- function TOperation.getop : char;
- begin
- getop := op;
- end;
-
- procedure TOperation.setop( c : char );
- begin
- op := c;
- end;
-
- { ------------- general-purpose routines ---------------- }
-
- { warn user if an erroneous value has been entered - e.g. '1..5' and
- allows them to edit the value before continuing }
- procedure InputError( TE: TEdit; errcode : integer );
- var
- Msg : string;
- begin
- if TE.Text = '' Then
- Msg := 'You must enter a value'
- else
- Msg := 'Invalid character: ' + Copy(TE.Text, errcode, 1);
- MessageDlg(Msg, mtError,
- [mbOk], 0);
- TE.SetFocus;
- TE.SelStart := errcode-1;
- TE.SelLength := 1;
- end;
-
- { checks to see if the value in the edit box is valid. if so,
- the value is returned in the variable, realValue and the function
- returns True. Otherwise, it returns false }
- function CurrentNumberOK( TE: TEdit; var realValue : real ) : boolean;
- var
- rv : real;
- errcode : integer;
- begin
- Val(TE.Text, rv, errcode);
- if errcode = 0 then
- begin
- realValue := rv;
- CurrentNumberOK := true;
- end
- else
- begin
- InputError(TE, errcode );
- CurrentNumberOK := false;
- end;
- end;
-
- procedure TCalcForm.ReInit;
- { Clear memory, clear edit field }
- begin
- DisplayEd.Text := '';
- LastResult.settotal(0.0);
- LastOp.setOp('+');
- EnterNewFigures := true;
- end;
-
- procedure TCalcForm.UpdateResult( newOp : char );
- { When an operator (newOp) is chosen, this method performs the current
- calculation and updates the Op field of the LastOp object so that
- this is avalable for use in the current calculation }
- var
- lastNum : real;
- lastOperator : char;
- newNum : real;
- total : real;
- strTotal : string;
- begin
- newNum := 0.0;
-
- { The code in this method only executes if the contents of the
- edit field are valid. If an error is encountered, nothing is done.
- This gives the user the chance to correct the error before
- continuing }
- if CurrentNumberOK( DisplayEd, newNum ) then
- begin
- { retrieve the previous value and operator needed for this
- calculation }
- lastNum := LastResult.gettotal;
- lastOperator := LastOp.getop;
- { use a CASE statment to select the appropriate calculation }
- case lastOperator of
- '+': total := lastNum + newNum;
- '-': total := lastNum - newNum;
- '/': total := lastNum / newNum;
- '*': total := lastNum * newNum;
- else total := lastNum; { i.e. if '=' was selected}
- end;
- { Convert the real value, total, to the string value,
- strTotal and display it in the edit box }
- Str(total:2:2, strTotal );
- DisplayEd.Text := strTotal;
- { update the lastOp and lastResult objects,
- ready for the next calculation }
- lastOp.setOp( newOp );
- lastResult.settotal(total);
- { set the EnterNewFigures variable to true. This is used in the
- AppendNumber method }
- EnterNewFigures := true;
- end;
- end;
-
- procedure TCalcForm.AppendNumber( numCh : char );
- { If a calculation has just been completed, the EnterNewFigures
- variable is True. So the edit box is cleared to let the user
- start entering a new number. Otherwise, digits are appended
- to the contents of the edit box }
- begin
- if EnterNewFigures = true then
- begin
- DisplayEd.Text := '';
- EnterNewFigures := false;
- end;
- DisplayEd.Text := DisplayEd.Text + numCh;
- end;
-
- { the form's event-handling code }
- { Each button sends a number to be added to the edit box }
- procedure TCalcForm.Btn0Click(Sender: TObject);
- begin
- AppendNumber( '0' );
- end;
-
- procedure TCalcForm.Btn1Click(Sender: TObject);
- begin
- AppendNumber( '1' );
- end;
-
- procedure TCalcForm.Btn2Click(Sender: TObject);
- begin
- AppendNumber( '2' );
- end;
-
- procedure TCalcForm.Btn3Click(Sender: TObject);
- begin
- AppendNumber( '3' );
- end;
-
- procedure TCalcForm.Btn4Click(Sender: TObject);
- begin
- AppendNumber( '4' );
- end;
-
- procedure TCalcForm.Btn5Click(Sender: TObject);
- begin
- AppendNumber( '5' );
- end;
-
- procedure TCalcForm.Btn6Click(Sender: TObject);
- begin
- AppendNumber( '6' );
- end;
-
- procedure TCalcForm.Btn7Click(Sender: TObject);
- begin
- AppendNumber( '7' );
- end;
-
- procedure TCalcForm.Btn8Click(Sender: TObject);
- begin
- AppendNumber( '8' );
- end;
-
- procedure TCalcForm.Btn9Click(Sender: TObject);
- begin
- AppendNumber( '9' );
- end;
-
- procedure TCalcForm.ClearBtnClick(Sender: TObject);
- begin
- ReInit;
- end;
-
- procedure TCalcForm.FormActivate(Sender: TObject);
- { When the calculator is first run, we create the two
- objects, LastResult and LastOp and call ReInit to do some
- setup tasks }
- begin
- LastResult := TMemory.Create;
- LastOp := TOperation.Create;
- ReInit;
- end;
-
-
- procedure TCalcForm.FormClose(Sender: TObject; var Action: TCloseAction);
- { When the calculator is closed, we 'clean up' by destroying the objects
- we created in the FormActivate method }
- begin
- LastResult.Free;
- LastOp.Free;
- end;
-
- { The operator buttons }
-
- procedure TCalcForm.BtnEqualsClick(Sender: TObject);
- begin
- UpdateResult( '=' );
- end;
-
- procedure TCalcForm.BtnDivClick(Sender: TObject);
- begin
- UpdateResult( '/' );
- end;
-
- procedure TCalcForm.BtnMultClick(Sender: TObject);
- begin
- UpdateResult( '*' );
- end;
-
- procedure TCalcForm.BtnPlusClick(Sender: TObject);
- begin
- UpdateResult( '+' );
- end;
-
- procedure TCalcForm.BtnMinusClick(Sender: TObject);
- begin
- UpdateResult( '-' );
- end;
-
- procedure TCalcForm.BtnDotClick(Sender: TObject);
- begin
- AppendNumber( '.' );
- end;
-
- end.
-