1 | /* Quick voting software. |
---|
2 | * Usage: |
---|
3 | * vote <file1> <file2> ... |
---|
4 | * File format: |
---|
5 | * <key1> <value1> |
---|
6 | * <key1> <value1> |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <string.h> |
---|
11 | #include <stdlib.h> |
---|
12 | |
---|
13 | #define MAXLEN 20 |
---|
14 | #define MAXITEMS 2000 |
---|
15 | |
---|
16 | static char *items[MAXITEMS]; |
---|
17 | static int nitems; |
---|
18 | |
---|
19 | static int *matrix; |
---|
20 | |
---|
21 | static int lookup(char const *str) |
---|
22 | { |
---|
23 | int i; |
---|
24 | |
---|
25 | for(i = 0; i < nitems; i++) |
---|
26 | if(!strcmp(str, items[i])) |
---|
27 | return i; |
---|
28 | |
---|
29 | return -1; |
---|
30 | } |
---|
31 | |
---|
32 | static int record(char const *str) |
---|
33 | { |
---|
34 | int n = lookup(str); |
---|
35 | |
---|
36 | if(n >= 0) |
---|
37 | return n; |
---|
38 | |
---|
39 | items[nitems] = strdup(str); |
---|
40 | return nitems++; |
---|
41 | } |
---|
42 | |
---|
43 | static void recordfile(char const *file) |
---|
44 | { |
---|
45 | char buf[MAXLEN]; |
---|
46 | FILE *f; |
---|
47 | float val; |
---|
48 | |
---|
49 | f = fopen(file, "r"); |
---|
50 | if(!f) |
---|
51 | return; |
---|
52 | while(!feof(f)) |
---|
53 | { |
---|
54 | fscanf(f, "%s %g", buf, &val); |
---|
55 | record(buf); |
---|
56 | } |
---|
57 | fclose(f); |
---|
58 | } |
---|
59 | |
---|
60 | static void readfile(char const *file) |
---|
61 | { |
---|
62 | float values[MAXITEMS]; |
---|
63 | char buf[MAXLEN]; |
---|
64 | FILE *f; |
---|
65 | float val; |
---|
66 | int n, i, j; |
---|
67 | |
---|
68 | for(i = 0; i < nitems; i++) |
---|
69 | values[i] = 9999.; |
---|
70 | |
---|
71 | f = fopen(file, "r"); |
---|
72 | if(!f) |
---|
73 | return; |
---|
74 | while(!feof(f)) |
---|
75 | { |
---|
76 | fscanf(f, "%s %g", buf, &val); |
---|
77 | n = lookup(buf); |
---|
78 | values[n] = val; |
---|
79 | } |
---|
80 | fclose(f); |
---|
81 | |
---|
82 | for(j = 0; j < nitems; j++) |
---|
83 | for(i = j + 1; i < nitems; i++) |
---|
84 | if(values[i] < values[j]) |
---|
85 | matrix[j * nitems + i]++; |
---|
86 | else if(values[i] > values[j]) |
---|
87 | matrix[i * nitems + j]++; |
---|
88 | } |
---|
89 | |
---|
90 | static void solvematrix(void) |
---|
91 | { |
---|
92 | int wins[MAXITEMS]; |
---|
93 | int i, j; |
---|
94 | |
---|
95 | memset(wins, 0, MAXITEMS * sizeof(int)); |
---|
96 | |
---|
97 | for(j = 0; j < nitems; j++) |
---|
98 | for(i = j + 1; i < nitems; i++) |
---|
99 | { |
---|
100 | if(matrix[j * nitems + i] > matrix[i * nitems + j]) |
---|
101 | wins[i]++; |
---|
102 | else |
---|
103 | wins[j]++; |
---|
104 | } |
---|
105 | |
---|
106 | for(i = 0; i < nitems; i++) |
---|
107 | printf("%s (%i): %i wins\n", items[i], i, wins[i]); |
---|
108 | } |
---|
109 | |
---|
110 | int main(int argc, char *argv[]) |
---|
111 | { |
---|
112 | int i; |
---|
113 | |
---|
114 | for(i = 1; i < argc; i++) |
---|
115 | recordfile(argv[i]); |
---|
116 | |
---|
117 | matrix = malloc(nitems * nitems * sizeof(int)); |
---|
118 | memset(matrix, 0, nitems * nitems * sizeof(int)); |
---|
119 | |
---|
120 | for(i = 1; i < argc; i++) |
---|
121 | readfile(argv[i]); |
---|
122 | |
---|
123 | solvematrix(); |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|