Advertisement
gguuppyy

lab51main

Apr 5th, 2024
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.55 KB | None | 0 0
  1. Unit MainUnit;
  2.  
  3. Interface
  4.  
  5. Uses
  6.     Winapi.Windows,
  7.     Winapi.Messages,
  8.     System.SysUtils,
  9.     System.Classes,
  10.     Vcl.Forms,
  11.     Vcl.ExtDlgs,
  12.     Vcl.Menus,
  13.     Vcl.StdCtrls,
  14.     Vcl.Grids,
  15.     Vcl.Dialogs,
  16.     Vcl.Controls,
  17.     ManualUnit,
  18.     DevInfUnit,
  19.     AddUnit,
  20.     BackUnit,
  21.     LinkedListUnit;
  22.  
  23. Type
  24.     TMainForm = Class(TForm)
  25.         MainMenu: TMainMenu;
  26.         FileButtonMenu: TMenuItem;
  27.         ExitButtonMenu: TMenuItem;
  28.         ManualButtonMenu: TMenuItem;
  29.         DeveloperButtonMenu: TMenuItem;
  30.         PopupMenu: TPopupMenu;
  31.         SaveDialog1: TSaveTextFileDialog;
  32.         CheckButton: TButton;
  33.         InfLabel: TLabel;
  34.         FirstListLabel: TLabel;
  35.         SecondListLabel: TLabel;
  36.         SecondListGrid: TStringGrid;
  37.         AddFirstButton: TButton;
  38.         AddSecondButton: TButton;
  39.         SaveButtonMenu: TMenuItem;
  40.         FirstListGrid: TStringGrid;
  41.         MergedListGrid: TStringGrid;
  42.         OpenFileButton: TMenuItem;
  43.         Opendialog1: TOpenTextFileDialog;
  44.         Procedure ManualButtonMenuClick(Sender: TObject);
  45.         Procedure DeveloperButtonMenuClick(Sender: TObject);
  46.         Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  47.         Procedure ExitButtonMenuClick(Sender: TObject);
  48.         Procedure FormCreate(Sender: TObject);
  49.         Procedure SaveButtonMenuClick(Sender: TObject);
  50.         Procedure AddFirstButtonClick(Sender: TObject);
  51.         Procedure AddSecondButtonClick(Sender: TObject);
  52.         Procedure CheckButtonClick(Sender: TObject);
  53.         Function FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  54.         Procedure OpenFileButtonClick(Sender: TObject);
  55.     Private
  56.         { Private declarations }
  57.     Public
  58.         { Public declarations }
  59.     End;
  60.  
  61. Procedure Add(HeadPt: ListPointer; Elem: String); Stdcall; External 'ListLib.dll';
  62. Function Len(HeadPt: ListPointer): Integer; Stdcall; External 'ListLib.dll';
  63. Procedure Merge(Dest, First, Second: ListPointer); Stdcall; External 'ListLib.dll';
  64.  
  65. Type
  66.     TGridCracker = Class(TStringGrid);
  67.  
  68. Var
  69.     MainForm: TMainForm;
  70.     IsSaved: Boolean = True;
  71.     FirstHead, SecondHead: ListPointer;
  72.  
  73. Implementation
  74.  
  75. {$R *.dfm}
  76.  
  77. Procedure FillGrid(HeadPt: ListPointer; Grid: TStringGrid);
  78. Var
  79.     I, Size: Integer;
  80. Begin
  81.     Grid.Visible := True;
  82.     Size := Len(HeadPt);
  83.     If Size > 5 Then
  84.     Begin
  85.         Grid.Width := (Grid.DefaultColWidth + 3) * 5;
  86.         Grid.Height := (Grid.DefaultRowHeight + 3) * 2 + 25;
  87.     End
  88.     Else
  89.     Begin
  90.         Grid.Width := (Grid.DefaultColWidth + 4) * Size;
  91.         Grid.Height := (Grid.DefaultRowHeight + 3) * 2;
  92.     End;
  93.     Grid.ColCount := Size;
  94.     Grid.RowCount := 2;
  95.     For I := 0 To Size - 1 Do
  96.     Begin
  97.         HeadPt := HeadPt^.Next;
  98.         Grid.Cells[I, 0] := IntToStr(I + 1);
  99.         Grid.Cells[I, 1] := IntToStr(HeadPt^.Data);
  100.     End;
  101. End;
  102.  
  103. Procedure ClearGrid(Grid: TStringGrid);
  104. Var
  105.     J, I: Integer;
  106. Begin
  107.     Grid.Visible := False;
  108.     For I := 0 To Grid.ColCount - 1 Do
  109.         For J := 0 To Grid.RowCount - 1 Do
  110.             Grid.Cells[I, J] := '';
  111. End;
  112.  
  113. Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  114. Begin
  115.     If SaveButtonMenu.Enabled And Not IsSaved Then
  116.         Case Application.MessageBox('Сохранить данные перед выходом?', 'Выход', MB_YESNOCANCEL + MB_ICONQUESTION + MB_DEFBUTTON3) Of
  117.             IDYES:
  118.                 Begin
  119.                     SaveButtonMenu.Click;
  120.                     CanClose := True;
  121.                 End;
  122.             IDNO:
  123.                 CanClose := True;
  124.             IDCANCEL:
  125.                 CanClose := False;
  126.         End
  127.     Else
  128.         Case Application.MessageBox('Вы точно хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2) Of
  129.             IDYES:
  130.                 CanClose := True;
  131.             IDNO:
  132.                 CanClose := False;
  133.         End;
  134. End;
  135.  
  136. Procedure TMainForm.FormCreate(Sender: TObject);
  137. Begin
  138.     New(FirstHead);
  139.     FirstHead^.Next := Nil;
  140.     New(SecondHead);
  141.     SecondHead^.Next := Nil;
  142. End;
  143.  
  144. Function TMainForm.FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  145. Begin
  146.     ManualButtonMenu.Click();
  147.     CallHelp := False;
  148. End;
  149.  
  150. Function ReadOneFromFile(Var Numb: Integer; Var MyFile: TextFile; IsElemRead: Boolean = True): ERRORS_CODE;
  151. Var
  152.     Err: ERRORS_CODE;
  153.     NumbInt: Integer;
  154.     NumbStr: String;
  155. Begin
  156.     Err := SUCCESS;
  157.     NumbInt := 0;
  158.     Try
  159.         Read(MyFile, NumbInt);
  160.     Except
  161.         Err := INCORRECT_DATA_FILE;
  162.     End;
  163.     If Err = SUCCESS Then
  164.         If IsElemRead Then
  165.             If (NumbInt > MAX_NUMB) Or (NumbInt < MIN_NUMB) Then
  166.                 Err := OUT_OF_BORDER
  167.             Else
  168.                 Numb := NumbInt
  169.         Else
  170.             If (NumbInt > MAX_SIZE) Or (NumbInt < MIN_SIZE) Then
  171.                 Err := OUT_OF_BORDER_SIZE
  172.             Else
  173.                 Numb := NumbInt;
  174.     ReadOneFromFile := Err;
  175. End;
  176.  
  177. Procedure TMainForm.SaveButtonMenuClick(Sender: TObject);
  178. Var
  179.     OutFile: TextFile;
  180.     I, J: Integer;
  181. Begin
  182.     If SaveDialog1.Execute() Then
  183.     Begin
  184.         AssignFile(OutFile, SaveDialog1.FileName);
  185.         Rewrite(OutFile);
  186.  
  187.         Writeln(OutFile, 'Итоговый список');
  188.         For I := 0 To MergedListGrid.ColCount - 1 Do
  189.             Write(OutFile, MergedListGrid.Cells[I, 1] + ' ');
  190.  
  191.         CloseFile(OutFile);
  192.         IsSaved := True;
  193.     End;
  194. End;
  195.  
  196. Procedure TMainForm.ManualButtonMenuClick(Sender: TObject);
  197. Var
  198.     ManualForm: TManualForm;
  199. Begin
  200.     ManualForm := TManualForm.Create(Self);
  201.     ManualForm.ShowModal;
  202.     ManualForm.Free;
  203. End;
  204.  
  205. Procedure TMainForm.OpenFileButtonClick(Sender: TObject);
  206. CONST
  207.     FIRST = 1;
  208.     SECOND = 2;
  209. Var
  210.     FileName: String;
  211.     FileStatus: TFileStatus;
  212.     Size: Integer;
  213. Begin
  214.     If OpenDialog1.Execute() Then
  215.     Begin
  216.         FileName := OpenDialog1.FileName;
  217.         FileStatus := CheckFile(FileName);
  218.         If FileStatus = FsGood Then
  219.         Begin
  220.             Size := ReadSize(Filename, FIRST, FileStatus);
  221.             If FileStatus = FsGood Then
  222.                 FirstHead := ReadList(FileName, Size, FIRST, FileStatus)
  223.             Else
  224.                 MessageBox(Self.Handle, ListOfMessages[FileStatus], 'Ошибка', MB_ICONERROR);
  225.         End
  226.         Else
  227.             MessageBox(Self.Handle, ListOfMessages[FileStatus], 'Ошибка', MB_ICONERROR);
  228.         If FileStatus = FsGood Then
  229.         Begin
  230.             Size := ReadSize(Filename, SECOND, FileStatus);
  231.             If FileStatus = FsGood Then
  232.                 SecondHead := ReadList(FileName, Size, SECOND, FileStatus)
  233.             Else
  234.                 MessageBox(Self.Handle, ListOfMessages[FileStatus], 'Ошибка', MB_ICONERROR);
  235.         End
  236.         Else
  237.             MessageBox(Self.Handle, ListOfMessages[FileStatus], 'Ошибка', MB_ICONERROR);
  238.         If FileStatus = FsGood Then
  239.         Begin
  240.             FillGrid(FirstHead, FirstListGrid);
  241.             ClearGrid(MergedListGrid);
  242.             FillGrid(SecondHead, SecondListGrid);
  243.             CheckButton.Enabled := True;
  244.         End;
  245.     End;
  246.  
  247. End;
  248.  
  249. Procedure TMainForm.AddFirstButtonClick(Sender: TObject);
  250. Var
  251.     AddForm: TAddForm;
  252.     Res: TModalResult;
  253.     NewElem: Integer;
  254. Begin
  255.     AddForm := TAddForm.Create(Self);
  256.     AddForm.FormCreate(FirstHead, Self);
  257.     Res := AddForm.ShowModal();
  258.     If Res = MrOk Then
  259.     Begin
  260.         FillGrid(FirstHead, FirstListGrid);
  261.         ClearGrid(MergedListGrid);
  262.     End;
  263.     AddForm.Free();
  264.     If FirstListGrid.Visible And SecondListGrid.Visible Then
  265.         CheckButton.Enabled := True;
  266. End;
  267.  
  268. Procedure TMainForm.AddSecondButtonClick(Sender: TObject);
  269. Var
  270.     AddForm: TAddForm;
  271.     Res: TModalResult;
  272.     NewElem: Integer;
  273. Begin
  274.     AddForm := TAddForm.Create(Self);
  275.     AddForm.FormCreate(SecondHead, Self);
  276.     Res := AddForm.ShowModal();
  277.     If Res = MrOk Then
  278.         FillGrid(SecondHead, SecondListGrid);
  279.     AddForm.Free();
  280.     If FirstListGrid.Visible And SecondListGrid.Visible Then
  281.         CheckButton.Enabled := True;
  282. End;
  283.  
  284. Procedure TMainForm.CheckButtonClick(Sender: TObject);
  285. Var
  286.     MergedHead: ListPointer;
  287. Begin
  288.     New(MergedHead);
  289.     Merge(MergedHead, FirstHead, SecondHead);
  290.     FillGrid(MergedHead, MergedListGrid);
  291.     SaveButtonMenu.Enabled := True;
  292.     IsSaved := False;
  293. End;
  294.  
  295. Procedure TMainForm.DeveloperButtonMenuClick(Sender: TObject);
  296. Var
  297.     DeveloperForm: TDeveloperForm;
  298. Begin
  299.     DeveloperForm := TDeveloperForm.Create(Self);
  300.     DeveloperForm.ShowModal;
  301.     DeveloperForm.Free;
  302. End;
  303.  
  304. Procedure TMainForm.ExitButtonMenuClick(Sender: TObject);
  305. Begin
  306.     Close();
  307. End;
  308.  
  309. Function IsAllCellFill(Grid: TStringGrid; Key: Char; CurCell: TInplaceEdit): Boolean;
  310. Var
  311.     IsFilled: Boolean;
  312.     I, J: Integer;
  313. Begin
  314.     IsFilled := True;
  315.     For I := 1 To Grid.ColCount - 1 Do
  316.         For J := 1 To Grid.RowCount - 1 Do
  317.         Begin
  318.             If (Grid.Col = I) And (Grid.Row = J) And Not(Key = VOID) Then
  319.             Begin
  320.                 If (Grid.Cells[I, J] = '') And Not CharInSet(Key, DIGITS) Then
  321.                     IsFilled := False;
  322.                 With CurCell Do
  323.                     If (Key = BACKSPACE) And (InsertKey(SelStart, Key, SelLength, Text) = '') Then
  324.                         IsFilled := False;
  325.             End
  326.             Else
  327.                 If (Grid.Cells[I, J] = '') Or (Grid.Cells[I, J] = '-') Then
  328.                     IsFilled := False;
  329.         End;
  330.     IsAllCellFill := IsFilled;
  331. End;
  332.  
  333. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement