Advertisement
MaksNew

Untitled

Feb 2nd, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 17.67 KB | None | 0 0
  1. unit Unit1;
  2. Type
  3.     InputType = (FromConsole, FromFile);
  4.     Worker = Record
  5.         Name: String[15];
  6.         DetailsOnMonday: Integer;
  7.         DetailsOnTuesday: Integer;
  8.         DetailsOnWednesday: Integer;
  9.         DetailsOnThursday: Integer;
  10.         DetailsOnFriday: Integer;
  11.         DetailsOnSaturday: Integer;
  12.     End;
  13.     AoW = Array of Worker;
  14.  
  15. var
  16.   MainForm: TMainForm;
  17.   ACol, ADeletedRow: Integer;
  18.   GlobalCorrect: Boolean;
  19.  
  20. function IsMainStringGridCorrect(StringGrid: TStringGrid; SpinEdit: TSpinEdit):boolean;
  21. var
  22.     I, J: Integer;                                             //Проверка таблицы на корректность данных
  23.     IsCorrect: Boolean;
  24. begin
  25.     IsCorrect := True;
  26.     I := 1;
  27.     while ((I <= SpinEdit.Value) and (IsCorrect)) do
  28.     begin
  29.         J := 2;
  30.         while ((J < 8) and (IsCorrect)) do
  31.         Begin
  32.             Try
  33.                 StrToInt(StringGrid.Cells[J, I])
  34.             Except
  35.                 IsCorrect := False;
  36.             End;
  37.             Inc(J);
  38.         End;
  39.         Inc(I);
  40.     end;
  41.     IsMainStringGridCorrect := IsCorrect;
  42. end;
  43.  
  44. procedure TMainForm.FindDialog1Find(Sender: TObject); //Вывод фамилии сборщика и общего количества деталей, собранного им за неделю
  45. var
  46.     I, J, Sum: Integer;
  47.     IsFound: Boolean;
  48. begin
  49.     Sum := 0;
  50.     IsFound := False;
  51.     for I := 1 to SpinEditOfWorkers.Value do
  52.     if MainStringGrid.Cells[1, i] = FindDialog1.FindText then
  53.         begin
  54.             for J := 2 to 7 do
  55.                 Sum := Sum + StrToInt(MainStringGrid.Cells[J, I]);
  56.             IsFound := True;
  57.             ShowMessage(FindDialog1.FindText + ' за шестидневную неделю в сумме сделал ' + IntToStr(Sum) + ' деталей');
  58.         end;
  59.     if not(IsFound) then
  60.         ShowMessage(FindDialog1.FindText + ' не найден в списке!');
  61. end;
  62.  
  63. function IsFileCorOfChangingCorrect(var FileCor: TextFile): boolean;
  64. var
  65.     I, Index, Details: Integer;
  66.     Name: AnsiString;
  67.     IsCorrect: Boolean;
  68.     RussianChars: set of ansichar;       //Проверка файла корректур с даннымии для изменения ячеек
  69. begin
  70.     IsCorrect := True;
  71.     Details := 0;
  72.     Reset(FileCor);
  73.     RussianChars := ['A'..'Я', 'a'..'я'];
  74.     while not(EoF(FileCor)) do
  75.     begin
  76.         Try
  77.             Readln(FileCor, Index);
  78.         Except
  79.             IsCorrect := False;
  80.         End;
  81.         Readln(FileCor, Name);
  82.         I := 1;
  83.         while ((I <= Length(Name)) and (IsCorrect)) do
  84.         begin
  85.             if not(Name[I] in RussianChars) then
  86.                 IsCorrect := False;
  87.             Inc(I)
  88.         end;
  89.         I := 1;
  90.         while not(eoln(FileCor)) and IsCorrect do
  91.         begin
  92.             Try
  93.                 Read(FileCor, Details);
  94.             Except
  95.                 IsCorrect := False;
  96.             End;
  97.             Inc(I)
  98.         end;
  99.         Readln(FileCor);
  100.     end;
  101.     CloseFile(FileCor);
  102.     IsFileCorOfChangingCorrect := IsCorrect;
  103. end;
  104.  
  105. procedure TMainForm.ChangeButtonClick(Sender: TObject);  //Изменение таблицы с помощью файла корректур
  106. var
  107.     FileCor:TextFile;
  108.     I, J, Index: Integer;
  109.     Name: AnsiString;
  110.     Details: array[1..6] of integer;
  111. begin
  112.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  113.     begin
  114.         if OpenDialogForTextFile.Execute then
  115.         begin
  116.             AssignFile(FileCor, OpenDialogForTextFile.FileName);
  117.             if IsFileCorOfChangingCorrect(FileCor) then
  118.             begin
  119.                 Reset(FileCor);
  120.                 while not(EoF(FileCor)) do
  121.                 begin
  122.                     Readln(FileCor, Index);
  123.                     Readln(FileCor, Name);
  124.                     I := 1;
  125.                     while not(eoln(FileCor)) do
  126.                     begin
  127.                         Read(FileCor, Details[I]);
  128.                         Inc(I)
  129.                     end;
  130.                     for I := 1 to SpinEditOfWorkers.Value do
  131.                         if I = Index then
  132.                         begin
  133.                             MainStringGrid.Cells[1, I] := Name;
  134.                             for J := 2 to 7 do
  135.                                 MainStringGrid.Cells[J, I] := IntToStr(Details[J-1]);
  136.                         end;
  137.                     Readln(FileCor);
  138.                 end;
  139.                 CloseFile(FileCor)
  140.             end
  141.             else
  142.                 ShowMessage('Проверьте содержимое файла!');
  143.         end
  144.     end
  145.     else
  146.         ShowMessage('Проверьте таблицу!');
  147. end;
  148.  
  149. function IsFileCorOfAddingCorrect(var FileCor: TextFile): boolean; //Проверка файла корректур с данными для добавления ячеек
  150. var
  151.     I, Details: Integer;
  152.     Name: AnsiString;
  153.     IsCorrect: Boolean;
  154.     RussianChars: set of ansichar;
  155. begin
  156.     IsCorrect := True;
  157.     Details := 0;
  158.     Reset(FileCor);
  159.     RussianChars := ['A'..'Я', 'a'..'я'];
  160.     while not(EoF(FileCor)) do
  161.     begin
  162.         Readln(FileCor, Name);
  163.         I := 1;
  164.         while ((I <= Length(Name)) and (IsCorrect)) do
  165.         begin
  166.             if not(Name[I] in RussianChars) then
  167.                 IsCorrect := False;
  168.             Inc(I)
  169.         end;
  170.         I := 1;
  171.         while not(eoln(FileCor)) and IsCorrect do
  172.         begin
  173.             Try
  174.                 Read(FileCor, Details);
  175.             Except
  176.                 IsCorrect := False;
  177.             End;
  178.             Inc(I)
  179.         end;
  180.         Readln(FileCor);
  181.     end;
  182.     CloseFile(FileCor);
  183.     IsFileCorOfAddingCorrect := IsCorrect;
  184. end;
  185.  
  186. procedure TMainForm.AddButtonClick(Sender: TObject); //Добавление ячеек таблицы при помощи файла корректур
  187. var
  188.     FileCor:TextFile;
  189.     I, J: Integer;
  190.     Name: AnsiString;
  191.     Deatils: array[1..6] of integer;
  192. begin
  193.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  194.     begin
  195.         if OpenDialogForTextFile.Execute then
  196.         begin
  197.             AssignFile(FileCor, OpenDialogForTextFile.FileName);
  198.             if IsFileCorOfAddingCorrect(FileCor) then
  199.             begin
  200.                 Reset(FileCor);
  201.                 while not(EoF(FileCor)) do
  202.                 begin
  203.                     Readln(FileCor, Name);
  204.                     I := 1;
  205.                     while not(EoLn(FileCor)) do
  206.                     begin
  207.                         Read(FileCor, Deatils[I]);
  208.                         Inc(I)
  209.                     end;
  210.                     SpinEditOfWorkers.Value := SpinEditOfWorkers.Value + 1;
  211.                     MainStringGrid.Cells[1, SpinEditOfWorkers.Value] := Name;
  212.                     for J := 2 to 7 do
  213.                         MainStringGrid.Cells[J, SpinEditOfWorkers.Value] := IntToStr(Deatils[J-1]);
  214.                     Readln(FileCor);
  215.                 end;
  216.                 CloseFile(FileCor);
  217.             end
  218.             else
  219.                 ShowMessage('Проверьте содержимое файла!');
  220.         end;
  221.     end
  222.     else
  223.         ShowMessage('Проверьте таблицу');
  224. end;
  225.  
  226. function IsFileCorOfDeleteCorrect(var FileCor: TextFile; MaxValue: Integer): boolean;
  227. var
  228.     I, DeleteIndex: Integer;
  229.     Name: AnsiString;
  230.     IsCorrect: Boolean;      //Проверка файла корректур с данными для удаления ячеек
  231. begin
  232.     IsCorrect := True;
  233.     DeleteIndex := 0;
  234.     I := 1;
  235.     Reset(FileCor);
  236.     while not(EoF(FileCor)) do
  237.     begin
  238.         Try
  239.             Readln(FileCor, DeleteIndex);
  240.         Except
  241.             IsCorrect := False;
  242.         End;
  243.         if IsCorrect then
  244.             if DeleteIndex > MaxValue then
  245.                 IsCorrect := False;
  246.         Inc(I);
  247.     end;
  248.     CloseFile(FileCor);
  249.     if I > MaxValue then
  250.         IsCorrect := False;
  251.     IsFileCorOfDeleteCorrect := IsCorrect;
  252. end;
  253.  
  254. procedure DeleteRow(var ArrayOfRecords: AoW; I, DeletedRow: Integer; var MainStringGrid: TStringGrid; SpinEditOfWorkers: TSpinEdit);
  255. begin
  256.     while (I < DeletedRow) do
  257.     begin
  258.         ArrayOfRecords[I-1].Name := MainStringGrid.Cells[1, I];
  259.         ArrayOfRecords[I-1].DetailsOnMonday := StrToInt(MainStringGrid.Cells[2, I]);
  260.         ArrayOfRecords[I-1].DetailsOnTuesday := StrToInt(MainStringGrid.Cells[3, I]);
  261.         ArrayOfRecords[I-1].DetailsOnWednesday := StrToInt(MainStringGrid.Cells[4, I]);
  262.         ArrayOfRecords[I-1].DetailsOnThursday := StrToInt(MainStringGrid.Cells[5, I]);
  263.         ArrayOfRecords[I-1].DetailsOnFriday := StrToInt(MainStringGrid.Cells[6, I]);
  264.         ArrayOfRecords[I-1].DetailsOnSaturday := StrToInt(MainStringGrid.Cells[7, I]);
  265.         Inc(I);
  266.     end;
  267.  
  268.     I := DeletedRow + 1;
  269.  
  270.     while (I <= SpinEditOfWorkers.Value) do
  271.     begin
  272.         ArrayOfRecords[I-2].Name := MainStringGrid.Cells[1, I];
  273.         ArrayOfRecords[I-2].DetailsOnMonday := StrToInt(MainStringGrid.Cells[2, I]);
  274.         ArrayOfRecords[I-2].DetailsOnTuesday := StrToInt(MainStringGrid.Cells[3, I]);
  275.         ArrayOfRecords[I-2].DetailsOnWednesday := StrToInt(MainStringGrid.Cells[4, I]);
  276.         ArrayOfRecords[I-2].DetailsOnThursday := StrToInt(MainStringGrid.Cells[5, I]);
  277.         ArrayOfRecords[I-2].DetailsOnFriday := StrToInt(MainStringGrid.Cells[6, I]);
  278.         ArrayOfRecords[I-2].DetailsOnSaturday := StrToInt(MainStringGrid.Cells[7, I]);
  279.         Inc(I);
  280.     end;
  281.  
  282.     for I := 1 to 7 do
  283.         MainStringGrid.Cells[I, SpinEditOfWorkers.Value] := ' ';
  284.  
  285.     SpinEditOfWorkers.Value := SpinEditOfWorkers.Value - 1;
  286.     for I := 1 to SpinEditOfWorkers.Value do
  287.     begin
  288.         MainStringGrid.Cells[1, I] := ArrayOfRecords[I-1].Name;
  289.         MainStringGrid.Cells[2, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnMonday);
  290.         MainStringGrid.Cells[3, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnTuesday);
  291.         MainStringGrid.Cells[4, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnWednesday);
  292.         MainStringGrid.Cells[5, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnThursday);
  293.         MainStringGrid.Cells[6, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnFriday);
  294.         MainStringGrid.Cells[7, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnSaturday);
  295.     end;
  296. end;
  297.  
  298. procedure TMainForm.DeleteFromFileButtonClick(Sender: TObject); //Удаление ячеек таблицы при помощи файла корректур
  299. var
  300.     FileCor:TextFile;
  301.     DeletedRow, I, Buf: Integer;
  302.     IsCorrect: Boolean;
  303.     ArrayOfRecords: AoW;
  304. begin
  305.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  306.     begin
  307.         if OpenDialogForTextFile.Execute then
  308.         begin
  309.             AssignFile(FileCor, OpenDialogForTextFile.FileName);
  310.             if IsFileCorOfDeleteCorrect(FileCor, SpinEditOfWorkers.Value) then
  311.             begin
  312.                 Reset(FileCor);
  313.                 SetLength(ArrayOfRecords, SpinEditOfWorkers.Value-1);
  314.                 I := 1;
  315.                 Buf := 0;
  316.                 DeletedRow := 0;
  317.                 While not(EoF(FileCor)) do
  318.                 begin
  319.                     Readln(FileCor, DeletedRow);
  320.                     if Buf > 0 then
  321.                         DeletedRow := DeletedRow-Buf;
  322.                     DeleteRow(ArrayOfRecords, I, DeletedRow, MainStringGrid, SpinEditOfWorkers);
  323.                     Inc(Buf);
  324.                 end;
  325.                 CloseFile(FileCor);
  326.             end
  327.             else
  328.                 ShowMessage('Проверьте содержимое файла!');
  329.         end;
  330.     end
  331.     else
  332.         ShowMessage('Проверьте таблицу!');
  333. end;
  334.  
  335. procedure TMainForm.OpenFileButtonClick(Sender: TObject); //Открыть таблицу из файла
  336. var
  337.     OpenedFile: File of Worker;
  338.     I, J: Integer;
  339.     IsCorrect: Boolean;
  340.     ArrayOfRecords: AoW;
  341. begin
  342.     if OpenDialogForStringGrid.Execute then
  343.     begin
  344.         for I := 1 to SpinEditOfWorkers.Value do
  345.             for J := 1 to 7 do
  346.                 MainStringGrid.Cells[J, I] := '';
  347.         AssignFile(OpenedFile, OpenDialogForStringGrid.FileName);
  348.         Reset(OpenedFile);
  349.         SpinEditOfWorkers.Value := FileSize(OpenedFile);
  350.         for I := 1 to SpinEditOfWorkers.Value do
  351.             MainStringGrid.Cells[0, I] := IntToStr(I);
  352.         SetLength(ArrayOfRecords, SpinEditOfWorkers.Value);
  353.         I := 0;
  354.         While not(EoF(OpenedFile)) do
  355.         Begin
  356.             Read(OpenedFile, ArrayOfRecords[I]);
  357.             Inc(I);
  358.         End;
  359.  
  360.         for I := 1 to SpinEditOfWorkers.Value do
  361.         begin
  362.             MainStringGrid.Cells[1, I] := ArrayOfRecords[I-1].Name;
  363.             MainStringGrid.Cells[2, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnMonday);
  364.             MainStringGrid.Cells[3, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnTuesday);
  365.             MainStringGrid.Cells[4, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnWednesday);
  366.             MainStringGrid.Cells[5, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnThursday);
  367.             MainStringGrid.Cells[6, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnFriday);
  368.             MainStringGrid.Cells[7, I] := IntToStr(ArrayOfRecords[I-1].DetailsOnSaturday);
  369.         end;
  370.         CloseFile(OpenedFile);
  371.     end;
  372. end;
  373.  
  374. procedure TMainForm.SaveButtonClick(Sender: TObject);   //Сохранить файл
  375. var
  376.     I: Integer;
  377.     SavedFile: File of Worker;
  378.     ArrayOfRecords: AoW;
  379. begin
  380.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  381.     begin
  382.         if SaveDialog.Execute then
  383.         begin
  384.             AssignFile(SavedFile, SaveDialog.FileName);
  385.             Rewrite(SavedFile);
  386.             SetLength(ArrayOfRecords, SpinEditOfWorkers.Value);
  387.             for I := 1 to SpinEditOfWorkers.Value do
  388.             begin
  389.                 ArrayOfRecords[I-1].Name := MainStringGrid.Cells[1, I];
  390.                 ArrayOfRecords[I-1].DetailsOnMonday := StrToInt(MainStringGrid.Cells[2, I]);
  391.                 ArrayOfRecords[I-1].DetailsOnTuesday := StrToInt(MainStringGrid.Cells[3, I]);
  392.                 ArrayOfRecords[I-1].DetailsOnWednesday := StrToInt(MainStringGrid.Cells[4, I]);
  393.                 ArrayOfRecords[I-1].DetailsOnThursday := StrToInt(MainStringGrid.Cells[5, I]);
  394.                 ArrayOfRecords[I-1].DetailsOnFriday := StrToInt(MainStringGrid.Cells[6, I]);
  395.                 ArrayOfRecords[I-1].DetailsOnSaturday := StrToInt(MainStringGrid.Cells[7, I]);
  396.             end;
  397.  
  398.             for I := 0 to SpinEditOfWorkers.Value-1 do
  399.                 Write(SavedFile, ArrayOfRecords[I]);
  400.             CloseFile(SavedFile);
  401.         end;
  402.     end
  403.     else
  404.         ShowMessage('Проверьте таблицу!');
  405. end;
  406.  
  407. procedure TMainForm.WorkerAndDetailsOnWeekFindButtonClick(Sender: TObject);  //Сборщик и детали за неделю
  408. begin
  409.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  410.         FindDialog1.Execute()
  411.     else
  412.         ShowMessage('Проверьте таблицу!');
  413. end;
  414.  
  415. procedure TMainForm.BestWorkerFindButtonClick(Sender: TObject);  //Сборщик-рекордсмен
  416. var
  417.     ArrayOfSumOfProducts: array of integer;
  418.     BestWorkman, BestDay: AnsiString;
  419.     I, J, SumOfDetails, MaxDetails, BestBuf: Integer;
  420. begin
  421.     if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  422.     begin
  423.         SetLength(ArrayOfSumOfProducts, SpinEditOfWorkers.Value);
  424.         MaxDetails := 0;
  425.         BestBuf := 0;
  426.         for I := 0 to SpinEditOfWorkers.Value-1 do //находит сумму всех деталей каждого рабочего
  427.         begin
  428.             SumOfDetails := 0;
  429.             for J := 2 to 7 do
  430.             begin
  431.                 SumOfDetails := SumOfDetails + StrToInt(MainStringGrid.Cells[J,I+1]);
  432.             end;
  433.             ArrayOfSumOfProducts[I] := SumOfDetails;
  434.         end;
  435.  
  436.         for I := 0 to SpinEditOfWorkers.Value-1 do   // ищет максимум деталей среди рабочих и день высшей производительности
  437.         begin
  438.             if ((ArrayOfSumOfProducts[I] > ArrayOfSumOfProducts[I-1]) and (ArrayOfSumOfProducts[I] > MaxDetails)) then
  439.             begin
  440.                 MaxDetails := ArrayOfSumOfProducts[I];
  441.                 BestWorkman := MainStringGrid.Cells[1, I+1];
  442.                 for J := 2 to 6 do
  443.                     if ((StrToInt(MainStringGrid.Cells[J, I+1]) > StrToInt(MainStringGrid.Cells[J+1,I+1])) and (StrToInt(MainStringGrid.Cells[J, I+1]) > BestBuf)) then
  444.                     begin
  445.                         BestDay := MainStringGrid.Cells[J,0];
  446.                         BestBuf := StrToInt(MainStringGrid.Cells[J, I+1])
  447.                     end;
  448.             end;
  449.         end;
  450.  
  451.         if MaxDetails > 0 then
  452.             ShowMessage(BestWorkman + ' собрал ' + IntToStr(MaxDetails) + ' деталей, что является лучшим показателем! День высшей производительности: ' + BestDay)
  453.         else
  454.             ShowMessage('Таких нет!');
  455.     end
  456.     else
  457.         ShowMessage('Проверьте таблицу!');
  458. end;
  459.  
  460. procedure TMainForm.DeleteButtonClick(Sender: TObject);   //Удаление поля таблицы
  461. var
  462.     ArrayOfRecords: AoW;
  463.     I: Integer;
  464. begin
  465.     GlobalCorrect := False;
  466.     if SpinEditOfWorkers.Value > 1 then
  467.     begin
  468.         if IsMainStringGridCorrect(MainStringGrid, SpinEditOfWorkers) then
  469.         begin
  470.             Setlength(ArrayOfRecords, SpinEditOfWorkers.Value-1);
  471.             I := 1;
  472.             DeleteRow(ArrayOfRecords, I, ADeletedRow, MainStringGrid, SpinEditOfWorkers);
  473.         end
  474.         else
  475.             ShowMessage('Проверьте таблицу!');
  476.     end
  477. end;
  478.  
  479. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement