Помогите, пожалуйста, доработать код.Думаю ошибок там много(((
program lab_a; {Описать класс, реализующий тип данных "вещественная матрица" и работу с ними.Методы класса должны обеспечивать: сложение, вычитание, умножение матриц (умножение, как на другую матрицу, так и на число); вычисление транспонированной матриц; проверку типа матрицы (квадратная, диагональная, нулевая, единичная); Написать программу, демонстрирующую работу с этим классом.}
{$APPTYPE CONSOLE}
uses SysUtils;
type veshmatr=class X:array of array of Double; n,m:integer;
Procedure vvod (var B:veshmatr); Procedure vivod ( var B:veshmatr); Procedure slosh(const A,B:veshmatr; var C:veshmatr); Procedure vichet(const A,B:veshmatr; var C:veshmatr); Procedure umnosh_na_chislo(const A:veshmatr; var B:veshmatr); Procedure umnosh_na_matr(const A,B:veshmatr; var C:veshmatr); Procedure transpan(const A:veshmatr;var B:veshmatr); Function TypematriX:string;{определяет тип матрицы:квадратная, диоганальная,нулевая или единичная} end;
var W,R,T:veshmatr; i,j,k:integer;
procedure veshmatr.vvod( var B:veshmatr); begin for i:=1 to n do for j:=1 to m do begin write('vvedite_element[',i,',',j,']=',''); readln(B.X[i,j]) end end;
Procedure veshmatr.vivod ( var B:veshmatr); begin for i:=1 to n do begin for j:=1 to m do writeln(' ',B.X[i,j]:12:3); writeln end;
end;
Procedure veshmatr.slosh(const A,B:veshmatr; var C:veshmatr); begin for I := 1 to n do for j := 1 to m do begin C.X[i,j]:=A.X[i,j]+B.X[i,j] end; end;
Procedure veshmatr.vichet(const A,B:veshmatr; var C:veshmatr); begin for I := 1 to n do for j := 1 to m do begin C.X[i,j] :=A.X[i,j]-B.X[i,j] end; end;
Procedure veshmatr.umnosh_na_chislo(const A:veshmatr; var B:veshmatr); var chislo:double; begin writeln('vvedite_chislo'); readln(chislo); for i:=1 to n do for j:=1 to m do begin B.X[i,j]:= A.X[i,j] * chislo end end;
Procedure veshmatr.umnosh_na_matr(const A,B:veshmatr; var C:veshmatr); var i,j,k:word; begin for i:=1 to n do for j:=1 to m do begin C.X[i,j]:=0; for k:=1 to n do C.X[i,j]:=C.X[i,j]+A.X[i,k]*B.X[k,j]; end; end;
Procedure veshmatr.transpan( const A:veshmatr;var B:veshmatr); var i,j:integer; begin for i:=1 to n do for j:=i to m do B.X[i,j]:=A.X[j,i]; end;
Function veshmatr.TypematriX:string; begin if n=m then writeln('matrica_kvadratnaya');
for i:=1 to n do for j:=1 to m do begin if (i<>j) and (X[i,j]=0) then writeln('matrica_diagonalnaya'); if X[i,j]=0 then writeln('matrica_nulevaya'); end;
for i:=1 to n do for j:=1 to m do if (i=j) and (X[i,j]=1) then writeln('matrica_edinichnaya')
Ну вот. Сравни с твоей реализацией, и скажи, с чем удобнее работать? Можно и дальше улучшать, кстати, это еще не самый окончательный вариант. Если честно, я думал, ты сама уже все сделала - столько времени прошло.
program lab_a; {$APPTYPE CONSOLE} uses SysUtils;
type veshmatr = class private X: array of array of real; rows, cols: integer;
public constructor Create(ARows, ACols: integer); destructor Destroy;
procedure Vvod(); procedure Vivod();
function Add(const B: veshmatr): veshmatr; function Sub(const B: veshmatr): veshmatr; function Scale(f: double): veshmatr; function Mult(const B: veshmatr): veshmatr; function T: veshmatr; function TypematriX(): string; end;
procedure veshmatr.Vvod(); var iCols, iRows: integer; begin for iRows := 0 to Rows - 1 do for iCols := 0 to Cols - 1 do begin write('element [', iRows,',',iCols,'] = '); readln(X[iRows, iCols]) end; Vivod(); end;
procedure veshmatr.vivod(); var iCols, iRows: integer; begin if (Rows = 0) and (Cols = 0) then writeln('<empty>');
for iRows := 0 to Rows - 1 do begin for iCols := 0 to Cols - 1 do write(X[iRows, iCols]:12:3); writeln; end; end;
function veshmatr.Add(const B: veshmatr): veshmatr; var iCols, iRows: integer; begin if (Rows <> B.Rows) or (Cols <> B.Cols) then result := VeshMatr.Create(0, 0) else begin result := VeshMatr.Create(Rows, Cols); for iRows := 0 to Rows - 1 do for iCols := 0 to Cols - 1 do result.X[iRows, iCols] := X[iRows, iCols] + B.X[iRows, iCols]; end end;
function veshmatr.Sub(const B: veshmatr): veshmatr; var iCols, iRows: integer; begin if (Rows <> B.Rows) or (Cols <> B.Cols) then result := VeshMatr.Create(0, 0) else begin result := VeshMatr.Create(Rows, Cols); for iRows := 0 to Rows - 1 do for iCols := 0 to Cols - 1 do result.X[iRows, iCols] := X[iRows, iCols] - B.X[iRows, iCols]; end end;
function veshmatr.Scale(f: double): veshmatr; var iCols, iRows: integer; begin result := veshmatr.Create(Rows, Cols); for iRows := 0 to Rows - 1 do for iCols := 0 to Cols - 1 do result.X[iRows, iCols] := f * X[iRows, iCols];
end;
function veshmatr.Mult(const B: veshmatr): veshmatr; var i, j, k: integer; begin if (Cols <> B.Rows) then result := VeshMatr.Create(0, 0) else begin result := veshmatr.Create(Rows, B.Cols); for i := 0 to Rows - 1 do for j := 0 to B.Cols - 1 do begin result.X[i, j] := 0; for k := 0 to Cols - 1 do result.X[i, j] := result.X[i, j] + X[i, k] * B.X[k, j]; end end; end;
function veshmatr.T: VeshMatr; var iCols, iRows: integer; begin result := VeshMatr.Create(Cols, Rows); for iRows := 0 to Rows - 1 do for iCols := 0 to Cols - 1 do result.X[iCols, iRows] := X[iRows, iCols]; end;
function veshmatr.TypematriX(): string; var iCols, iRows: integer; flag_diag, flag_O, flag_E: boolean; begin result := '';
if (Rows = Cols) then result := result + 'square'#10#13;
for iCols := 0 to Cols - 1 do for iRows := 0 to Rows - 1 do begin flag_diag := flag_diag and ( ((iCols = iRows) and (X[iCols, iRows] <> 0)) or ((iCols <> iRows) and (X[iCols, iRows] = 0)) ); flag_O := (X[iCols, iRows] = 0); flag_E :=flag_E and ( ((iCols = iRows) and (X[iCols, iRows] = 1)) or ((iCols <> iRows) and (X[iCols, iRows] = 0)) ); end;
if flag_diag then result := result + 'diagonal'#10#13 else result := result + 'non-diagonal'#10#13; if flag_O then result := result + 'O-matrix'#10#13; if flag_E then result := result + 'E-matrix'#10#13; end;
function GetAnswer(s: string): boolean; var answer: char; begin write(s); readln(answer); result := answer in ['y', 'Y']; end;
if GetAnswer('Add M1 + M2? y/n >>>_') then begin Writeln('__________Add_M1+M2________'); with M1.Add(M2) do begin Vivod(); Destroy(); end; end;
if GetAnswer('Sub M1-M2? y/n >>>_') then begin Writeln('__________Sub_M1-M2________'); with M1.Sub(M2) do begin Vivod(); Destroy(); end; end;
if GetAnswer('Mult s*M1? y/n >>>_') then begin write('vvedite s: '); readln(s); Writeln('__________Mult_s*M1________'); with M1.Scale(s) do begin Vivod(); Destroy(); end; end;
if GetAnswer('Mult M1*M2? y/n >>>_') then begin Writeln('__________Mult_M1*M2_______'); with M1.Mult(M2) do begin Vivod(); Destroy(); end; end;
if GetAnswer('Traspon M1? y/n >>>_') then begin Writeln('______Transponirovanaya____'); with M1.T do begin Vivod(); Destroy(); end; end;
if GetAnswer('Found type of M1? y/n >>>_') then begin Writeln(M1.TypematriX()); end; if GetAnswer('Found type of M2? y/n >>>_') then begin Writeln(M2.TypematriX()); end;