IPB
ЛогинПароль:

> вещественная матрица, ООП в ТР
Svetlana
сообщение 16.09.2009 18:04
Сообщение #1


Новичок
*

Группа: Пользователи
Сообщений: 24
Пол: Женский

Репутация: -  0  +


Помогите, пожалуйста, доработать код.Думаю ошибок там много(((

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')

end;

{начало программы}
begin
W:= veshmatr.Create; {создаём первую матрицу}
W.vvod(W); {ввод первой матрицы}
R:= veshmatr.Create; {создаём 2-ую матрицу}
R.vvod®; {ввод 2-ой матрицы}
T.slosh(W,R,T);
T.vivod(T);
T.vichet(W,R,T);
T.vivod (T);
R.umnosh_na_chislo(W,R);
R.vivod ®;
T.umnosh_na_matr(W,R,T);
T.vivod (T);
R.transpan(R,T);
R.vivod ®;


W.Free; {удаляем объект}
R.Free;
T.Free;

readln
end.




Сообщение отредактировано: Svetlana - 16.09.2009 18:07
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 
 
 Ответить  Открыть новую тему 
Ответов
volvo
сообщение 3.11.2009 10:53
Сообщение #2


Гость






Ну вот. Сравни с твоей реализацией, и скажи, с чем удобнее работать? Можно и дальше улучшать, кстати, это еще не самый окончательный вариант. Если честно, я думал, ты сама уже все сделала - столько времени прошло.
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;



constructor veshmatr.Create(ARows, ACols: integer);
begin
inherited Create;
Rows := ARows; Cols := ACols;
setlength(X, Rows, Cols);
end;
destructor veshmatr.Destroy;
begin
SetLength(X, 0, 0);
inherited;
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;

flag_diag := true;
flag_O := true; flag_E := true;

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;

var
M1, M2: veshmatr;
s: Double;
Cm1, Cm2, Rm1, Rm2: integer;

begin
write('M1: rows = '); readln(Rm1);
write('M1: cols = '); readln(Cm1);
M1 := veshmatr.Create(Rm1, Cm1);
M1.Vvod();

write('M2: rows = '); readln(Rm2);
write('M2: cols = '); readln(Cm2);
M2 := veshmatr.Create(Rm2, Cm2);
M2.Vvod();

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;

M2.Destroy;
M1.Destroy;

readln
end.

 К началу страницы 
+ Ответить 

Сообщений в этой теме


 Ответить  Открыть новую тему 
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 



- Текстовая версия 13.07.2025 16:00
Хостинг предоставлен компанией "Веб Сервис Центр" при поддержке компании "ДокЛаб"