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)
- Edit1: TEdit;
- Edit2: TEdit;
- StringGrid1: TStringGrid;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Button1: TButton;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- MainMenu1: TMainMenu;
- PopupMenu1: TPopupMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N5: TMenuItem;
- procedure Edit1Change(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Button1Click(Sender: TObject);
- procedure Edit2KeyPress(Sender: TObject; var Key: Char);
- procedure Edit2Change(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- N : Integer;
- Arr : Array of Integer;
- IsCorrect, IsFileOpen : Boolean;
- Path, Result : String;
- implementation
- {$R *.dfm}
- procedure Swap (var Arr : Array of Integer; I, J : Integer);
- Var
- Temp : Integer;
- Begin
- Temp := Arr[I];
- Arr[I] := Arr[J];
- Arr[J] := Temp;
- End;
- Function NextSet (var Arr : Array of Integer; N : Integer) : Boolean;
- Var
- I, J, K, L, R : Integer;
- AnythingElse : Boolean;
- Begin
- AnythingElse := True;
- J := N - 2;
- While ((J <> -1) and (Arr[J] >= Arr[J + 1])) Do
- begin
- Dec(J);
- end;
- If (J = -1) then
- AnythingElse := False;
- If AnythingElse then
- Begin
- K := N - 1;
- While (Arr[J] >= Arr[K]) Do
- begin
- Dec(K);
- end;
- Swap(Arr, J, K);
- L := J + 1;
- R := N - 1;
- While (L < R) Do
- Begin
- Swap(Arr, L, R);
- Inc(L);
- Dec(R);
- End;
- End;
- NextSet := AnythingElse;
- End;
- procedure TForm1.Button1Click(Sender: TObject);
- Const
- WIDE = 82;
- HEIGHT = 35;
- SCROLLBAR = 20;
- Var
- I, J, Number, K : Integer;
- Str, Element: String;
- IsSmthWrong : Boolean;
- begin
- If IsCorrect then
- begin
- IsSmthWrong := True;
- SetLength(Arr, N);
- StringGrid1.ColCount := N + 1;
- StringGrid1.Width := WIDE * 6 + + SCROLLBAR;
- If N < 5 then
- Begin
- //StringGrid1.Height := HEIGHT;
- StringGrid1.ScrollBars := ssVertical;
- case N of
- 1: StringGrid1.Width := WIDE * 2 + 1;
- 2: StringGrid1.Width := WIDE * 3;
- 3: StringGrid1.Width := WIDE * 4 - 10;
- 4: StringGrid1.Width := WIDE * 5 + SCROLLBAR;
- end;
- End;
- For I := 0 to StringGrid1.RowCount - 1 do
- For J := 0 to StringGrid1.ColCount - 1 do
- StringGrid1.Cells[J, I] := '';
- Str := Trim(Edit2.Text);
- Str := Str + ' ';
- I := 1;
- K := 0;
- try
- While (I <> (Length(Str) + 1)) and (IsSmthWrong) Do
- Begin
- If (Str[I] <> ' ') then
- Element := Element + Str[I]
- else
- Begin
- Number := StrToInt(Element);
- try
- Arr[K] := Number;
- except
- IsSmthWrong := False;
- end;
- Inc(K);
- Element := '';
- End;
- Inc(I);
- If ((K) > N) then
- begin
- MessageBox(Form1.Handle, Pchar('Вы ввели больше ' + Edit1.Text + ' элементов.'), 'Ошибка', MB_ICONSTOP);
- IsSmthWrong := False;
- Edit2.Text := '';
- StringGrid1.Visible := False;
- end;
- End;
- except
- Edit2.Text := '';
- IsSmthWrong := False;
- StringGrid1.Visible := False;
- MessageBox(Form1.Handle, Pchar('Проверьте последовательность, там должны быть только положительные цифры.'), 'Ошибка', MB_ICONSTOP);
- end;
- If ((K) < N) and IsSmthWrong then
- begin
- MessageBox(Form1.Handle, Pchar('Вы ввели меньше ' + Edit1.Text + ' элементов.'), 'Ошибка', MB_ICONSTOP);
- IsSmthWrong := False;
- Edit2.Text := '';
- StringGrid1.Visible := False;
- end;
- If IsSmthWrong then
- Begin
- For I := 0 To High(Arr) - 1 do
- For J := I + 1 To High(Arr) do
- If (Arr[J] < Arr[I]) then
- Swap(Arr, J, I);
- StringGrid1.Cells[0, 0] := '1: ';
- Result := Result + StringGrid1.Cells[0, 0];
- For J := 1 To (N) Do
- Begin
- StringGrid1.Cells[J, 0] := InttoStr(Arr[J - 1]);
- Result := Result + StringGrid1.Cells[J, 0] + ' ';
- End;
- Result := Result + #13#10;
- StringGrid1.Visible := True;
- I := 1;
- While (NextSet(Arr, N)) Do
- begin
- StringGrid1.RowCount := StringGrid1.RowCount + 1;
- //StringGrid1.Height := StringGrid1.Height + HEIGHT;
- StringGrid1.Cells[0, I] := IntToStr(I + 1) + ': ';
- Result := Result + StringGrid1.Cells[0, I];
- For J := 1 To (N) Do
- Begin
- StringGrid1.Cells[J, I] := InttoStr(Arr[J - 1]);
- Result := Result + StringGrid1.Cells[J, I] + ' ';
- End;
- StringGrid1.Cells[0, I] := IntToStr(I + 1) + ':';
- Inc(I);
- Result := Result + #13#10;
- end;
- StringGrid1.Visible := True;
- N3.Enabled := True;
- End;
- end;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- IsCorrect := True;
- StringGrid1.Visible := False;
- N3.Enabled := False;
- StringGrid1.RowCount := 1;
- try
- N := StrToInt(Edit1.Text);
- except
- IsCorrect := False;
- end;
- If (Edit1.Text <> '') and (Edit2.Text <> '') then
- Button1.Enabled := True
- else
- Button1.Enabled := False;
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- If (Key = #13) and (Button1.Enabled) then
- Button1.Click;
- If (Not(Key In ['2'..'5', #08, #46])) Then
- Key := #0;
- end;
- procedure TForm1.Edit2Change(Sender: TObject);
- begin
- If (Edit1.Text <> '') and (Edit2.Text <> '') then
- Button1.Enabled := True
- else
- Button1.Enabled := False;
- StringGrid1.RowCount := 1;
- N3.Enabled := False;
- end;
- procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
- begin
- If (Key = #13) and (Button1.Enabled) then
- Button1.Click;
- If (Not(Key In ['0'..'9', #08, #46, #32])) Then
- Key := #0;
- end;
- procedure TForm1.N5Click(Sender: TObject);
- begin
- Application.MessageBox('Арефин Владислав гр.251004', 'Разрабочик', 0);
- end;
- Function CheckFileData(Num: String): Boolean;
- Const
- MAX_LIM = 5;
- MIN_LIM = 2;
- Var
- NewNum: Integer;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Try
- NewNum := StrToInt(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := IntToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('Недопустимое значение!'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileData := 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 TakeDataFromFile( Var FileOutput: TextFile): String;
- Var
- IsRight : Boolean;
- Str : String;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Str);
- IsRight := CheckFileData(Str);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile := ''
- Else
- TakeDataFromFile := Str;
- End;
- Function Take2FromFile(Var FileOutput: TextFile): String;
- Var
- IsRight : Boolean;
- Str : String;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Str);
- Except
- IsRight := False;
- End;
- If(Not(IsRight)) Then
- Take2FromFile := ''
- Else
- Take2FromFile := Str;
- End;
- procedure TForm1.N2Click(Sender: TObject);
- Var
- FileInput: TextFile;
- Num : Integer;
- Str1, Str2: String;
- begin
- Num := 0;
- Path := Open;
- AssignFile(FileInput, Path);
- Reset(FileInput);
- If(IsFileOpen) Then
- Begin
- Str1 := TakeDataFromFile(FileInput);
- Str2 := Take2FromFile(FileInput);
- End;
- if (Length(Str1) > 0) then
- Begin
- Edit1.Text := Str1;
- End
- Else
- Edit1.Text := #0;
- if (Length(Str2) > 0) and ((Length(Str1) > 0))then
- Begin
- Edit2.Text := Str2;
- End
- Else
- Edit2.Text := #0;
- CloseFile(FileInput);
- end;
- procedure TForm1.N3Click(Sender: TObject);
- Var
- FileOutput: TextFile;
- begin
- Path := Open;
- If (IsFileOpen) Then
- Begin
- AssignFile(FileOutput, Path);
- Rewrite(FileOutput);
- Write(FileOutput, Result);
- Application.MessageBox('Запись файла выполнена успешно.', 'Результат', 0);
- CloseFile(FileOutput);
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement