Advertisement
SmnVadik

Lab 6.2 (Delphi)

Sep 9th, 2023 (edited)
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.54 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.     Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.     System.Classes, Vcl.Graphics,
  8.     Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.Menus;
  9.  
  10. type
  11.     TForm1 = class(TForm)
  12.     ButtonSolve: TButton;
  13.         Memo1: TMemo;
  14.         StringGridPoints: TStringGrid;
  15.     ButtonPoints: TButton;
  16.         EditAmount: TEdit;
  17.         MainMenu1: TMainMenu;
  18.         N1: TMenuItem;
  19.         N2: TMenuItem;
  20.         N3: TMenuItem;
  21.         N5: TMenuItem;
  22.         N6: TMenuItem;
  23.         N7: TMenuItem;
  24.         N8: TMenuItem;
  25.         OpenDialog1: TOpenDialog;
  26.         SaveDialog1: TSaveDialog;
  27.         procedure ButtonSolveClick(Sender: TObject);
  28.         procedure ButtonPointsClick(Sender: TObject);
  29.         procedure FormCreate(Sender: TObject);
  30.         procedure EditAmountKeyPress(Sender: TObject; var Key: Char);
  31.         procedure EditAmountChange(Sender: TObject);
  32.         procedure StringGridPointsKeyPress(Sender: TObject; var Key: Char);
  33.         procedure StringGridPointsSetEditText(Sender: TObject;
  34.           ACol, ARow: Integer; const Value: string);
  35.         procedure N3Click(Sender: TObject);
  36.         procedure N8Click(Sender: TObject);
  37.         procedure N5Click(Sender: TObject);
  38.         procedure N6Click(Sender: TObject);
  39.         procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  40.         procedure N2Click(Sender: TObject);
  41.     private
  42.         { Private declarations }
  43.     public
  44.         { Public declarations }
  45.     end;
  46.  
  47.     TPoint = record
  48.         X, Y: Real;
  49.     end;
  50.  
  51.     TLine = record
  52.         K, B: Real;
  53.     end;
  54.  
  55. var
  56.     Form1: TForm1;
  57.     Points: TArray<TPoint>;
  58.     LineWithMostPoints: TLine;
  59.     IsFileOpen: Boolean;
  60.     Path: String;
  61.  
  62. implementation
  63.  
  64. {$R *.dfm}
  65.  
  66. function FindLineWithMostPoints(const Points: TArray<TPoint>): TLine;
  67. var
  68.     ResultLine: TLine;
  69.     MaxPointsCount, PointsOnLine, I, J, P: Integer;
  70.     K, B: Real;
  71. begin
  72.     ResultLine.K := 0;
  73.     ResultLine.B := 0;
  74.     MaxPointsCount := 0;
  75.  
  76.     for I := 0 to High(Points) do
  77.     begin
  78.         for J := I + 1 to High(Points) do
  79.         begin
  80.  
  81.             PointsOnLine := 2; // Две уже минимум есть
  82.  
  83.             if (Points[J].X - Points[I].X) <> 0 then
  84.                 K := (Points[J].Y - Points[I].Y) / (Points[J].X - Points[I].X)
  85.             Else
  86.                 Dec(PointsOnLine);
  87.  
  88.             B := Points[I].Y - K * Points[I].X;
  89.  
  90.             for P := 0 to High(Points) do
  91.             begin
  92.                 if (P <> I) and (P <> J) and
  93.                   (Abs(Points[P].Y - (K * Points[P].X + B)) < 0.0001) then
  94.                     Inc(PointsOnLine);
  95.             end;
  96.  
  97.             if PointsOnLine > MaxPointsCount then
  98.             begin
  99.                 MaxPointsCount := PointsOnLine;
  100.                 ResultLine.K := K;
  101.                 ResultLine.B := B;
  102.             end;
  103.         end;
  104.     end;
  105.  
  106.     Result := ResultLine;
  107. end;
  108.  
  109. procedure TForm1.ButtonSolveClick(Sender: TObject);
  110. var
  111.     Size, I: Integer;
  112. begin
  113.     Size := StrToInt(EditAmount.Text);
  114.     SetLength(Points, Size);
  115.     for I := 0 to Size - 1 do
  116.     Begin
  117.         Points[I].X := StrToFloat(StringGridPoints.Cells[I + 1, 1]);
  118.         Points[I].Y := StrToFloat(StringGridPoints.Cells[I + 1, 2]);
  119.     End;
  120.     LineWithMostPoints := FindLineWithMostPoints(Points);
  121.     N6.Enabled := True;
  122.     Memo1.Text := 'Уравнение прямой с большинством точек: y = ' +
  123.       FloatToStr(LineWithMostPoints.K) + 'x + ' +
  124.       FloatToStr(LineWithMostPoints.B);
  125. end;
  126.  
  127. procedure TForm1.ButtonPointsClick(Sender: TObject);
  128. var
  129.     I: Integer;
  130. begin
  131.     StringGridPoints.Enabled := True;
  132.     StringGridPoints.Rows[1].Clear;
  133.     StringGridPoints.Rows[2].Clear;
  134.     StringGridPoints.Cells[0, 1] := 'X';
  135.     StringGridPoints.Cells[0, 2] := 'Y';
  136.  
  137.     StringGridPoints.ColCount := StrToInt(EditAmount.Text) + 1;
  138.     For I := 1 to StringGridPoints.ColCount do
  139.         StringGridPoints.Cells[I, 0] := IntToStr(I);
  140.  
  141.     Memo1.Clear;
  142.     ButtonSolve.Enabled := False;
  143.     N6.Enabled := False;
  144. end;
  145.  
  146. procedure TForm1.EditAmountChange(Sender: TObject);
  147. var
  148.     Num: Integer;
  149.     IsCorrect: Boolean;
  150. begin
  151.     IsCorrect := True;
  152.     try
  153.         Num := StrToInt(EditAmount.Text)
  154.     Except
  155.         IsCorrect := False;
  156.     end;
  157.     if IsCorrect And (Num > 1) And (Num <= 20) then
  158.         ButtonPoints.Enabled := True
  159.     Else
  160.     Begin
  161.         ButtonPoints.Enabled := False;
  162.         IsCorrect := False;
  163.     End;
  164.     StringGridPoints.Enabled := False;
  165.     ButtonSolve.Enabled := False;
  166.     Memo1.Clear;
  167.     N6.Enabled := False
  168. end;
  169.  
  170. procedure TForm1.EditAmountKeyPress(Sender: TObject; var Key: Char);
  171. begin
  172.     If not(Key in ['0' .. '9', #13, #8]) Then
  173.         Key := #0;
  174.     if (ButtonPoints.Enabled = True) And (Key = #13) then
  175.         ButtonPoints.Click
  176. end;
  177.  
  178. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  179. begin
  180.     CanClose := Application.MessageBox('Вы действительно хотите выйти?',
  181.       'Выход', MB_ICONQUESTION + MB_YESNO) = ID_YES;
  182. end;
  183.  
  184. procedure TForm1.FormCreate(Sender: TObject);
  185. begin
  186.     StringGridPoints.Cells[0, 0] := '№/№';
  187.     StringGridPoints.Cells[0, 1] := 'X';
  188.     StringGridPoints.Cells[0, 2] := 'Y';
  189. end;
  190.  
  191. procedure TForm1.N2Click(Sender: TObject);
  192. const
  193.     Info1 = 'На плоскости заданы n точек своими координатами. Найти уравнение'#13#10;
  194.     Info2 = 'прямой, которой принадлежит наибольшее число данных точек.'#13#10;
  195.     Info3 = '1. Минимальное количество точек - 2, максимальная - 20.'#13#10;
  196.     Info4 = '2. Диапазон ввода координат от 0 до 99.'#13#10;
  197. begin
  198.     Application.MessageBox(Info1 + Info2 + Info3 + Info4, 'Справка', 0);
  199. end;
  200.  
  201. procedure TForm1.N3Click(Sender: TObject);
  202. begin
  203.     Application.MessageBox('Сымоник Вадим, гр. 251004', 'О разработчике', 0)
  204. end;
  205.  
  206. Function GetSize(var FileInput: TextFile): String;
  207. Const
  208.     MIN_NUM = 2;
  209.     MAX_NUM = 20;
  210. Var
  211.     Size, Num: Integer;
  212.     Str: String;
  213.     IsCorrect: Boolean;
  214. Begin
  215.     Size := 0;
  216.     Str := '';
  217.     If Not Eof(FileInput) Then
  218.     Begin
  219.         IsCorrect := True;
  220.         Try
  221.             Read(FileInput, Size);
  222.         Except
  223.             MessageBox(Form1.Handle, PChar('Недопустимый размер массива!'),
  224.               'Ошибка', MB_ICONSTOP);
  225.             IsCorrect := False;
  226.             Size := 0;
  227.         End;
  228.     End
  229.     Else
  230.         MessageBox(Form1.Handle, PChar('Недостаточно данных в файле!'),
  231.           'Ошибка', MB_ICONSTOP);
  232.     If (Size >= MIN_NUM) And (Size <= MAX_NUM) Then
  233.         Str := IntToStr(Size)
  234.     Else
  235.         Application.MessageBox('Проверьте корректность данных в файле',
  236.           'Ошибка', 0);
  237.     GetSize := Str;
  238. End;
  239.  
  240. Function TakeInformationIntoCell(Var FileInput: TextFile;
  241.   Var IsCorrect: Boolean): String;
  242. Var
  243.     Temp: Integer;
  244.     Str: String;
  245. Begin
  246.     If Not Eof(FileInput) Then
  247.     Begin
  248.         Try
  249.             Read(FileInput, Temp);
  250.             Str := IntToStr(Temp);
  251.         Except
  252.             MessageBox(Form1.Handle, PChar('Неверные данные!'), 'Ошибка',
  253.               MB_ICONSTOP);
  254.             IsCorrect := False;
  255.         End;
  256.         If IsCorrect And (Temp < 0) And (Temp >= 100) Then
  257.         Begin
  258.             IsCorrect := False;
  259.             MessageBox(Form1.Handle,
  260.               PChar('Недопустимый диапазон входных данных!'), 'Ошибка',
  261.               MB_ICONSTOP);
  262.             Str := '';
  263.         End;
  264.     End
  265.     Else
  266.     Begin
  267.         IsCorrect := False;
  268.         MessageBox(Form1.Handle, PChar('Недостаточно значений в файле!'),
  269.           'Ошибка', MB_ICONSTOP);
  270.         Str := '';
  271.     End;
  272.     TakeInformationIntoCell := Str;
  273. End;
  274.  
  275. Procedure InputPointsInMatrix(var FileInput: TextFile);
  276. Var
  277.     I, J: Integer;
  278.     IsCorrect: Boolean;
  279. Begin
  280.     IsCorrect := True;
  281.     With Form1 do
  282.     Begin
  283.         For I := 1 to StringGridPoints.ColCount - 1 do
  284.         Begin
  285.             For J := 1 to StringGridPoints.RowCount - 1 do
  286.             Begin
  287.                 StringGridPoints.Cells[I, J] :=
  288.                   TakeInformationIntoCell(FileInput, IsCorrect);
  289.             End;
  290.         End;
  291.         ButtonSolve.Enabled := IsCorrect;
  292.     End;
  293. End;
  294.  
  295. procedure TForm1.N5Click(Sender: TObject);
  296. var
  297.     FileInput: TextFile;
  298. begin
  299.     If OpenDialog1.Execute Then
  300.     Begin
  301.         AssignFile(FileInput, OpenDialog1.FileName);
  302.         Try
  303.             Try
  304.                 Reset(FileInput);
  305.                 EditAmount.Text := GetSize(FileInput);
  306.                 if EditAmount.Text <> '' then
  307.                 Begin
  308.                     ButtonPoints.Click;
  309.                     InputPointsInMatrix(FileInput);
  310.                 End;
  311.             Finally
  312.                 CloseFile(FileInput);
  313.             End;
  314.         Except
  315.  
  316.         End;
  317.     End;
  318. end;
  319.  
  320. Function Open(): String;
  321. begin
  322.     with Form1 Do
  323.     begin
  324.         If SaveDialog1.Execute Then
  325.         begin
  326.             Path := SaveDialog1.FileName;
  327.             IsFileOpen := True;
  328.         end
  329.         Else
  330.             IsFileOpen := False;
  331.     end;
  332.     Open := Path;
  333. end;
  334.  
  335. procedure TForm1.N6Click(Sender: TObject);
  336. var
  337.     F: TextFile;
  338. begin
  339.     Path := Open;
  340.     If IsFileOpen Then
  341.     Begin
  342.         AssignFile(F, Path);
  343.         Rewrite(F);
  344.         Writeln(F, Memo1.Text);
  345.         Application.MessageBox('Данные успешно сохранены в файл',
  346.           'Результат', 0);
  347.         CloseFile(F);
  348.     End;
  349. end;
  350.  
  351. procedure TForm1.N8Click(Sender: TObject);
  352. begin
  353.     Form1.Close
  354. end;
  355.  
  356. procedure TForm1.StringGridPointsKeyPress(Sender: TObject; var Key: Char);
  357. begin
  358.     If not(Key in ['0' .. '9', #8, #13]) Then
  359.         Key := #0;
  360.     if (Key = #13) And (ButtonSolve.Enabled = True) then
  361.         ButtonSolve.Click
  362. end;
  363.  
  364. procedure TForm1.StringGridPointsSetEditText(Sender: TObject;
  365.   ACol, ARow: Integer; const Value: string);
  366.  
  367. var
  368.     I, J: Integer;
  369.     N: Integer;
  370.     IsCorrect: Boolean;
  371.  
  372. begin
  373.     For I := 1 to StringGridPoints.RowCount - 1 do
  374.     Begin
  375.         for J := 1 to StringGridPoints.ColCount - 1 do
  376.         Begin
  377.             IsCorrect := True;
  378.             If StringGridPoints.Cells[I, J] <> '' Then
  379.             Begin
  380.                 Try
  381.                     N := StrToInt(StringGridPoints.Cells[I, J]);
  382.                 Except
  383.                     StringGridPoints.Cells[I, J] := '';
  384.                     Application.MessageBox
  385.                       ('Проверьте корректность введенных данных', 'Ошибка', 0);
  386.                     IsCorrect := False;
  387.                 End;
  388.             End;
  389.  
  390.             If IsCorrect And (Length(StringGridPoints.Cells[I, J]) > 2) Then
  391.             Begin
  392.                 StringGridPoints.Cells[I, J] := '';
  393.                 Application.MessageBox('Диапазон ввода [0; 99]', 'Ошибка', 0);
  394.             End;
  395.         End;
  396.     End;
  397.  
  398.     For J := 1 to StringGridPoints.RowCount - 1 do
  399.         for I := 1 to StringGridPoints.ColCount - 1 do
  400.             If Length(StringGridPoints.Cells[I, J]) = 0 Then
  401.                 IsCorrect := False;
  402.     ButtonSolve.Enabled := IsCorrect;
  403.     Memo1.Clear;
  404.     N6.Enabled := False
  405. end;
  406.  
  407. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement