№5 КОНТРОЛЬНАЯ РАБОТА по дисциплине «Структуры и алгоритмы обработки данных»
- Составить алгоритм упорядочивания слов заданного текста по алфавиту
- Имеется текст из нескольких строк; строки разделены возвратами каретки, слова в строке разделены пробелами.
- Вывести исходный текст и упорядоченный.
Сортировка «вставкой»
type
DataItem = char;
DataArray = array [1..100] of char;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click (Sender: TObject);
const Alpha : set of char=['A'..'Z','А'..'П','Р'..'Я','a'..'z','а'..'п','р'..'я'];
var s,t:string;
i:integer;
begin
s:=ListBox2.Items.Text;
i:=1;
Repeat
while NOT (s[i] in Alpha) and (i<=length (s)) do inc (i);
t:='';
while (s[i] in Alpha) and (i<=length (s)) do begin
t:=t+s[i];
inc (i);
end;
if length (t)<>0 then
ListBox1.Items.Add (t)
Until (i>length (s));
ListBox1.Sorted:=True;
end;
procedure TForm1.Button2Click (Sender: TObject);
Var Z, Y, i, j,x: Integer;
Begin
For i := 2 to ListBox2.Count do
For j := 1 to i-1 do
If ListBox2.Items[j] > ListBox2.Items[i] then
begin
Z := strtoint (ListBox2.Items[i]);
For Y := i downto j+1 do ListBox2.Items[Y] := ListBox2.Items[Y-1];
ListBox2.Items[j] :=IntToStr ( Z);
end
end;
procedure TForm1.Button3Click (Sender: TObject);
Var Z, Y, i, j: Integer;
Begin
For i := 0 to ListBox3.Count-1 do
For j := 0 to i-1 do
If ListBox3.Items[j] > ListBox3.Items[i] then
begin
Z := strtoint (ListBox3.Items[i]);
For Y := i downto j+1 do ListBox3.Items[Y] := ListBox3.Items[Y-1];
ListBox3.Items[j] := IntToStr (z);
end;
end;
procedure TForm1.Button5Click (Sender: TObject);
begin
ListBox1.Sorted:=False;
end;
procedure TForm1.FormCreate (Sender: TObject);
begin
ListBox2.Items.LoadFromFile ('text.txt');
end;
end.