Пользователь
Регистрация: 20.08.2017
Сообщений: 12
|
Нахождение минимального значения в каждой строке и сортировка.
Была ошибка в запись в матрицу "C", после исправления не выходит верно написать часть о сортировки и поиска минимального значения.Точнее сортировка, сама по себе рабочая, вот только попросили переписать часть самих расчетов, после изменения выводит ошибку.
Выделил красным нужную часть, в остальном все работает нормально
Цитата:
unit Unit1;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, Grids;
type
{ TForm1 }
TForm1 = class(TForm)
StringGrid1: TStringGrid;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
Button1: TButton;
StringGrid2: TStringGrid;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
StringGrid3: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
procedure SpinEdit2Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
VAR
i,j:integer;
begin
Randomize();
SpinEdit1.Value:=Random(3)+3;
SpinEdit2.Value:=Random(3)+2;
for i:=0 to StringGrid1.RowCount-1 do
begin
for j:=0 to StringGrid1.ColCount-1 do
begin
StringGrid1.Cells[j,i]:=IntToStr(RandomRange(-45,36));
end;
end;
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
StringGrid1.RowCount:=SpinEdit1.Val ue;
StringGrid2.RowCount:=SpinEdit1.Val ue;
StringGrid3.RowCount:=SpinEdit1.Val ue;
end;
procedure TForm1.SpinEdit2Change(Sender: TObject);
begin
StringGrid1.ColCount:=SpinEdit2.Val ue;
StringGrid3.ColCount:=SpinEdit2.Val ue;
end;
procedure TForm1.Button2Click(Sender: TObject);
VAR
A: array [1..100,1..100] of integer;
B: array [1..100] of integer;
C: array [1..100,1..100] of Integer;
i,j,g,min:integer;
begin
StringGrid2.Cols[0].Clear;
begin
for i:=1 to StringGrid1.RowCount do
for j:=1 to StringGrid1.ColCount do
A[i,j]:= StrToInt(StringGrid1.Cells[j-1,i-1]);
begin
for i:=1 to StringGrid1.RowCount do
for j:=1 to StringGrid1.ColCount do
if A[i,j]>0 then
begin
C[i,j]:=A[i,j];
end
else
if A[i,j]<=0 then
C[i,j]:=1;
end;
for i:=1 to StringGrid3.RowCount do
for j:=1 to StringGrid3.ColCount do
StringGrid3.Cells[j-1,i-1]:=IntToStr(C[i,j]);
end;
begin
for i:=1 to StringGrid1.RowCount do
for j:=1 to StringGrid1.ColCount do
C[i,j]:= StrToInt(StringGrid3.Cells[j-1,i-1]);
for i:=1 to StringGrid3.RowCount do
min:= C[i,1];
for j:=1 to StringGrid3.ColCount do
if C[i,j+1]<min then
min:=C[i,j+1];
end;
begin
g:=j;
if g=1 then
b[g]:=min
else
begin
while (g<>1) do
begin
if min>b[g-1] then
begin
b[g]:=b[g-1];
b[g-1]:=min;
g:=g-1;
end
else
begin
b[g]:=min;
g:=1;
end;
end;
end;
end;
for i:=1 to StringGrid2.RowCount do
StringGrid2.Cells[0,i-1]:=IntToStr(b[j]);
end;
procedure TForm1.Button3Click(Sender: TObject);
VAR
f :file;
i,j,x: Integer;
begin
AssignFile(f,'source.dat');
Rewrite(f,1);
x:=SpinEdit1.Value;
BlockWrite(f,x,SizeOf(x));
x:=SpinEdit2.Value;
BlockWrite(f,x,SizeOf(x));
for i:=0 to StringGrid1.ColCount-1 do
for j:=0 to StringGrid2.RowCount-1 do
begin
x:=StrToInt(StringGrid1.Cells[i,j]);
BlockWrite(f,x,SizeOf(x));
end;
CloseFile(f);
end;
procedure TForm1.Button4Click(Sender: TObject);
VAR
f :file;
i,j,x: Integer;
begin
AssignFile(f,'source.dat');
Reset(f,1);
BlockRead(f,x,SizeOf(x));
SpinEdit1.Value:=x;
BlockRead(f,x,SizeOf(x));
SpinEdit2.Value:=x;
for i:=0 to SpinEdit2.Value-1 do
for j:=0 to SpinEdit1.Value-1 do
begin
BlockRead(f,x,SizeOf(x));
StringGrid1.Cells[i,j]:=IntToStr(x);
end;
CloseFile(f);
end;
procedure TForm1.Button5Click(Sender: TObject);
VAR
f:TextFile;
i:integer;
begin
AssignFile(f,'Result.txt');
Rewrite(f);
for i:=0 to StringGrid2.RowCount-1 do
Writeln(f,StringGrid2.Cells[0,i]);
CloseFile(f);
end;
end.
|
|