1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| unit Unit2;
interface
const
OP_ADD = 1;
OP_SUS = 2;
OP_MUL = 3;
OP_DIV = 4;
type
TOperationStruct=record
OperationKind :Word;
Operand1 :Integer;
Operand2 :Integer;
Result :Integer;
end;
TMathOperation=class
private
procedure xAdd(var Data:TOperationStruct);message OP_ADD;
procedure xSus(var Data:TOperationStruct);message OP_SUS;
procedure xMul(var Data:TOperationStruct);message OP_MUL;
procedure xDiv(var Data:TOperationStruct);message OP_DIV;
public
procedure DefaultHandler(var message);override;
function Calc(Opd1,Opd2:integer;Okind:Word):integer;
end;
implementation
uses SysUtils;
{ TMathOperation }
function TMathOperation.Calc(Opd1, Opd2: integer;
Okind: Word): integer;
var
V:TOperationStruct;
begin
V.OperationKind :=Okind;
V.Operand1 :=Opd1;
V.Operand2 :=Opd2;
Dispatch(V);
Result :=V.Result;
end;
procedure TMathOperation.DefaultHandler(var message);
begin
raise Exception.Create('Opération non supportée !');
end;
procedure TMathOperation.xAdd(var Data: TOperationStruct);
begin
with Data do
Result :=Operand1+Operand2;
end;
procedure TMathOperation.xDiv(var Data: TOperationStruct);
begin
with Data do
Result :=Operand1 div Operand2;
end;
procedure TMathOperation.xMul(var Data: TOperationStruct);
begin
with Data do
Result :=Operand1 * Operand2;
end;
procedure TMathOperation.xSus(var Data: TOperationStruct);
begin
with Data do
Result :=Operand1 - Operand2;
end;
end. |
Partager