Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.Menus;
- type
- TForm1 = class(TForm)
- Label1: TLabel;
- StringGrid1: TStringGrid;
- StringGrid2: TStringGrid;
- Button1: TButton;
- StringGrid3: TStringGrid;
- Label2: TLabel;
- Edit1: TEdit;
- Label3: TLabel;
- MainMenu1: TMainMenu;
- PopupMenu1: TPopupMenu;
- SaveDialog1: TSaveDialog;
- OpenDialog1: TOpenDialog;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- procedure Edit1Change(Sender: TObject);
- procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
- procedure N4Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- type
- TArray = Array Of Integer;
- TStr = Array of String[5];
- var
- Form1: TForm1;
- TempArr : TArray;
- Path, Result : String;
- IsFileOpen : Boolean;
- Const
- BAD = 100000;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- Var
- Arr : TArray;
- I, J, K, N, Temp, Min : Integer;
- IsCorrect : Boolean;
- begin
- N := StringGrid1.ColCount;
- SetLength (Arr, N);
- IsCorrect := True;
- Result := 'Исходный массив: ';
- For I := 0 To N - 1 Do
- Begin
- Try
- Arr[I] := StrToInt(StringGrid1.Cells[I, 0]);
- Result := Result + (StringGrid1.Cells[I, 0]) + ' ';
- Except
- IsCorrect := False;
- End;
- End;
- If IsCorrect then
- Begin
- K := 0;
- For I := 0 To High(Arr) - 1 do
- Begin
- For J := I + 1 To High(Arr) do
- If (Arr[J] < Arr[I]) then
- Begin
- Min := Arr[J];
- Arr[J] := Arr[I];
- Arr[I] := Min;
- End;
- For Temp := 0 To High(Arr) Do
- Begin
- StringGrid2.Cells[Temp + 1, K] := IntToStr(Arr[Temp]);
- StringGrid2.Cells[0, K] := 'Этап ' + IntToStr(K + 1);
- End;
- Inc(K);
- End;
- Result := Result + ' Отсортированный массив: ';
- For I := 0 To High(Arr) Do
- begin
- StringGrid3.Cells[I, 0] := IntToStr(Arr[I]);
- Result := Result + IntToStr(Arr[I]) + ' ';
- end;
- StringGrid2.Visible := True;
- StringGrid3.Visible := True;
- Label2.Visible := True;
- N3.Enabled := True;
- End;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- Var
- N, J, I: Integer;
- IsCorrect : Boolean;
- Const
- WIDE = 82;
- HEIGHT = 35;
- begin
- IsCorrect := True;
- try
- N := StrToInt(Edit1.Text);
- except
- IsCorrect := False;
- end;
- If Iscorrect then
- begin
- StringGrid1.ColCount := N;
- StringGrid1.ScrollBars := ssHorizontal;
- StringGrid1.Height := 55;
- StringGrid1.Width := WIDE * 7;
- If N < 8 then
- Begin
- StringGrid1.Height := HEIGHT;
- StringGrid1.ScrollBars := ssNone;
- case N of
- 1: StringGrid1.Width := WIDE;
- 2: StringGrid1.Width := WIDE * 2 + 1;
- 3: StringGrid1.Width := WIDE * 3;
- 4: StringGrid1.Width := WIDE * 4;
- 5: StringGrid1.Width := WIDE * 5;
- 6: StringGrid1.Width := WIDE * 6 - 2;
- end;
- End;
- for I := 0 to StringGrid1.RowCount - 1 do
- for J := 0 to StringGrid1.ColCount - 1 do
- StringGrid1.Cells[J, I] := '';
- for I := 0 to StringGrid2.RowCount - 1 do
- for J := 0 to StringGrid2.ColCount - 1 do
- StringGrid2.Cells[J, I] := '';
- StringGrid2.Visible := False;
- StringGrid3.Visible := False;
- StringGrid1.Visible := True;
- StringGrid2.ColCount := N + 1;
- Label2.Visible := False;
- N3.Enabled := False;
- StringGrid2.ScrollBars := ssHorizontal;
- StringGrid2.Width := WIDE * 7;
- If N < 7 then
- Begin
- StringGrid2.ScrollBars := ssNone;
- case N of
- 1: StringGrid2.Width := WIDE * 2;
- 2: StringGrid2.Width := WIDE * 3 + 1;
- 3: StringGrid2.Width := WIDE * 4;
- 4: StringGrid2.Width := WIDE * 5;
- 5: StringGrid2.Width := WIDE * 6;
- 6: StringGrid2.Width := WIDE * 7 - 2;
- end;
- End;
- StringGrid3.ColCount := N;
- StringGrid3.ScrollBars := ssHorizontal;
- StringGrid3.Height := 55;
- StringGrid3.Width := WIDE * 7;
- If N < 8 then
- Begin
- StringGrid3.Height := HEIGHT;
- StringGrid3.ScrollBars := ssNone;
- case N of
- 1: StringGrid3.Width := WIDE;
- 2: StringGrid3.Width := WIDE * 2 + 1;
- 3: StringGrid3.Width := WIDE * 3;
- 4: StringGrid3.Width := WIDE * 4;
- 5: StringGrid3.Width := WIDE * 5;
- 6: StringGrid3.Width := WIDE * 6 - 2;
- end;
- End;
- Button1.Visible := False;
- end;
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key = #13) and (Button1.Enabled) then
- Button1.Click;
- If (Not(Key In ['2'..'9', #08, #46])) Then
- Key := #0;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Form1.Handle, 'Вы уверены, что хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION)=ID_YES;
- end;
- procedure TForm1.N4Click(Sender: TObject);
- begin
- Application.MessageBox('Программа сортирует массив методом простого выбора с пошаговой детализацией.'#13#10'Размер массива: 2..9.'#13#10'Диапазон элементов массива: -9999..99999', 'Инструкция', 0);
- end;
- procedure TForm1.N5Click(Sender: TObject);
- begin
- Application.MessageBox('Арефин Владислав гр.251004', 'Разрабочик', 0);
- end;
- procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
- Const
- LIM = 4; // значит 5 - реальный лимит символов
- begin
- If (Not(Key In ['0'..'9', #08, #46, '-'])) Then
- Key := #0;
- With Sender As TStringGrid Do
- Begin
- If (Length(StringGrid1.Cells[Col, Row]) > LIM) then
- If (Not(Key In [#08, #46])) Then
- Key := #0;
- If (Length(StringGrid1.Cells[Col, Row]) = 1) and (Key = '-') then
- Key := #0;
- End;
- end;
- procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- var
- I : Integer;
- IsCorrect : Boolean;
- Temp : String;
- begin
- IsCorrect := True;
- for I := 0 to StringGrid1.ColCount - 1 do
- If (Length(StringGrid1.Cells[I, 0]) = 0) Then
- begin
- Button1.Visible := False;
- IsCorrect := False;
- end;
- If IsCorrect then
- begin
- Button1.Visible := True;
- end;
- N3.Enabled := False;
- end;
- Function CheckFileDataForN(Num: String): Boolean;
- Const
- MAX_LIM = 99999;
- MIN_LIM = 2;
- Var
- NewNum: Integer;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Num := Trim (Num);
- Try
- NewNum := StrToInt(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Не получилось преобразовать N к целочисленному типу данных. Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := IntToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('N вне разрешенного диапазона! Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileDataForN := IsCorrect;
- End;
- Function Open (): String;
- Begin
- With Form1 Do
- Begin
- If OpenDialog1.Execute Then
- Begin
- Path := OpenDialog1.FileName;
- IsFileOpen := True;
- End
- Else
- IsFileOpen := False;
- End;
- Open := Path;
- End;
- Function TakeDataFromFile2(Number2: String; Var FileOutput: TextFile): String;
- Var //N
- IsRight : Boolean;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Number2);
- Number2 := Trim (Number2);
- IsRight := CheckFileDataForN(Number2);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile2 := ''
- Else
- TakeDataFromFile2 := Number2;
- End;
- function SeparateString (Str : String) : TStr;
- Var
- StrArr : TStr;
- I, K : Integer;
- Flag : Boolean;
- Begin
- K := 0;
- SetLength(StrArr, (Str.Length div 2) + 1);
- For I := 0 To Str.Length div 2 do
- StrArr[i] := '';
- I := 1;
- While I <= Str.Length Do
- begin
- Flag := true;
- While (Str[I] <> ' ') and (I <= Str.Length) Do
- Begin
- StrArr[K] := StrArr[K] + Str[I];
- Inc(I);
- Flag := False;
- End;
- If Not(Flag) then
- Inc(K);
- If Flag then
- Inc(I);
- end;
- I := 1;
- K := 0;
- While Str[I] <> '' Do
- Begin
- If Str[I] = ' ' then
- Inc(K);
- Inc(I);
- End;
- SetLength(StrArr, K + 1);
- Result := StrArr;
- End;
- function ConvertStringToArray (StringGridColCount : Integer; Var FileOutput: TextFile) : TArray;
- Var
- I: Integer;
- Arr : TArray;
- Str : String;
- StrArr : TStr;
- Const
- MIN = -9999;
- MAX = 99999;
- Begin
- Readln(FileOutput, Str);
- SetLength(Arr, StringGridColCount);
- for I := Low(Arr) to High(Arr) do
- Arr[I] := 0;
- Str := Trim(Str);
- StrArr := SeparateString (Str);
- If (High(StrArr) + 1 <> StringGridColCount) then
- begin
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Количество элементов массива не совпадает с заявленным. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- Exit
- end;
- try
- For I := Low(Arr) to High(Arr) do
- Arr[I] := StrToInt (StrArr[I]);
- except
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Не удалось преобразовать исходные данные в целочисленный тип. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- Exit
- end;
- For I := Low(Arr) to High(Arr) do
- If (Arr[I] > MAX) or (Arr[I] < MIN) then
- begin
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Исходные данные выходят за границы допустимых. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- end;
- ConvertStringToArray := Arr;
- End;
- procedure TForm1.N2Click(Sender: TObject);
- Var
- FileInput: TextFile;
- Num, I: Integer;
- Str1, Str2 : String;
- Arr : TArray;
- begin
- Num := 0;
- Path := Open;
- AssignFile(FileInput, Path);
- Reset(FileInput);
- If(IsFileOpen) Then
- Begin
- Str1 := TakeDataFromFile2(IntToStr(Num), FileInput);
- End;
- if Not(Str1 = '') then
- Begin
- Edit1.Text := Str1;
- End
- Else
- Begin
- Edit1.Text := #0;
- End;
- if (Str1 <> '') then
- Begin
- Arr := ConvertStringToArray(StrToInt(Str1), FileInput);
- if (Arr[0] <> BAD) then
- Begin
- for I := Low(Arr) to High(Arr) do
- Begin
- StringGrid1.Cells[I, 0] := IntToStr(Arr[I]);
- Button1.Visible := True;
- End;
- End
- else
- Begin
- for I := Low(Arr) to StrToInt(Str1) - 1 do
- Begin
- StringGrid1.Cells[I, 0] := '';
- End;
- Edit1.Text := '';
- End;
- End;
- CloseFile(FileInput);
- end;
- procedure TForm1.N3Click(Sender: TObject);
- Var
- FileOutput: TextFile;
- IsCorrect : Boolean;
- begin
- IsCorrect := True;
- Path := Open;
- If (IsFileOpen) Then
- Begin
- try
- AssignFile(FileOutput, Path);
- Rewrite(FileOutput);
- Write(FileOutput, Result);
- except
- IsCorrect := False;
- Application.MessageBox('Запись в файл не удалась.', 'Ошибка', MB_ICONSTOP);
- end;
- if IsCorrect then
- Begin
- Application.MessageBox('Запись файла выполнена успешно.', 'Результат', 0);
- CloseFile(FileOutput);
- Edit1.Text := '2';
- End;
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement