Objectif :
L'interface
de calcul est sécurisée au premier niveau
avec adjonction d'exceptions.
La fiche
principale, le code associé dans la unit
Exécuter
le programme
La fiche principale du projet :
Le code de la Unit fiche principale :
unit Usolexc;
{avec exceptions}
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
Editnbr1: TEdit;
Editnbr2: TEdit;
Labelnbr1: TLabel;
ListBoxOperation: TListBox;
Editmessage: TEdit;
LabelResultat: TLabel;
Labelnbr2: TLabel;
Label1: TLabel;
ButtonCalcul: TBitBtn;
Labeltypoperat: TLabel;
Panel1: TPanel;
Editresult: TEdit;
procedure FormActivate(Sender: TObject);
procedure Editnbr1Change(Sender: TObject);
procedure Editnbr2Change(Sender: TObject);
procedure ListBoxOperationClick(Sender: TObject);
procedure ButtonCalculClick(Sender: TObject);
private
{ Déclarations private
}
public
{ Déclarations public
}
end;
var
Form1: TForm1;
App_Path:string;
oper1,oper2,operat:boolean;
Toper:array[0..5]of string;
implementation
const Maxint=32767;
{$R *.DFM}
{******************** procédures internes
*********************}
procedure TestEntrees;
{positionne le bouton si les drapeaux sont
levés}
begin
if oper1 and oper2 and operat then
Form1.ButtonCalcul.enabled:=true //
si oui: le bouton calcul est activé
end;
procedure RAZTout;
begin
with Form1 do
begin
ButtonCalcul.enabled:=false;
//
le bouton calcul se désactive
Editnbr1.clear;
Editnbr2.clear;
Editresult.clear;
Editmessage.clear;
oper1:=false;
oper2:=false;
operat:=false;
end
end;
{***************************************************************}
procedure TForm1.FormActivate(Sender: TObject);
begin
RAZTout;
Toper[0]:='*'; Toper[1]:='/';
Toper[2]:='+'; Toper[3]:='-';
Toper[4]:=' div '; Toper[5]:=' mod ';
end;
procedure TForm1.Editnbr1Change(Sender: TObject);
begin
if Editnbr1.text<>'' then
begin
oper1:=true;
TestEntrees;
end
else
begin
ButtonCalcul.enabled:=false;
oper1:=false; // drapeau de Editnbr1
baissé
end
end;
procedure TForm1.Editnbr2Change(Sender: TObject);
begin
if Editnbr2.text<>'' then
begin
oper2:=true;
TestEntrees;
end
else
begin
ButtonCalcul.enabled:=false;
oper2:=false; // drapeau de Editnbr2
baissé
end
end;
procedure TForm1.ListBoxOperationClick(Sender: TObject);
begin
operat:=true;
TestEntrees;
Labeltypoperat.caption:=Toper[ListBoxOperation.ItemIndex];
end;
procedure TForm1.ButtonCalculClick(Sender: TObject);
var op1,op2,result:smallint;
function signe(n:smallint):smallint;
begin
if n>0 then signe:=1
else if n<0 then signe:=-1
else signe:=0
end;
begin
ButtonCalcul.enabled:=false;
try
op1:=strtoint(Editnbr1.text);
except
on ERangeError do
if Editnbr1.text[1]='-' then
op1:=-Maxint
else op1:=Maxint;
on EConvertError do op1:=Maxint;
end;
try
op2:=strtoint(Editnbr2.text);
except
on EConvertError do op2:=Maxint;
on ERangeError do
if Editnbr2.text[1]='-' then
op2:=-Maxint
else op2:=Maxint;
end;
try
case ListBoxOperation.ItemIndex of
0: result:=op1 * op2;
1: result:=trunc(op1 / op2);
2: result:=op1 + op2;
3: result:=op1 - op2;
4: result:=op1 div op2;
5: result:=op1 mod op2;
end;
except
on EDivByZero do
if ListBoxOperation.ItemIndex=4 then
result:=signe(op1)*Maxint
else result:=op1;
on EZeroDivide do result:=signe(op1)*Maxint;
on EIntOverFlow do result:=signe(op1)*signe(op2)*Maxint;
end;
Editresult.text:=inttostr(result);
Editmessage.text:=inttostr(op1)+Toper[ListBoxOperation.ItemIndex]+'
'+inttostr(op2)+
'= '+Editresult.text;
end;
end.
Remonter