Перегрузка операций

Перегрузка операций + и - для комплексных чисел.

type
  TComplex = class
    private
      x,y: real; //действительная и мнимая части
    public
      constructor Create;
      function Modul(): real;
      function Argument(): real;
      procedure Printer();
  end;
constructor TComplex.Create;
  begin
    x := 0; y := 0;
    inherited Create;
  end;
function TComplex.Modul(): real;
  begin
    modul := sqrt(x*x + y*y);
  end;
function TComplex.Argument(): real;
  begin
    argument := arctan(y/x) * 180/pi;
  end;
procedure TComplex.Printer();
  begin
    if y >= 0 then
      writeln(x:3:1,'+',y:3:1,'i')
    else
      writeln(x:3:1,y:3:1,'i');
  end;
 
operator +(const a,b: TComplex)r: TComplex;
  begin
    r := TComplex.Create;
    r.x := a.x + b.x;
    r.y := a.y + b.y;
  end;
operator -(const a,b: TComplex)r: TComplex;
  begin
    r := TComplex.Create;
    r.x := a.x - b.x;
    r.y := a.y - b.y;
  end;
 
var
  c1, c2, c3, c4: TComplex;
  a, b: real;
begin
  c1 := TComplex.Create;
  c2 := TComplex.Create;
  c3 := TComplex.Create;
  c4 := TComplex.Create;
  write('real and imag of the FIRST number: ');
  readln(c1.x, c1.y);
  write('real and imag of the SECOND number: ');
  readln(c2.x, c2.y);
  c3 := c1 + c2;
  c4 := c1 - c2;
  c3.printer();
  c4.printer();
readln;
end.

В примере реализуется возможность сложения и перемножения двух матриц, добавление к элементам матрицы любого числа или умножение на любое число.

type
  TMatrix = class
    private
        x: array[0..5,0..5] of real;
    public
        constructor Create;
        procedure printer();
        function uniq(): boolean; // является ли матрица единичной
  end;
 
  constructor TMatrix.Create;
    var i,j: integer;
    begin
      for i:=0 to 4 do
        for j:=0 to 4 do
          x[i,j] := Random(10);
      inherited Create;
    end;
 
  function TMatrix.uniq(): boolean;
    var i,j: integer;
    begin
      result := true;
      for i:=0 to 4 do
        for j:=0 to 4 do
          if ((i=j) and (x[i,j]<>1)) or ((i<>j) and (x[i,j]<>0)) then begin
            result := false;
            break;
          end;
    end;
 
  operator +(const a,b: TMatrix)r: TMatrix;
    var i,j: integer;
    begin
      r := TMatrix.Create;
      for i:=0 to 4 do
        for j:=0 to 4 do
          r.x[i,j] := a.x[i,j] + b.x[i,j];
    end;
  operator +(const a: TMatrix; b: real)r: TMatrix;
    var i,j: integer;
    begin
      r := TMatrix.Create;
      for i:=0 to 4 do
        for j:=0 to 4 do
          r.x[i,j] := a.x[i,j] + b;
    end;
  operator *(const a,b: TMatrix)r: TMatrix;
    var i,j: integer;
    begin
      r := TMatrix.Create;
      for i:=0 to 4 do
        for j:=0 to 4 do
          r.x[i,j] := a.x[i,j] * b.x[i,j];
    end;
  operator *(const a: TMatrix; b: real)r: TMatrix;
    var i,j: integer;
    begin
      r := TMatrix.Create;
      for i:=0 to 4 do
        for j:=0 to 4 do
          r.x[i,j] := a.x[i,j] * b;
    end;
 
  procedure TMatrix.printer();
    var i,j: integer;
    begin
      for i:=0 to 4 do begin
        for j:=0 to 4 do
          write(x[i,j]:5:0);
        writeln;
      end;
    end;
 
var
    m1,m2,m3,m4,m5,m6: TMatrix;
begin
  m1 := TMatrix.Create;
  m2 := TMatrix.Create;
  m3 := TMatrix.Create;
  m4 := TMatrix.Create;
  m5 := TMatrix.Create;
  m6 := TMatrix.Create;
  m1.printer; writeln('--------------------------');
  m2.printer; writeln('--------------------------');
  m3 := m1 + m2;
  m3.printer; writeln('--------------------------');
  m4 := m1 * m2;
  m4.printer; writeln('--------------------------');
  m5 := m1 + 10;
  m5.printer; writeln('--------------------------');
  m6 := m2 * 5;
  m6.printer; writeln('--------------------------');
  writeln(m1.uniq());
readln;
end.

Пример перегрузки операций < и >.

type
  TFraction = class
    private
      num,den: integer; // числитель и знаменатель
    public
      constructor Create(a,b: integer);
      procedure printer();
  end;
constructor TFraction.Create(a,b: integer);
  begin
    num := a; den := b;
    inherited Create;
  end;
operator <(const a,b: TFraction)r: boolean;
  begin
    if a.num * b.den < b.num * a.den then
        r := true
    else r := false;
  end;
operator >(const a,b: TFraction)r: boolean;
  begin
    if a.num * b.den > b.num * a.den then
        r := true
    else r := false;
  end;
procedure TFraction.printer();
  begin
    if den <> 0 then
        if num <> 0 then
            write(num,'/',den)
        else
            writeln(0)
    else
        writeln('Dividing by a zero');
  end;
var
  f1, f2: TFraction;
  a, b: integer;
begin
  write('Numerator and denominator of the FIRST fraction: ');
  readln(a,b);
  f1 := TFraction.Create(a,b);
  write('Numerator and denominator of the SECOND fraction: ');
  readln(a,b);
  f2 := TFraction.Create(a,b);
 
  f1.printer();
  write(' < ');
  f2.printer();
  writeln(' is ', f1<f2);
 
  f1.printer();
  write(' > ');
  f2.printer();
  writeln(' is ', f1>f2);
readln;
end.

Теги

Добавить комментарий