Advertisement
Matixs

Untitled

May 31st, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.59 KB | None | 0 0
  1. unit Newsletter;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  7.   Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, System.Generics.Collections;
  8.  
  9. type
  10.   TNewsletterFrame = class(TFrame)
  11.     GroupBox1: TGroupBox;
  12.     SGCandidate: TStringGrid;
  13.     procedure FrameEnter(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.   public
  17.     { Public declarations }
  18.   end;
  19.  
  20. implementation
  21.  
  22. {$R *.dfm}
  23.  
  24. uses BidirectionalCandidate, BidirectionalParty, Candidate, CountyNumber, Main;
  25.  
  26. const
  27.     HEADER_PARTYNAME = 'Название партии';
  28.     HEADER_AMOUNTREQUEST = 'Кол-во заявок';
  29.     HEADER_MIDDLEAGE = 'Средний возраст кандидатов';
  30.     HEADER_FREQUENTPROFESSION = 'Частая профессия';
  31.  
  32. procedure ClearStringGrid(SG : TStringGrid);
  33. var
  34.     I, J : Integer;
  35. begin
  36.     for I := 0 to SG.RowCount - 1 do
  37.     begin
  38.         SG.Rows[I].Clear;
  39.     end;
  40. end;
  41.  
  42. procedure FillHeadersSG(SGCandidate : TStringGrid);
  43. begin
  44.     SGCandidate.Cells[0,0] := HEADER_PARTYNAME;
  45.     SGCandidate.Cells[1,0] := HEADER_AMOUNTREQUEST;
  46.     SGCandidate.Cells[2,0] := HEADER_MIDDLEAGE;
  47.     SGCandidate.Cells[3,0] := HEADER_FREQUENTPROFESSION;
  48.     SGCandidate.ColWidths[0] := 300;
  49.     SGCandidate.ColWidths[1] := 300;
  50.     SGCandidate.ColWidths[2] := 400;
  51.     SGCandidate.ColWidths[3] := 350;
  52. end;
  53.  
  54. Function FillDictionary(PartyId : Integer) : TDictionary<String,Integer>;
  55. var
  56.     ProfessionDictionary : TDictionary<String, Integer>;
  57.     TempHeadCandidate : PCandidateList;
  58.     Count : Integer;
  59. begin
  60.     TempHeadCandidate := Main.CandidateList.Head;
  61.     ProfessionDictionary := TDictionary<String, Integer>.Create();
  62.     while TempHeadCandidate <> NIL do
  63.     begin
  64.         if TempHeadCandidate^.Candidate.PartyId = PartyId then
  65.         begin
  66.             if not ProfessionDictionary.TryGetValue(TempHeadCandidate^.Candidate.Profession, Count) then
  67.                 Count := 0;
  68.             ProfessionDictionary.AddOrSetValue(TempHeadCandidate^.Candidate.Profession, Count + 1);
  69.         end;
  70.         TempHeadCandidate := TempHeadCandidate^.Next;
  71.     end;
  72.  
  73.     FillDictionary := ProfessionDictionary;
  74. end;
  75.  
  76. Function GetMostFrequentProfession(ProfessionDict : TDictionary<String,Integer>) : String;
  77. var
  78.     MaxCount, Count: Integer;
  79.     MostFrequentProfession: string;
  80. begin
  81.     MaxCount := 0;
  82.     for var Profession in ProfessionDict.Keys do
  83.     begin
  84.         Count := ProfessionDict[Profession];
  85.         if Count > MaxCount then
  86.         begin
  87.             MaxCount := Count;
  88.             MostFrequentProfession := Profession;
  89.         end;
  90.     end;
  91.     GetMostFrequentProfession := MostFrequentProfession;
  92. end;
  93.  
  94. Procedure FillStringGrid(SG : TStringGrid);
  95. var
  96.     HeadCandidateList : PCandidateList;
  97.     HeadPartyList : PPartyList;
  98.     I, CurrentPartyId, AmountRequest, AgeAmount: Integer;
  99.     ProfessionDictionary : TDictionary<String, Integer>;
  100.     MostFrequentProfession : String;
  101. begin
  102.     ProfessionDictionary := TDictionary<String, Integer>.Create;
  103.  
  104.     ClearStringGrid(SG);
  105.     FillHeadersSG(SG);
  106.  
  107.     HeadPartyList := Main.PartyList.Head;
  108.  
  109.     I := 1;
  110.  
  111.     while HeadPartyList <> NIL do
  112.     begin
  113.         SG.Cells[0, I] := HeadPartyList^.Party.Name;
  114.         Inc(I);
  115.         HeadPartyList := HeadPartyList^.Next;
  116.     end;
  117.  
  118.     SG.RowCount := I;
  119.  
  120.     for I := 1 to SG.RowCount - 1 do
  121.     begin
  122.         HeadCandidateList := Main.CandidateList.Head;
  123.         AmountRequest := 0;
  124.         AgeAmount := 0;
  125.  
  126.         while HeadCandidateList <> NIL do
  127.         begin
  128.             CurrentPartyId := TPartyList.GetPartyPrimaryKeyByName(Main.PartyList, SG.Cells[0, I]);
  129.  
  130.             if HeadCandidateList.Candidate.PartyId = CurrentPartyId then
  131.             begin
  132.                 Inc(AmountRequest); // Количество заявок по партии
  133.                 AgeAmount := AgeAmount + HeadCandidateList^.Candidate.Age; // Сумма возрастов
  134.  
  135.             end;
  136.  
  137.             HeadCandidateList := HeadCandidateList^.Next;
  138.         end;
  139.  
  140.         AgeAmount := AgeAmount div AmountRequest; // Средний возраст
  141.  
  142.         ProfessionDictionary := FillDictionary(CurrentPartyId);
  143.         MostFrequentProfession := GetMostFrequentProfession(ProfessionDictionary);
  144.  
  145.         SG.Cells[1, I] := AmountRequest.ToString;
  146.         SG.Cells[2, I] := AgeAmount.ToString;
  147.         SG.Cells[3, I] := MostFrequentProfession;
  148.     end;
  149.  
  150. end;
  151.  
  152. procedure TNewsletterFrame.FrameEnter(Sender: TObject);
  153. begin
  154.     FillStringGrid(SGCandidate);
  155. end;
  156.  
  157. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement