-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlrm.c
61 lines (51 loc) · 1.44 KB
/
lrm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "lrm.h"
#include "seat_alloc_utils.c"
// Quota functions
double hare(const int partyc, party *parties, int seats)
{
return floor(total_valid_poll(partyc, parties) / (double)seats);
}
double droop(const int partyc, party *parties, int seats)
{
return floor(total_valid_poll(partyc, parties) / (double)(seats + 1)) + 1;
}
// Procedure
void lrm(const int partyc, party *parties, int seats)
{
double quota = hare(partyc, parties, seats);
auto int comp_residual();
party *p;
for (int i = 0; i < partyc; i++)
{
p = &parties[i];
p->seats = floor(p->votes / quota);
seats -= p->seats;
}
qsort(parties, partyc, sizeof(party), comp_residual);
for (p = parties; seats > 0; p++)
{
p->seats++;
seats--;
}
int comp_residual(const void *elem1, const void *elem2)
{
party i = *((party *)elem1);
party j = *((party *)elem2);
int i_residual = i.votes - (i.seats * quota);
int j_residual = j.votes - (j.seats * quota);
return (i_residual > j_residual) ? -1 : (i_residual < j_residual) ? 1
: 0;
}
}
// Display
void lrm_print_table(const int partyc, party *parties)
{
for (int i = 0; i < partyc; i++)
{
party p = parties[i];
printf("| %-8i | %-3i |\n", p.votes, p.seats);
}
}