Разработка классов на языке Object Pascal. Соколов Е.В. - 77 стр.

UptoLike

Составители: 

77
procedure Rational.DivideBy(n: Number);
var _num,_den: Integer;
begin
if n is Rational then begin
if (n as Rational).num=0
then raise Exception.Create('Division by zero');
_num:=self.num*(n as Rational).den;
_den:=self.den*(n as Rational).num;
self.num:=_num; self.den:=_den;
self.Normalize; self.Reduce;
end
else raise Exception.Create('Invalid operation');
end;
procedure Rational.Assign(n: Number);
begin
if (n is Rational) then begin
self.num:=(n as Rational).num;
self.den:=(n as Rational).den;
end
else raise Exception.Create('Invalid operation');
end;
function Rational.IsEqual(n: Number): Boolean;
begin
if (n is Rational) and
(self.num*(n as Rational).den=self.den*(n as Rational).num)
then result:=true
else result:=false;
end;
function Rational.AsString: String;
begin result:=IntToStr(self.num)+'/'+IntToStr(self.den); end;
constructor Rational.Create;
begin self.num:=0; self.den:=1; end;
function Rational.GetNum: Integer;
begin result:=self.num; end;
function Rational.GetDen: Integer;
begin result:=self.den; end;
procedure Rational.SetNum(value:Integer);
begin self.num:=value; end;
procedure Rational.SetDen(value:Integer);
begin
if value>0 then self.den:=value
else raise Exception.Create('Illegal value of denominator');
end;
end.
procedure Rational.DivideBy(n: Number);
var _num,_den: Integer;
begin
  if n is Rational then begin
     if (n as Rational).num=0
       then raise Exception.Create('Division by zero');
     _num:=self.num*(n as Rational).den;
     _den:=self.den*(n as Rational).num;
     self.num:=_num; self.den:=_den;
     self.Normalize; self.Reduce;
  end
  else raise Exception.Create('Invalid operation');
end;
procedure Rational.Assign(n: Number);
begin
  if (n is Rational) then begin
     self.num:=(n as Rational).num;
     self.den:=(n as Rational).den;
  end
  else raise Exception.Create('Invalid operation');
end;
function Rational.IsEqual(n: Number): Boolean;
begin
  if (n is Rational) and
      (self.num*(n as Rational).den=self.den*(n as Rational).num)
     then result:=true
     else result:=false;
end;
function Rational.AsString: String;
begin result:=IntToStr(self.num)+'/'+IntToStr(self.den); end;
constructor Rational.Create;
begin self.num:=0; self.den:=1; end;
function Rational.GetNum: Integer;
begin result:=self.num; end;
function Rational.GetDen: Integer;
begin result:=self.den; end;
procedure Rational.SetNum(value:Integer);
begin self.num:=value; end;
procedure Rational.SetDen(value:Integer);
begin
  if value>0 then self.den:=value
  else raise Exception.Create('Illegal value of denominator');
end;
end.




                                 77