Advertisement
MaksNew

Untitled

Feb 1st, 2021
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.12 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, ClipBrd, Vcl.Menus,
  8.   Vcl.Samples.Spin;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     StringGridOfArray: TStringGrid;
  13.     Label1: TLabel;
  14.     Label2: TLabel;
  15.     Label3: TLabel;
  16.     Label4: TLabel;
  17.     StringGridOfIndex: TStringGrid;
  18.     FindPalindromButton: TButton;
  19.     SpinEditOfSizeArray: TSpinEdit;
  20.     MainMenu: TMainMenu;
  21.     FileButton: TMenuItem;
  22.     OpenFileButton: TMenuItem;
  23.     SaveFileButton: TMenuItem;
  24.     OpenDialog: TOpenDialog;
  25.     SaveDialog: TSaveDialog;
  26.     LabelOfResult: TLabel;
  27.     procedure FindPalindromButtonClick(Sender: TObject);
  28.     procedure FormActivate(Sender: TObject);
  29.     procedure StringGridOfArrayKeyPress(Sender: TObject; var Key: Char);
  30.     procedure SpinEditOfSizeArrayChange(Sender: TObject);
  31.     procedure OpenFileButtonClick(Sender: TObject);
  32.     procedure SaveFileButtonClick(Sender: TObject);
  33.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  34.   private
  35.     { Private declarations }
  36.   public
  37.     { Public declarations }
  38.   end;
  39.  
  40. var
  41.   Form1: TForm1;
  42.  
  43. implementation
  44.  
  45. {$R *.dfm}
  46.  
  47. function IsArrayCorrect(StringGrid: TStringGrid; MaxValue: TSpinEdit):Boolean;
  48. var
  49.     IsCorrect, VseOK: Boolean;
  50.     I: Integer;
  51. begin
  52.     IsCorrect := True;
  53.     I := 0;
  54.     while ((I < MaxValue.Value) and (IsCorrect)) do
  55.     begin
  56.         try
  57.             StrToInt(StringGrid.Cells[I, 0]);
  58.         except
  59.             IsCorrect := False;
  60.         end;
  61.         if IsCorrect then
  62.             if not((StrToInt(StringGrid.Cells[I, 0]) < 2000000000) and (StrToInt(StringGrid.Cells[I, 0]) > -2000000000)) then
  63.                 IsCorrect := False;
  64.         Inc(I);
  65.     end;
  66.     IsArrayCorrect := IsCorrect;
  67. end;
  68.  
  69. procedure SearchForPalindrom(StringGrid, StringGridOfIndex: TStringGrid; Size, RowCount, Col: Integer; ClientHeight: Integer; LabelOfResult: TLabel);
  70. var
  71.     Bufi, Bufj, J, I: Integer;
  72.     IsPalindrom: Boolean;
  73. begin
  74.     Bufi := 0;
  75.     Bufj := 0;
  76.     J := 0;
  77.     IsPalindrom := True;
  78.     for I := 0 to Size-1 do
  79.     Begin
  80.         J := Size;
  81.         if (StrToInt(StringGrid.Cells[I, 0]) = StrToInt(StringGrid.Cells[J, 0])) then
  82.         Begin
  83.             Bufi := I;
  84.             Bufj := J;
  85.             IsPalindrom := True;
  86.             while ((Bufi <> Bufj) and (IsPalindrom)) do
  87.             Begin
  88.                 if ((Bufi = I) and (BufJ = J) and (Bufi+1 = BufJ) and (StrToInt(StringGrid.Cells[Bufi+1, 0]) = StrToInt(StringGrid.Cells[Bufj, 0]))) then
  89.                     IsPalindrom := False;
  90.                 if (IsPalindrom and (StrToInt(StringGrid.Cells[Bufi, 0]) <> StrToInt(StringGrid.Cells[Bufj, 0]))) then
  91.                     IsPalindrom := False;
  92.                 Inc(Bufi);
  93.                 Dec(Bufj);
  94.             End;
  95.             if ((IsPalindrom) and (I <> J)) then
  96.             Begin
  97.                 StringGridOfIndex.Cells[0, Col] := IntToStr(I+1);
  98.                 StringGridOfIndex.Cells[1, Col] := IntToStr(J+1);
  99.                 StringGridOfIndex.Visible  := True;
  100.                 StringGridOfIndex.RowCount := RowCount;
  101.                 StringGridOfIndex.Height := 37 * StringGridOfIndex.RowCount;
  102.                 if (StringGridOfIndex.Height + StringGridOfIndex.Top < 100) then
  103.                     ClientHeight := 100
  104.                 else
  105.                     ClientHeight := StringGridOfIndex.Height + StringGridOfIndex.Top + 10;
  106.                 Inc(RowCount);
  107.                 Inc(Col);
  108.             End;
  109.         End;
  110.     End;
  111.     if (J > 0) then
  112.         SearchForPalindrom(StringGrid, StringGridOfIndex, Size - 1, RowCount, Col, ClientHeight, LabelOfResult)
  113.     else
  114.         if not(StringGridOfIndex.Visible) then
  115.             LabelOfResult.Visible := True;
  116.  
  117. end;
  118.  
  119. procedure TForm1.FindPalindromButtonClick(Sender : TObject);
  120. var
  121.     I, Size, RowCount, Col: Integer;
  122.     Buf: String;
  123.     IsCorrect: Boolean;
  124. begin
  125.     RowCount := 1;
  126.     Col := 0;
  127.     Size := SpinEditOfSizeArray.Value;
  128.     if IsArrayCorrect(StringGridOfArray, SpinEditOfSizeArray) then
  129.     begin
  130.         SearchForPalindrom(StringGridOfArray, StringGridOfIndex, Size-1, RowCount, Col, ClientHeight, LabelOfResult);
  131.         SaveFileButton.Enabled := True;
  132.     end
  133.     else
  134.     begin
  135.         MessageDlg('Проверьте введённые данные! Это должны быть целые числа в диапазоне +- 2*10^9', mtError, [mbOk], 0);
  136.         SaveFileButton.Enabled := False;
  137.     end;
  138. end;
  139.  
  140. procedure TForm1.FormActivate(Sender: TObject);
  141. begin
  142.     SaveFileButton.Enabled := False;
  143.     SpinEditOfSizeArray.Value := 3;
  144.     Label1.Caption := 'Дана последовательность натуральных чисел а1, а2, …, аn. Разработать рекурсивную' +#13#10 + 'процедуру нахождения i и j, таких что подпоследовательность аi, аi+1, …, аj является перевертышем.';
  145. end;
  146.  
  147. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  148. var
  149.     Choise: Integer;
  150. begin
  151.     Choise := Messagedlg('Вы уверены, что хотите выйти?', mtinformation, [mbYes, mbNo], 0);
  152.     case Choise of
  153.         mrYes: CanClose:= True;
  154.         mrNo: CanClose:= False;
  155.     end;
  156. end;
  157.  
  158. function IsFileCorrect(Path: String): boolean;
  159. var
  160.     I, Value: Integer;
  161.     IsCorrect: Boolean;
  162.     UserFile: TextFile;
  163.     Size: Integer;
  164. begin
  165.     AssignFile(UserFile, Path);
  166.     Reset(UserFile);
  167.     IsCorrect := True;
  168.     I := 0;
  169.     Try
  170.         Readln(UserFile, Size);
  171.     except
  172.         IsCorrect := False;
  173.     End;
  174.     while not(SeekEoLn(UserFile)) and IsCorrect do
  175.     Begin
  176.         Try
  177.             Read(UserFile, Value);
  178.         except
  179.             IsCorrect := False;
  180.         End;
  181.         Inc(I);
  182.     End;
  183.  
  184.     if I<>Size then
  185.         IsCorrect := False;
  186.  
  187.     CloseFile(UserFile);
  188.     IsFileCorrect := IsCorrect;
  189. end;
  190.  
  191. procedure TForm1.OpenFileButtonClick(Sender: TObject);
  192. var
  193.     I,Value, Size: Integer;
  194.     UserFile: TextFile;
  195.     Path: String;
  196. begin
  197.     if OpenDialog.Execute then
  198.     Begin
  199.         Path := OpenDialog.FileName;
  200.         if IsFileCorrect(Path) then
  201.         begin
  202.             AssignFile(UserFile, OpenDialog.FileName);
  203.             Reset(UserFile);
  204.             Readln(UserFile, Size);
  205.             SpinEditOfSizeArray.Value := Size;
  206.             for I := 0 to SpinEditOfSizeArray.Value-1 do
  207.                 Begin
  208.                     Read(UserFile, Value);
  209.                     StringGridOfArray.Cells[i, 0] := IntToStr(Value);
  210.                 End;
  211.             CloseFile(UserFile)
  212.         end
  213.         else
  214.             MessageDlg('Ошибка. Проверьте данные в файле.', mtError, [mbOK], 0)
  215.     End;
  216.     if Length(StringGridOfArray.Cells[0,0]) > 0 then
  217.         FindPalindromButton.Enabled := True;
  218. end;
  219.  
  220. procedure TForm1.SaveFileButtonClick(Sender: TObject);
  221. var
  222.     SaveFile: TextFile;
  223.     I: Integer;
  224. begin
  225.     if SaveDialog.Execute then
  226.     Begin
  227.         AssignFile(SaveFile, SaveDialog.FileName);
  228.         Rewrite(SaveFile);
  229.         Write(SaveFile, 'Для массива чисел: ');
  230.         for I := 0 to SpinEditOfSizeArray.Value-1 do
  231.             Write(SaveFile, StringGridOfArray.Cells[i, 0], ' ');
  232.         Writeln(SaveFile);
  233.         Writeln(SaveFile, 'Индексы элементов, которые являются крайними в подпоследовательности перевёртышей: ');
  234.         if (StringGridOfIndex.Visible) then
  235.         for I := 0 to SpinEditOfSizeArray.Value-1 do
  236.         begin
  237.             Write(SaveFile, StringGridOfIndex.Cells[0, I], ' ', StringGridOfIndex.Cells[1, I]);
  238.             Writeln(SaveFile, StringGridOfIndex.Cells[0, I], ' ', StringGridOfIndex.Cells[1, I])
  239.         end
  240.         else
  241.             Write(SaveFile, 'Отсутствуют!');
  242.         CloseFile(SaveFile)
  243.     End;
  244. end;
  245.  
  246. procedure TForm1.SpinEditOfSizeArrayChange(Sender: TObject);
  247. var
  248.     I: Integer;
  249. begin
  250.     SaveFileButton.Enabled := False;
  251.     LabelOfResult.Visible := False;
  252.     StringGridOfIndex.Visible := False;
  253.     StringGridOfIndex.Cells[0, 0] := '';
  254.     StringGridOfIndex.Cells[1, 0] := '';
  255.     StringGridOfIndex.RowCount := 1;
  256.     if ((SpinEditOfSizeArray.Value > 2) and (SpinEditOfSizeArray.Value < 18)) then
  257.     begin
  258.         StringGridOfArray.Options := StringGridOfArray.Options+[goEditing];
  259.         FindPalindromButton.Enabled := True;
  260.     end
  261.     else
  262.     begin
  263.         StringGridOfArray.Options := StringGridOfArray.Options-[goEditing];
  264.         FindPalindromButton.Enabled := False;
  265.     end;
  266.  
  267.     if (SpinEditOfSizeArray.Value < 18) then
  268.     begin
  269.         StringGridOfArray.ColCount := SpinEditOfSizeArray.Value;
  270.         StringGridOfArray.Width := 85 * StringGridOfArray.ColCount;
  271.         if (StringGridOfArray.Width + StringGridOfArray.Left < 682) then
  272.             ClientWidth := 682
  273.         else
  274.             ClientWidth := StringGridOfArray.Width + StringGridOfArray.Left + 10;
  275.     end;
  276. end;
  277.  
  278. procedure TForm1.StringGridOfArrayKeyPress(Sender: TObject; var Key: Char);
  279. begin
  280.     FindPalindromButton.Enabled := True;
  281.     if not(Key in ['0'..'9', '-', #8]) then
  282.         Key :=#0;
  283. end;
  284.  
  285. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement