Extras din laborator
But du travail : Créer une classe matrice. La classe doit contenir un pointeur au real. Créer le constructeur sans paramètre, avec un paramètre et avec deux paramètres et aussi le destructeur. Créer les méthodes : setAt(i,j,value) et getAt(i,j). Créer les fonctions: Print, ajouter, soustraire et aussi multiplier.
Réalisation du travail:
program matrita;
uses crt;
var i,j:integer;
type
matrix = object
private
row : integer;
col : integer;
pointarray : array[0..10,0..10] of ^real;
public
constructor init;
constructor initone(r:integer);
constructor inittwo(r,c:integer);
destructor destroy;
procedure setatribut(i,j:integer; val:real);
function getatribut(i,j:integer):real;
procedure print;
procedure add(a,b:matrix);
procedure sub(a,b:matrix);
procedure multiply(a,b:matrix);
end;
constructor matrix.init;
begin
row:=0;
col:=0;
pointarray[0,0]:=nil;
end;
constructor matrix.initone(r:integer);
begin
row:=r;
col:=r;
for i:=1 to row do
begin
for j:=1 to col do
begin
new (pointarray[i,j]);
pointarray[i,j]^:=1;
end;
end;
end;
constructor matrix.inittwo(r,c:integer);
begin
row:=r;
col:=c;
for i:=1 to row do
begin
for j:=1 to col do
begin
new (pointarray[i,j]);
pointarray[i,j]^:=1;
end;
end;
end;
destructor matrix.destroy;
begin
for i:=1 to row do
for j:=1 to col do
dispose(pointarray[i,j])
end;
procedure matrix.setatribut(i,j:integer; val:real);
begin
pointarray[i,j]^:=val;
end;
function matrix.getatribut(i,j:integer):real;
begin
getatribut:=pointarray[i,j]^;
end;
procedure matrix.print;
begin
for i:=1 to row do
begin
for j:=1 to col do
write(' ',pointarray[i,j]^:3:1);
writeln(' ');
end;
end;
procedure matrix.add(a,b:matrix);
begin
for i:=1 to row do
begin
for j:=1 to col do
setatribut(i,j,a.getatribut(i,j)+b.getatribut(i,j));
end;
end;
procedure matrix.sub(a,b:matrix);
begin
for i:=1 to row do
begin
for j:=1 to col do
setatribut(i,j,a.getatribut(i,j)-b.getatribut(i,j));
end;
end;
procedure matrix.multiply(a,b:matrix);
var k:integer;
res:real;
begin
res:=0;
if(a.row<>b.col)then
writeln('matricile nu pot fi inmultite !!!')
else if (a.row=b.col) then
begin
for i:=1 to a.row do
for j:=1 to b.col do
begin
for k:=1 to a.row do
begin
res:=res+a.getatribut(i,k)*b.getatribut(k,j);
setatribut(i,j,res);
end;
end;
end;
end;
Preview document
Conținut arhivă zip
- Classes, Objets, Constructeurs et Destructeurs.doc