Программа всё та же самая: не позволяет пользователю вводить в Edit'ы и StringGrid буквы, символы и прочую чепуху, не позволяет выходить за границы массива. Это я сделала (спасибо Вам blush
.
gif ). НО ещё нужно сделать чтобы программа не вылетала если мы вводим число превышающее диапазон
Integer
(-
2147483648..2147483647
) и как только будет попытка ввести число большее то программа должна выдавать сообщение "Превышен размер числа!" и стирать неправильное число.
Вот программа:
unit
laba3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 =
class
(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
procedure
Button1Click(Sender: TObject);
procedure
Edit1Change(Sender: TObject);
procedure
Edit1KeyPress(Sender: TObject;
var
Key:
Char
);
procedure
StringGrid1KeyPress(Sender: TObject;
var
Key:
Char
);
procedure
Edit2KeyPress(Sender: TObject;
var
Key:
Char
);
private
public
end
;
var
Form1: TForm1;
I,J:
integer
;
implementation
{$R *.dfm}
procedure
TForm1
.
Edit1Change(Sender: TObject);
begin
If
(StrToInt(Edit1
.
Text)>
20
)
OR
(StrToInt(Edit1
.
Text)<=
0
)
then
begin
ShowMessage(
'Выход за границы массива!'
);
Edit1
.
Text:=
'20'
;
end
;
StringGrid1
.
ColCount:=StrToInt(Edit1
.
Text)+
1
;
end
;
procedure
TForm1
.
Button1Click(Sender: TObject);
const
SIZE=
20
;
var
a:
array
[
1..
SIZE]
of
integer
;
n:
integer
;
found:
boolean
;
i,m:
integer
;
begin
n:=StrToInt(Edit1
.
text);
for
i:=
1
to
n-
1
do
a[i]:=StrToInt(StringGrid1
.
Cells[i,
1
]);
m:=StrToInt(Edit2
.
text);
found:=
false
;
i:=
1
;
repeat
if
a[i]=m
then
found:=
true
else
i:=i+
1
;
until
(i>n)
or
(found =
true
);
if
found=
true
then
ShowMessage(
'Совпадение с элементом номер '
+ IntToStr(i)+#
13
+
'Поиск успешно завершен'
)
else
ShowMessage(
'Заданный элемент не найден!'
);
end
;
procedure
TForm1
.
Edit1KeyPress(Sender: TObject;
var
Key:
Char
);
begin
case
Key
of
'0'
..
'9'
:;
else
begin
Key := #
0
;
ShowMessage(
'Недопустимый элемент!'
);
end
;
end
;
end
;
procedure
TForm1
.
StringGrid1KeyPress(Sender: TObject;
var
Key:
Char
);
begin
case
Key
of
'0'
..
'9'
:;
else
begin
Key := #
0
;
ShowMessage(
'Недопустимый элемент!'
);
end
;
end
;
end
;
procedure
TForm1
.
Edit2KeyPress(Sender: TObject;
var
Key:
Char
);
begin
case
Key
of
'0'
..
'9'
:;
else
begin
Key := #
0
;
ShowMessage(
'Недопустимый элемент!'
);
end
;
end
;
end
;
end
.