![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
#1
|
|||
|
|||
|
Помогите решить проблему, задача такая, нужно вводить количество строк, и столбцов с клавиатуры, и заполнить таблицу стринггрид тоже с клавиатуры и найти максимальный элемент...но проблема в том, что программа выдаёт ошибку raised exception class eaccessviolation и выделяет edt1.Text:=IntToStr(A[imax,jmax]); Помогите решить проблему.
Код:
unit Unit_masiv;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
edt1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
A: array of array of integer;
m, n: integer;
imax, jmax: integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
i, j: integer;
begin
imax:=0;
jmax:=0;
For i:=0 to High(A) do
For j:=0 to High(A) do
if A[i,j] > A[imax,jmax] then begin
imax:=i;
jmax:=j;
end;
edt1.Text:=IntToStr(A[imax,jmax]);
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
i, j: integer;
begin
if (Edit1.Text<>'') and (Edit2.Text<>'')
then
begin
n:=StrToInt(Edit1.Text);
m:=StrToInt(Edit2.Text);
for j:=0 to m do
for i:=0 to n do
if
Length(StringGrid1.Cells[i, j]) <>0
then
A[i,j]:=StrToInt(StringGrid1.Cells[i,j]);
StringGrid1.RowCount:=n;
StringGrid1.ColCount:=m;
end
else Showmessage('Ievadit masiva dimensiju.');
end;
end. |
|
#2
|
||||
|
||||
|
А чего же ты хочешь? При пустом поле так и будет. Закрывай в:
Код:
try except end Последний раз редактировалось angvelem, 15.12.2011 в 02:30. |
|
#3
|
|||
|
|||
|
я плохо разбираюсь в этом...то есть мне нужно сделать так..
Код:
try edt1.Text:=IntToStr(A[imax,jmax]); except End; Последний раз редактировалось lmikle, 15.12.2011 в 02:38. |
|
#4
|
||||
|
||||
|
Код:
A: array of array of integer; Последний раз редактировалось angvelem, 15.12.2011 в 02:28. |
|
#5
|
|||
|
|||
|
Вот писал получение матрицы из StringGrid:
Код:
function TMainForm5.GetMatrix : Boolean;
var
I, J : Integer;
begin
Result := True;
SetLength(A,sgMatrix.ColCount);
For I := Low(A) To High(A) Do
Begin
SetLength(A[i],sgMatrix.RowCount);
For J := Low(A[i]) To High(A[i]) Do
If Not TryStrToInt(sgMatrix.Cells[I,J],A[I,J]) Then
Begin
ShowMessage(Format('Error: Can''t convert value in cell %d,%d to integer.',[I+1,J+1]));
sgMatrix.SetFocus;
Result := False;
Exit;
End;
End;
end;Переделаешь под себя, заместо сообщения и вылета можно, например, 0 подставлять. |
|
#6
|
|||
|
|||
|
Спасибо помогло!.
|
|
#7
|
|||
|
|||
|
не подскажите что нужно изменить в данном коде, чтобы числа вводились не случайно а с клавиатуры?
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
StringGrid: TStringGrid;
Button2: TButton;
Edit3: TEdit;
Edit4: TEdit;
Label3: TLabel;
Label4: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
A: array of array of integer;
m, n: integer;
imax, jmax, imin, jmin: integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
i, j: integer;
begin
if (Edit1.Text<>'') and (Edit2.Text<>'') then begin
Randomize;
n:=StrToInt(Edit1.Text);
m:=StrToInt(Edit2.Text);
SetLength(A,n,m);
for j:=0 to m-1 do
for i:=0 to n-1 do
begin
A[i,j]:=Round(Sin(Random(100))*100);
end;
StringGrid.RowCount:=n+1;
StringGrid.ColCount:=m+1;
with StringGrid do
begin
for i:=1 to RowCount do
begin
Cells[i,0]:=IntToStr(i);
end;
for j:=1 to ColCount do
begin
Cells[0,j]:=IntToStr(j);
end;
end;
with StringGrid do
begin
for i:=1 to n do
for j:=1 to m do
Cells[j,i]:=IntToStr(A[i-1,j-1]);
end;
end
else Showmessage('Введите размерность массива.');
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
i, j: integer;
begin
imax:=0;
jmax:=0;
imin:=0;
jmin:=0;
For i:=0 to High(A) do
For j:=0 to High(A) do
if A[i,j] > A[imax,jmax] then begin
imax:=i;
jmax:=j;
end;
For i:=0 to High(A) do
For j:=0 to High(A) do
if A[i,j] < A[imin,jmin] then begin
imin:=i;
jmin:=j;
end;
Edit3.Text:=IntToStr(A[imax,jmax])+' индексы ['+IntToStr(imax+1)+','+IntToStr(jmax+1)+']';
Edit4.Text:=IntToStr(A[imin,jmin])+' индексы ['+IntToStr(imin+1)+','+IntToStr(jmin+1)+']';
end;
procedure TForm1.Button3Click(Sender: TObject);
Var
i, j, p: integer;
begin
p:=A[imax,jmax];
A[imax,jmax]:=A[imin,jmin];
A[imin,jmin]:=p;
with StringGrid do
begin
for i:=1 to n do
for j:=1 to m do
Cells[j,i]:=IntToStr(A[i-1,j-1]);
end;
end;
end.
|
|
#8
|
||||
|
||||
|
Много чего надо изменить, но самое главное это включить для StringGrida опцию goEditing в True.
|
|
#9
|
|||
|
|||
|
я включил опцию goEditing, и изменил код....но теперь выдаёт ошибку EConvertError при нажатие на Button1
Код:
unit Unit_masiv;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
edt1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
A : array of array of integer;
m, n: integer;
imax, jmax, imin, jmin: integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
i, j: integer;
begin
SetLength(A,n,m);
for j:=0 to m-1 do
for i:=0 to n-1 do
begin
A[i,j]:=StrToInt(StringGrid1.Cells[j,i]);
end;
imax:=0;
jmax:=0;
For i:=0 to High(A) do
For j:=0 to High(A) do
if A[i,j] > A[imax,jmax] then begin
imax:=i;
jmax:=j;
end;
edt1.Text:='Skaitlis: '+IntToStr(A[imax,jmax])+' Indeks: ['+IntToStr(imax+1)+','+IntToStr(jmax+1)+']';
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
i, j: integer;
begin
if (Edit1.Text<>'') and (Edit2.Text<>'') then
begin
n:=StrToInt(Edit1.Text);
m:=StrToInt(Edit2.Text);
StringGrid1.RowCount:=n+1;
StringGrid1.ColCount:=m+1;
with StringGrid1 do
begin
for i:=1 to RowCount do
begin
Cells[i,0]:=IntToStr(i);
end;
for j:=1 to ColCount do
begin
Cells[0,j]:=IntToStr(j);
end;
end;
end
else edt1.Text:='Error!!! Ievadit dimensiju!';
end;
end.
|
|
#10
|
||||
|
||||
|
У вас видимо ошибка преобразования текста в число, поэтому лучше использовать StrToIntDef вместо StrToInt
|
|
#11
|
|||
|
|||
|
Не помогло пишет ошибку
[Error] Unit_masiv.pas(43): Not enough actual parameters [Fatal Error] Project_masiv.dpr(5): Could not compile used unit 'Unit_masiv.pas' и выделяет строчку A[i,j]:=StrToIntDef(StringGrid1.Cells[i,j]); |
|
#12
|
||||
|
||||
|
Там предполагается вторым параметром значение по умолчанию, если произойдет ошибка преобразования:
Код:
A[i,j]:=StrToIntDef(StringGrid1.Cells[i,j], 0); |
|
#13
|
|||
|
|||
|
Спасибо огромное, помогло работает!!! :-)
|
|
#14
|
||||
|
||||
|
Все-таки проблема была в этой строке A[i,j]:=StrToIntDef(StringGrid1.Cells[i,j]); там нужно написать A[i,j]:=StrToIntDef(StringGrid1.Cells[i+1,j+1]);
ведь в ячейке[0,0] пусто. Вот я накатал пример (нечем заняться) |