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.Grids, Vcl.StdCtrls, ClipBrd, Vcl.Menus,
- Vcl.Samples.Spin;
- type
- TForm1 = class(TForm)
- StringGridOfArray: TStringGrid;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- StringGridOfIndex: TStringGrid;
- FindPalindromButton: TButton;
- SpinEditOfSizeArray: TSpinEdit;
- MainMenu: TMainMenu;
- FileButton: TMenuItem;
- OpenFileButton: TMenuItem;
- SaveFileButton: TMenuItem;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- LabelOfResult: TLabel;
- procedure FindPalindromButtonClick(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure StringGridOfArrayKeyPress(Sender: TObject; var Key: Char);
- procedure SpinEditOfSizeArrayChange(Sender: TObject);
- procedure OpenFileButtonClick(Sender: TObject);
- procedure SaveFileButtonClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- function IsArrayCorrect(StringGrid: TStringGrid; MaxValue: TSpinEdit):Boolean;
- var
- IsCorrect, VseOK: Boolean;
- I: Integer;
- begin
- IsCorrect := True;
- I := 0;
- while ((I < MaxValue.Value) and (IsCorrect)) do
- begin
- try
- StrToInt(StringGrid.Cells[I, 0]);
- except
- IsCorrect := False;
- end;
- if IsCorrect then
- if not((StrToInt(StringGrid.Cells[I, 0]) < 2000000000) and (StrToInt(StringGrid.Cells[I, 0]) > -2000000000)) then
- IsCorrect := False;
- Inc(I);
- end;
- IsArrayCorrect := IsCorrect;
- end;
- procedure SearchForPalindrom(StringGrid, StringGridOfIndex: TStringGrid; Size, RowCount, Col: Integer; ClientHeight: Integer; LabelOfResult: TLabel);
- var
- Bufi, Bufj, J, I: Integer;
- IsPalindrom: Boolean;
- begin
- Bufi := 0;
- Bufj := 0;
- J := 0;
- IsPalindrom := True;
- for I := 0 to Size-1 do
- Begin
- J := Size;
- if (StrToInt(StringGrid.Cells[I, 0]) = StrToInt(StringGrid.Cells[J, 0])) then
- Begin
- Bufi := I;
- Bufj := J;
- IsPalindrom := True;
- while ((Bufi <> Bufj) and (IsPalindrom)) do
- Begin
- if ((Bufi = I) and (BufJ = J) and (Bufi+1 = BufJ) and (StrToInt(StringGrid.Cells[Bufi+1, 0]) = StrToInt(StringGrid.Cells[Bufj, 0]))) then
- IsPalindrom := False;
- if (IsPalindrom and (StrToInt(StringGrid.Cells[Bufi, 0]) <> StrToInt(StringGrid.Cells[Bufj, 0]))) then
- IsPalindrom := False;
- Inc(Bufi);
- Dec(Bufj);
- End;
- if ((IsPalindrom) and (I <> J)) then
- Begin
- StringGridOfIndex.Cells[0, Col] := IntToStr(I+1);
- StringGridOfIndex.Cells[1, Col] := IntToStr(J+1);
- StringGridOfIndex.Visible := True;
- StringGridOfIndex.RowCount := RowCount;
- StringGridOfIndex.Height := 37 * StringGridOfIndex.RowCount;
- if (StringGridOfIndex.Height + StringGridOfIndex.Top < 100) then
- ClientHeight := 100
- else
- ClientHeight := StringGridOfIndex.Height + StringGridOfIndex.Top + 10;
- Inc(RowCount);
- Inc(Col);
- End;
- End;
- End;
- if (J > 0) then
- SearchForPalindrom(StringGrid, StringGridOfIndex, Size - 1, RowCount, Col, ClientHeight, LabelOfResult)
- else
- if not(StringGridOfIndex.Visible) then
- LabelOfResult.Visible := True;
- end;
- procedure TForm1.FindPalindromButtonClick(Sender : TObject);
- var
- I, Size, RowCount, Col: Integer;
- Buf: String;
- IsCorrect: Boolean;
- begin
- RowCount := 1;
- Col := 0;
- Size := SpinEditOfSizeArray.Value;
- if IsArrayCorrect(StringGridOfArray, SpinEditOfSizeArray) then
- begin
- SearchForPalindrom(StringGridOfArray, StringGridOfIndex, Size-1, RowCount, Col, ClientHeight, LabelOfResult);
- SaveFileButton.Enabled := True;
- end
- else
- begin
- MessageDlg('Проверьте введённые данные! Это должны быть целые числа в диапазоне +- 2*10^9', mtError, [mbOk], 0);
- SaveFileButton.Enabled := False;
- end;
- end;
- procedure TForm1.FormActivate(Sender: TObject);
- begin
- SaveFileButton.Enabled := False;
- SpinEditOfSizeArray.Value := 3;
- Label1.Caption := 'Дана последовательность натуральных чисел а1, а2, …, аn. Разработать рекурсивную' +#13#10 + 'процедуру нахождения i и j, таких что подпоследовательность аi, аi+1, …, аj является перевертышем.';
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- var
- Choise: Integer;
- begin
- Choise := Messagedlg('Вы уверены, что хотите выйти?', mtinformation, [mbYes, mbNo], 0);
- case Choise of
- mrYes: CanClose:= True;
- mrNo: CanClose:= False;
- end;
- end;
- function IsFileCorrect(Path: String): boolean;
- var
- I, Value: Integer;
- IsCorrect: Boolean;
- UserFile: TextFile;
- Size: Integer;
- begin
- AssignFile(UserFile, Path);
- Reset(UserFile);
- IsCorrect := True;
- I := 0;
- Try
- Readln(UserFile, Size);
- except
- IsCorrect := False;
- End;
- while not(SeekEoLn(UserFile)) and IsCorrect do
- Begin
- Try
- Read(UserFile, Value);
- except
- IsCorrect := False;
- End;
- Inc(I);
- End;
- if I<>Size then
- IsCorrect := False;
- CloseFile(UserFile);
- IsFileCorrect := IsCorrect;
- end;
- procedure TForm1.OpenFileButtonClick(Sender: TObject);
- var
- I,Value, Size: Integer;
- UserFile: TextFile;
- Path: String;
- begin
- if OpenDialog.Execute then
- Begin
- Path := OpenDialog.FileName;
- if IsFileCorrect(Path) then
- begin
- AssignFile(UserFile, OpenDialog.FileName);
- Reset(UserFile);
- Readln(UserFile, Size);
- SpinEditOfSizeArray.Value := Size;
- for I := 0 to SpinEditOfSizeArray.Value-1 do
- Begin
- Read(UserFile, Value);
- StringGridOfArray.Cells[i, 0] := IntToStr(Value);
- End;
- CloseFile(UserFile)
- end
- else
- MessageDlg('Ошибка. Проверьте данные в файле.', mtError, [mbOK], 0)
- End;
- if Length(StringGridOfArray.Cells[0,0]) > 0 then
- FindPalindromButton.Enabled := True;
- end;
- procedure TForm1.SaveFileButtonClick(Sender: TObject);
- var
- SaveFile: TextFile;
- I: Integer;
- begin
- if SaveDialog.Execute then
- Begin
- AssignFile(SaveFile, SaveDialog.FileName);
- Rewrite(SaveFile);
- Write(SaveFile, 'Для массива чисел: ');
- for I := 0 to SpinEditOfSizeArray.Value-1 do
- Write(SaveFile, StringGridOfArray.Cells[i, 0], ' ');
- Writeln(SaveFile);
- Writeln(SaveFile, 'Индексы элементов, которые являются крайними в подпоследовательности перевёртышей: ');
- if (StringGridOfIndex.Visible) then
- for I := 0 to SpinEditOfSizeArray.Value-1 do
- begin
- Write(SaveFile, StringGridOfIndex.Cells[0, I], ' ', StringGridOfIndex.Cells[1, I]);
- Writeln(SaveFile, StringGridOfIndex.Cells[0, I], ' ', StringGridOfIndex.Cells[1, I])
- end
- else
- Write(SaveFile, 'Отсутствуют!');
- CloseFile(SaveFile)
- End;
- end;
- procedure TForm1.SpinEditOfSizeArrayChange(Sender: TObject);
- var
- I: Integer;
- begin
- SaveFileButton.Enabled := False;
- LabelOfResult.Visible := False;
- StringGridOfIndex.Visible := False;
- StringGridOfIndex.Cells[0, 0] := '';
- StringGridOfIndex.Cells[1, 0] := '';
- StringGridOfIndex.RowCount := 1;
- if ((SpinEditOfSizeArray.Value > 2) and (SpinEditOfSizeArray.Value < 18)) then
- begin
- StringGridOfArray.Options := StringGridOfArray.Options+[goEditing];
- FindPalindromButton.Enabled := True;
- end
- else
- begin
- StringGridOfArray.Options := StringGridOfArray.Options-[goEditing];
- FindPalindromButton.Enabled := False;
- end;
- if (SpinEditOfSizeArray.Value < 18) then
- begin
- StringGridOfArray.ColCount := SpinEditOfSizeArray.Value;
- StringGridOfArray.Width := 85 * StringGridOfArray.ColCount;
- if (StringGridOfArray.Width + StringGridOfArray.Left < 682) then
- ClientWidth := 682
- else
- ClientWidth := StringGridOfArray.Width + StringGridOfArray.Left + 10;
- end;
- end;
- procedure TForm1.StringGridOfArrayKeyPress(Sender: TObject; var Key: Char);
- begin
- FindPalindromButton.Enabled := True;
- if not(Key in ['0'..'9', '-', #8]) then
- Key :=#0;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement