% % ElectionResults class (file "election.tu") % % Written by Phil Edmonds April 1997. % % Sorry for not having more comments in this. % unit class ElectionResults export Init, RegisterCandidate, AddResults, RidingReport, PartyReport const MaxCandidates := 10 const MaxRidings := 20 type CandidateType : record name : string party : string riding : int votes : int end record type RidingType : record num_cand : int ballots : int candidate : array 1 .. MaxCandidates of CandidateType end record var num_ridings : int var riding : array 1 .. MaxRidings of RidingType var total_votes : int % ----------------- private ------------------------------------- % ( can only be used in this file ) proc PrintRidingLine (n : string, p : string, v : int, per : real) put n : 10, " (", p : 10, ") ", v : 4, " ", per : 5 : 1, "%" end PrintRidingLine proc PrintPartyLine (p : string, s : int, pop : real) put p : 10, " ", s : 4, " ", pop : 5 : 1, "%" end PrintPartyLine % sorts by decreasing # of votes, then by increasing orthographic name % uses a bubble sort (see textbook) % proc SortRiding (var R : RidingType) for i : 1 .. R.num_cand - 1 for decreasing j : R.num_cand .. i + 1 if R.candidate (i).votes < R.candidate (j).votes or (R.candidate (i).votes = R.candidate (j).votes and R.candidate (i).party > R.candidate (j).party) then var temp := R.candidate (i) R.candidate (i) := R.candidate (j) R.candidate (j) := temp end if end for end for end SortRiding % ----------------- public ------------------------------------- % ( these are exported (above) so, any program can call them ) proc Init (total_ridings : int) assert total_ridings > 0 and total_ridings <= MaxRidings for i : 1 .. total_ridings riding (i).num_cand := 0 riding (i).ballots := 0 end for total_votes := 0 num_ridings := total_ridings end Init proc RegisterCandidate (riding_n : int, name : string, party : string) assert riding (riding_n).num_cand < MaxCandidates riding (riding_n).num_cand += 1 var c := riding (riding_n).num_cand riding (riding_n).candidate (c).name := name riding (riding_n).candidate (c).party := party riding (riding_n).candidate (c).riding := riding_n riding (riding_n).candidate (c).votes := 0 end RegisterCandidate proc AddResults (riding_n : int, name : string, votes : int) var c := 1 loop exit when c > riding (riding_n).num_cand exit when riding (riding_n).candidate (c).name = name c += 1 end loop if c <= riding (riding_n).num_cand then riding (riding_n).candidate (c).votes += votes riding (riding_n).ballots += votes total_votes += votes end if end AddResults proc RidingReport (riding_n : int) SortRiding (riding (riding_n)) for c : 1 .. riding (riding_n).num_cand var percentage := 0.0 if (riding (riding_n).ballots > 0) then percentage := riding (riding_n).candidate (c).votes / riding (riding_n).ballots * 100 end if PrintRidingLine ( riding (riding_n).candidate (c).name, riding (riding_n).candidate (c).party, riding (riding_n).candidate (c).votes, percentage) end for end RidingReport proc PartyReport (party : string) var seats_won : int := 0 var pop_vote : int := 0 for r : 1 .. num_ridings SortRiding (riding (r)) if riding (r).candidate (1).party = party then seats_won += 1 end if var c := 1 loop exit when c > riding (r).num_cand exit when riding (r).candidate (c).party = party c += 1 end loop if c <= riding (r).num_cand then pop_vote += riding (r).candidate (c).votes end if end for if total_votes = 0 then PrintPartyLine (party, seats_won, 0) else PrintPartyLine (party, seats_won, pop_vote / total_votes * 100) end if end PartyReport end ElectionResults