made rank work with struct
This commit is contained in:
parent
0083dcae95
commit
f89d098c9a
8
rank.h
8
rank.h
@ -8,14 +8,14 @@ struct Rank
|
||||
{
|
||||
char name[M_STR_LEN];
|
||||
int score;
|
||||
}
|
||||
};
|
||||
|
||||
struct RankList
|
||||
{
|
||||
Rank rank[M_STR];
|
||||
Rank rank_sorted[M_STR];
|
||||
struct Rank rank[M_STR];
|
||||
struct Rank rank_sorted[M_STR];
|
||||
int rank_count;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
BIN
weightedrank
Executable file
BIN
weightedrank
Executable file
Binary file not shown.
@ -6,11 +6,12 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "color.h"
|
||||
#include "rank.h"
|
||||
|
||||
void isort(char [][M_STR], char [][M_STR_LEN], int *, int);
|
||||
void isort(char [][M_STR], struct Rank[], int);
|
||||
|
||||
int
|
||||
main(void)
|
||||
@ -18,7 +19,7 @@ main(void)
|
||||
// allocate memory for Rank
|
||||
struct RankList *items = malloc(sizeof(struct RankList));
|
||||
memset(items, 0, sizeof(struct RankList));
|
||||
if (!rank) {
|
||||
if (!items) {
|
||||
printf(BOLDRED "Error: out of memory\n" RESET);
|
||||
return 1;
|
||||
}
|
||||
@ -29,7 +30,7 @@ main(void)
|
||||
int i;
|
||||
for (i = 0; i < M_STR; i++) {
|
||||
// local buffer for name
|
||||
char *name = items->rank[i]->name;
|
||||
char *name = items->rank[i].name;
|
||||
|
||||
fgets(name, M_STR_LEN, stdin);
|
||||
|
||||
@ -43,9 +44,6 @@ main(void)
|
||||
name[strcspn(name, "\n")] = '\0';
|
||||
}
|
||||
|
||||
// init sorted strings
|
||||
memcpy(items->rank_sorted, items->rank, M_STR);
|
||||
|
||||
// construct matrix of comparisons
|
||||
// NOTE: it doesn't actually store the "reason",
|
||||
// it just forces the user to type something out lol
|
||||
@ -54,25 +52,22 @@ main(void)
|
||||
// memcpy(3) with null bytes
|
||||
memset(matrix, 0, sizeof(matrix));
|
||||
|
||||
// memcpy(3) with null bytes
|
||||
memset(winners, 0, sizeof(winners));
|
||||
|
||||
// compare all of the elements
|
||||
for (int j = 0; j < i; j++) {
|
||||
for (int k = j + 1; k < i; k++) {
|
||||
printf("%s vs %s (>/<): ", strings[j], strings[k]);
|
||||
printf("%s vs %s (>/<): ", items->rank[j].name, items->rank[k].name);
|
||||
// read one char, set matrix accordingly
|
||||
char c = getchar();
|
||||
if (c == '>') {
|
||||
matrix[j][k]++;
|
||||
winners[j]++;
|
||||
items->rank[j].score++;
|
||||
} else if (c == '<') {
|
||||
matrix[k][j]++;
|
||||
winners[k]++;
|
||||
items->rank[k].score++;
|
||||
} else {
|
||||
printf("Setting to " BOLDRED "<" RESET ": %c\n", c);
|
||||
matrix[k][j]++;
|
||||
winners[k]++;
|
||||
items->rank[k].score++;
|
||||
}
|
||||
|
||||
// clear input buffer
|
||||
@ -86,39 +81,41 @@ main(void)
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(sorted_winners, winners, sizeof(sorted_winners));
|
||||
memcpy(items->rank_sorted, items->rank, sizeof(struct Rank) * M_STR);
|
||||
|
||||
// insertion sort: the array **should be** nearly sorted
|
||||
isort(matrix, sorted_strings, sorted_winners, i);
|
||||
isort(matrix, items->rank_sorted, i);
|
||||
|
||||
printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n");
|
||||
for (int j = 0; j < i; j++) {
|
||||
printf("%s: %d | %s: %d\n",
|
||||
strings[j], winners[j],
|
||||
sorted_strings[j], sorted_winners[j]);
|
||||
items->rank[j].name, items->rank[j].score,
|
||||
items->rank_sorted[j].name, items->rank_sorted[j].score);
|
||||
}
|
||||
|
||||
free(items);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
isort(char matrix[][M_STR], char name[][M_STR_LEN], int *value, int len) {
|
||||
isort(char matrix[][M_STR], struct Rank to_sort[], int len) {
|
||||
// sort by value, modify key accordingly
|
||||
for (int i = 1; i < len; i++) {
|
||||
int key = value[i];
|
||||
int key = to_sort[i].score;
|
||||
|
||||
char str_key[M_STR_LEN];
|
||||
strncpy(str_key, name[i], M_STR_LEN);
|
||||
strncpy(str_key, to_sort[i].name, M_STR_LEN);
|
||||
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && (value[j] < key || matrix[i][j]) ) {
|
||||
value[j + 1] = value[j];
|
||||
strncpy(name[j + 1], name[j], M_STR_LEN);
|
||||
while (j >= 0 && (to_sort[j].score < key || matrix[i][j]) ) {
|
||||
to_sort[j + 1].score = to_sort[j].score;
|
||||
strncpy(to_sort[j + 1].name, to_sort[j].name, M_STR_LEN);
|
||||
|
||||
j--;
|
||||
}
|
||||
value[++j] = key;
|
||||
strncpy(name[j], str_key, M_STR_LEN);
|
||||
to_sort[++j].score = key;
|
||||
strncpy(to_sort[j].name, str_key, M_STR_LEN);
|
||||
}
|
||||
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user