-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathham.c
54 lines (45 loc) · 1.08 KB
/
ham.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
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "ham.h"
#include "seat_alloc_utils.c"
// Quotient functions
double dhondt(party p)
{
return p.votes / (double)(p.seats + 1);
}
double sainte_laguë(party p)
{
return p.votes / (double)(2 * p.seats + 1);
}
// Procedure
void ham(const int partyc, party *parties, int seats, __quotient_func method)
{
auto int comp();
party *p;
for (int i = 0; i < seats; i++)
{
qsort(parties, partyc, sizeof(party), comp);
p = parties;
p->seats++;
}
int comp(const void *elem1, const void *elem2)
{
double i = method(*((party *)elem1));
double j = method(*((party *)elem2));
return (i > j) ? -1 : (i < j) ? 1
: 0;
}
}
// Display
void ham_print_table(const int partyc, party *parties)
{
int total_seats = 0;
for (int i = 0; i < partyc; i++)
{
party p = parties[i];
printf("| %-8i | %-3i |\n", p.votes, p.seats);
total_seats += p.seats;
}
printf(" %-8c | %-3i |", ' ', total_seats);
}