Лабораторные работы по курсу ''Введение в объектно-ориентированное программирование в Delphi''

  • Published on
    08-Dec-2016

  • View
    219

  • Download
    3

Embed Size (px)

Transcript

  • .. , ..

    - Delphi

    2005

  • 2

    . .. ..

    - Delphi, 2- .

    , .

    : 634050, . , . , 36, ,

    : (382-2) 52-94-96 -:

    http://www.inf.tsu.ru

    : skv@csd.tsu.ru poddubnaya@inf.tsu.ru

    . ., . . 2005

  • 3

    1 -

    : c TVector ( ), .

    Delphi class, . Delphi unit1 . :

    type TForm1 = class(TForm) private { Private declarations } public { Public declarations } end; Delphi

    T, ( ) F. class , .

    public () , , private , , .

    TVector,

    . .

    type TVector = class private FValues: array of double; // 4 accessing , , // , private function GetLength: integer; procedure SetLength(const Value: integer); function GetValues(Index: integer):double; procedure SetValues(Index: integer; const Value: double); public //Receive the vector as string function AsString: string;

  • 4

    //Length of the vector property Length: integer read GetLength write SetLength; //Receive the product of the vector to scalar procedure Product( Factor: double); overload; //Receive the product of two vectors procedure Product( const SecondVector: TVector); overload; end; ,

    .

    : . FValues , .. .

    private , Get Set. .

    public , ( property) Length ( ) , . (, size), Length, , , (. ).

    , public Product, overload (). , Delphi 4, C++. . . . Product. , .

    interface

    Delphi, ( ) implementation. :

  • 5

    unit Vectors; interface uses SysUtils, Classes; Type //Vector TVector = class private FValues: array of double; function GetLength: integer; procedure SetLength(const Value: integer); function GetValues(Index: integer):double; procedure SetValues(Index: integer; const Value: double); procedure CheckIndex(Index: integer); public constructor Create; //Get vector values as string function AsString: string; //Item values of the vector property Values[Index: integer]: double read GetValues Write SetValues; default; //Length of the vector property Length: integer read GetLength write SetLength; //Receive the product of the vector to scalar procedure Product( Factor: double); overload; //Receive the product of two vectors procedure Product( const SecondVector: TVector); overload; end;

  • 6

    implementation {TVector} constructor TVector.Create; begin System.SetLength(Fvalues, 1); end; function TVector.AsString: string; var i: integer; begin Result := '('; for i:=0 to Length-1 do begin if i>0 then Result:= Result+' , '; Result:=Result+Format('%.3n',[Values[i]]); end; Result:=Result+')'; end; function TVector.GetLength: integer; begin Result:=System.Length(FValues); end; function TVector.GetValues(Index: integer): double; begin CheckIndex(Index); Result:=FValues[Index]; end; procedure TVector.SetLength(const Value: integer); begin if Value

  • 7

    procedure TVector.CheckIndex(Index: integer); begin if (Index>=0) and (Index>=Length) then raise Exception.Create(Index out of the bounds); end. Procedure TVector.Product(const SecondVector: TVector); var i: integer; Begin For i:=0 to SecondVector.Length-1 do Fvalues[I]:= Self[i] * SecondVector[i]; end; end.

    : 1. construtor. 2. :

    - (TVector.Add(const SecondVector: TVector)), - (TVector.Product(Factor: double)), - .

    3. .

    .3 unit UnitMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ButtonInput: TButton; Memo1: TMemo; ButtonExit: TButton; procedure ButtonInputClick(Sender: TObject); procedure ButtonExitClick(Sender: TObject); private { Private declarations } public { Public declarations } end;

  • 8

    var Form1: TForm1; implementation uses Vectors; {$R *.dfm} procedure TForm1.ButtonInputClick(Sender: TObject); var V, SecondV: TVector; i, N: integer; R: double; begin // V:=TVector.Create; SecondV:=TVector.Create; try V.Length:=4; SecondV.Length:= 4; for i:=0 to V.Length-1 do begin Randomize; V[i]:=Random(100); end; for i:=0 to SecondV.Length-1 do SecondV[i]:= 10; Memo1.Lines.add(V.AsString); V.Product(10); Memo1.Lines.add(V.AsString); V.Add(SecondV); Memo1.Lines.Add(V.AsString); R:=V.ScalarProduct(SecondV); Memo1.Lines.Add(FloatToStr(R)); finally V.Free; SecondV.Free; end; end; procedure TForm1.ButtonExitClick(Sender: TObject); begin Close; end; end.

  • 9

    2 -

    : c TMatrix, , ( ) ( ), .

    - Delphi. : (properties), (virtual) (abstract) .

    , Delphi - (incapsulation), (polymorphism), (overriding) .

    . ,

    TVector. , TMatrix TVector, TMatrix TVector. , , ( ).

    . 1 , . , TVector, , .

    , TCustomVector, () , . (property Length) (property Values). (, ..). , . ( ) , .. . , , () . , TCustomVector , , , , .

  • 10

    // ( ) TCustomVector = class

    CheckIndex(Index: integer) - private function GetLength: integer; virtual; abstract - protected procedure SetLength( ); virtual; abstract - protected function GetValues( ): double; virtual; abstract - protected procedure SetValues( ); virtual; abstract - protected procedure Assign(Source: TCustomVector); virtual; - public property Length: integer read GetLength write SetLength; - public property Values[Index: integer]: double read GetValues write SetValues; default; - public

    TVector = class (TCustomVector) Fdata: array of double; - private function GetLength.; override; - protected procedure SetLength (.); override; - protected function GetValues (.): double; override; - protected procedure SetValues (.); override; - protected constructor Create; - public

    TMatrixVector = class (TCustomVector) FMatrix: TMatrix; - private procedure SetLength(); override; - protected procedure Assign(Source: TCustomVector); override; - public

    TMatrixColumn = class(TMatrixVector) FColumn: integer; - private

    function GetLength.; override; - protected function GetValues (.): double; override; - protected

    procedure SetValues (.); override; - protected constructor Create

    (Matrix: TMatrix; Column: integer); -public TMatrixRow = class(TMatrixVector) FRow: integer; - private

    function GetLength.; override; - protected function GetValues (.): double; override; - protected

    procedure SetValues (.); override; - protected constructor Create

    (Matrix: TMatrix; Row: integer); -public

    . 1

    , .

    , TCustomVector TVector ( , ) . TVector (private) FData TVector ,

  • 11

    FData.

    TCustomVector TMatrixColumn TMatrixRow, , , TCustomVector. , . . (SetLength Assign) FMatrix TMatrixVector.

    TMatrix.

    // TMatrix = class private FColumnCount: integer; FRowCount: integer; FValues: array of array of double; FColumns: array of TCustomVector; FRows: array of TCustomVector; function GetValues(Column, Row: integer): double; procedure SetValues(Column, Row: integer; Value: double); procedure SetColumnCount(const Value: integer); procedure SetRowCount(const Value: integer); procedure CheckIndex(Column,Row: integer); procedure SetSizes(NewColumnCount, NewRowCount: integer); function GetColumns(Column: integer): TCustomVector; function GetRows(Row: integer): TCustomVector; procedure SetColumns(Column: integer; const Value: TCustomVector); procedure SetRows(Row: integer; const Value: TCustomVector); public constructor Create; // procedure Assign(Source: TMatrix); // function AsString: string; // property ColumnCount: integer read FColumnCount write SetColumnCount; // property RowCount: integer read FRowCount write SetRowCount; // ( 0 ColumnCount-1, RowCount-1); property Values[Column, Row: integer]: double read GetValues write SetValues; default; // property Columns[Column: integer]: TCustomVector read GetColumns write SetColumns; // property Rows[Row: integer]: TCustomVector read GetRows write SetRows; end;

  • 12

    TMatrix , . ( ), (Values), , . private , public , . .

    Delphi

    (properties). . , . , . Code Completion, Delphi, , , , .. Delphi . , , Ctrl+Shift+C, , . ( , ), - .

    TCustomVector

    . ; abstract, -.

    () - - virtual , override.

    , ,

    , TVector, TMatrixColumn TMatrixRow TCustomVector, , . , TMatrix, TMatrix TVector.

    . .

  • 13

    , , .

    unit Matrices;

    interface Uses SysUtils, Classes; type // ( ) TCustomVector = class private procedure CheckIndex(Index: integer); protected function GetLength: integer; virtual; abstract; procedure SetLength(NewLength: integer); virtual; abstract; function GetValues(Index: integer): double; virtual; abstract; procedure SetValues(Index: integer; NewValue: double); virtual; abstract; public // procedure Assign(Source: TCustomVector); virtual; // property Length: integer read GetLength write SetLength; // ( 0 Length-1) property Values[Index: integer]: double read GetValues write SetValues; default; end; // TVector = class(TCustomVector) private FData: array of Double; protected function GetLength: integer; override; procedure SetLength(NewLength: integer); override; function GetValues(Index: integer): double; override; procedure SetValues(Index: integer; NewValue: double); override; public constructor Create; end;

  • 14

    // TMatrix = class private FColumnCount: integer; FRowCount: integer; FValues: array of array of double; FColumns: array of TCustomVector; FRows: array of TCustomVector; function GetValues(Column, Row: integer): double; procedure SetValues(Column, Row: integer; Value: double); procedure SetColumnCount(const Value: integer); procedure SetRowCount(const Value: integer); procedure CheckIndex(Column,Row: integer); procedure SetSizes(NewColumnCount, NewRowCount: integer); function GetColumns(Column: integer): TCustomVector; function GetRows(Row: integer): TCustomVector; procedure SetColumns(Column: integer; const Value: TCustomVector); procedure SetRows(Row: integer; const Value: TCustomVector); public constructor Create; // procedure Assign(Source: TMatrix); // function AsString: string; // property ColumnCount: integer read FColumnCount write SetColumnCount; // property RowCount: integer read FRowCount write SetRowCount; // ( 0 ColumnCount-1, RowCount-1); property Values[Column, Row: integer]: double read GetValues write SetValues; default; // property Columns[Column: integer]: TCustomVector read GetColumns write SetColumns; // property Rows[Row: integer]: TCustomVector read GetRows write SetRows; end; implementation { TCustomVector } procedure TCustomVector.Assign(Source: TCustomVector); var i: integer;

  • 15

    begin if Sourcenil then begin Length:=Source.Length; for i:=0 to Length-1 do Values[i]:=Source[i]; end; end;

    procedure TCustomVector.CheckIndex(Index: integer); begin if(Index=Length) then raise Exception.Create(' '); end; { TVector } constructor TVector.Create; begin inherited Create; System.SetLength(FData, 1); end; function TVector.GetLength: integer; begin // end; function TVector.GetValues(Index: integer): double; begin // end; procedure TVector.SetLength(NewLength: integer); begin // end; procedure TVector.SetValues(Index: integer; NewValue: double); begin // end;

  • 16

    // TMatrix . type // TMatrixVector = class(TCustomVector) private FMatrix: TMatrix; protected procedure SetLength(NewLength: integer); override; public // procedure Assign(Source: TCustomVector); override; end; // TMatrixColumn = class (TMatrixVector) private FColumn: integer; protected function GetLength: integer; override; function GetValues(Index: integer): double; override; procedure SetValues(Index: integer; NewValue: double); override; public constructor Create(Matrix: TMatrix; Column: integer); end; // TMatrixRow = class (TMatrixVector) private FRow: integer; protected function GetLength: integer; override; function GetValues(Index: integer): double; override; procedure SetValues(Index: integer; NewValue: double); override; public constructor Create(Matrix: TMatrix; Row: integer); end; { TMatrixVector } // procedure TMatrixVector.Assign(Source: TCustomVector); var i: integer; begin

  • 17

    if Source.LengthLength then raise Exception.Create(' . '); for i:=0 to Length-1 do Values[i]:=Source[i]; end; procedure TMatrixVector.SetLength(NewLength: integer); begin if NewLengthLength then raise Exception.Create(' , , .'); end; { TMatixColumn } constructor TMatrixColumn.Create(Matrix: TMatrix; Column: integer); begin inherited Create; FMatrix:=Matrix; FColumn:=Column; end; function TMatrixColumn.GetLength: integer; begin Result:=FMatrix.RowCount; end; function TMatrixColumn.GetValues(Index: integer): double; begin Result:=FMatrix[FColumn, Index]; end; procedure TMatrixColumn.SetValues(Index: integer; NewValue: double); begin FMatrix[FColumn, Index]:=NewValue; end; { TMatrixRow } constructor TMatrixRow.Create(Matrix: TMatrix; Row: integer); begin // en...

Recommended

View more >