Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Newsletter;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
- Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, System.Generics.Collections;
- type
- TNewsletterFrame = class(TFrame)
- GroupBox1: TGroupBox;
- SGCandidate: TStringGrid;
- procedure FrameEnter(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- implementation
- {$R *.dfm}
- uses BidirectionalCandidate, BidirectionalParty, Candidate, CountyNumber, Main;
- const
- HEADER_PARTYNAME = 'Название партии';
- HEADER_AMOUNTREQUEST = 'Кол-во заявок';
- HEADER_MIDDLEAGE = 'Средний возраст кандидатов';
- HEADER_FREQUENTPROFESSION = 'Частая профессия';
- procedure ClearStringGrid(SG : TStringGrid);
- var
- I, J : Integer;
- begin
- for I := 0 to SG.RowCount - 1 do
- begin
- SG.Rows[I].Clear;
- end;
- end;
- procedure FillHeadersSG(SGCandidate : TStringGrid);
- begin
- SGCandidate.Cells[0,0] := HEADER_PARTYNAME;
- SGCandidate.Cells[1,0] := HEADER_AMOUNTREQUEST;
- SGCandidate.Cells[2,0] := HEADER_MIDDLEAGE;
- SGCandidate.Cells[3,0] := HEADER_FREQUENTPROFESSION;
- SGCandidate.ColWidths[0] := 300;
- SGCandidate.ColWidths[1] := 300;
- SGCandidate.ColWidths[2] := 400;
- SGCandidate.ColWidths[3] := 350;
- end;
- Function FillDictionary(PartyId : Integer) : TDictionary<String,Integer>;
- var
- ProfessionDictionary : TDictionary<String, Integer>;
- TempHeadCandidate : PCandidateList;
- Count : Integer;
- begin
- TempHeadCandidate := Main.CandidateList.Head;
- ProfessionDictionary := TDictionary<String, Integer>.Create();
- while TempHeadCandidate <> NIL do
- begin
- if TempHeadCandidate^.Candidate.PartyId = PartyId then
- begin
- if not ProfessionDictionary.TryGetValue(TempHeadCandidate^.Candidate.Profession, Count) then
- Count := 0;
- ProfessionDictionary.AddOrSetValue(TempHeadCandidate^.Candidate.Profession, Count + 1);
- end;
- TempHeadCandidate := TempHeadCandidate^.Next;
- end;
- FillDictionary := ProfessionDictionary;
- end;
- Function GetMostFrequentProfession(ProfessionDict : TDictionary<String,Integer>) : String;
- var
- MaxCount, Count: Integer;
- MostFrequentProfession: string;
- begin
- MaxCount := 0;
- for var Profession in ProfessionDict.Keys do
- begin
- Count := ProfessionDict[Profession];
- if Count > MaxCount then
- begin
- MaxCount := Count;
- MostFrequentProfession := Profession;
- end;
- end;
- GetMostFrequentProfession := MostFrequentProfession;
- end;
- Procedure FillStringGrid(SG : TStringGrid);
- var
- HeadCandidateList : PCandidateList;
- HeadPartyList : PPartyList;
- I, CurrentPartyId, AmountRequest, AgeAmount: Integer;
- ProfessionDictionary : TDictionary<String, Integer>;
- MostFrequentProfession : String;
- begin
- ProfessionDictionary := TDictionary<String, Integer>.Create;
- ClearStringGrid(SG);
- FillHeadersSG(SG);
- HeadPartyList := Main.PartyList.Head;
- I := 1;
- while HeadPartyList <> NIL do
- begin
- SG.Cells[0, I] := HeadPartyList^.Party.Name;
- Inc(I);
- HeadPartyList := HeadPartyList^.Next;
- end;
- SG.RowCount := I;
- for I := 1 to SG.RowCount - 1 do
- begin
- HeadCandidateList := Main.CandidateList.Head;
- AmountRequest := 0;
- AgeAmount := 0;
- while HeadCandidateList <> NIL do
- begin
- CurrentPartyId := TPartyList.GetPartyPrimaryKeyByName(Main.PartyList, SG.Cells[0, I]);
- if HeadCandidateList.Candidate.PartyId = CurrentPartyId then
- begin
- Inc(AmountRequest); // Количество заявок по партии
- AgeAmount := AgeAmount + HeadCandidateList^.Candidate.Age; // Сумма возрастов
- end;
- HeadCandidateList := HeadCandidateList^.Next;
- end;
- AgeAmount := AgeAmount div AmountRequest; // Средний возраст
- ProfessionDictionary := FillDictionary(CurrentPartyId);
- MostFrequentProfession := GetMostFrequentProfession(ProfessionDictionary);
- SG.Cells[1, I] := AmountRequest.ToString;
- SG.Cells[2, I] := AgeAmount.ToString;
- SG.Cells[3, I] := MostFrequentProfession;
- end;
- end;
- procedure TNewsletterFrame.FrameEnter(Sender: TObject);
- begin
- FillStringGrid(SGCandidate);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement