made rank work with struct

This commit is contained in:
Om Raheja 2024-08-27 17:07:20 -04:00
parent 0083dcae95
commit f89d098c9a
3 changed files with 26 additions and 29 deletions

8
rank.h
View File

@ -8,14 +8,14 @@ struct Rank
{ {
char name[M_STR_LEN]; char name[M_STR_LEN];
int score; int score;
} };
struct RankList struct RankList
{ {
Rank rank[M_STR]; struct Rank rank[M_STR];
Rank rank_sorted[M_STR]; struct Rank rank_sorted[M_STR];
int rank_count; int rank_count;
} };
#endif #endif

BIN
weightedrank Executable file

Binary file not shown.

View File

@ -6,11 +6,12 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "color.h" #include "color.h"
#include "rank.h" #include "rank.h"
void isort(char [][M_STR], char [][M_STR_LEN], int *, int); void isort(char [][M_STR], struct Rank[], int);
int int
main(void) main(void)
@ -18,7 +19,7 @@ main(void)
// allocate memory for Rank // allocate memory for Rank
struct RankList *items = malloc(sizeof(struct RankList)); struct RankList *items = malloc(sizeof(struct RankList));
memset(items, 0, sizeof(struct RankList)); memset(items, 0, sizeof(struct RankList));
if (!rank) { if (!items) {
printf(BOLDRED "Error: out of memory\n" RESET); printf(BOLDRED "Error: out of memory\n" RESET);
return 1; return 1;
} }
@ -29,7 +30,7 @@ main(void)
int i; int i;
for (i = 0; i < M_STR; i++) { for (i = 0; i < M_STR; i++) {
// local buffer for name // local buffer for name
char *name = items->rank[i]->name; char *name = items->rank[i].name;
fgets(name, M_STR_LEN, stdin); fgets(name, M_STR_LEN, stdin);
@ -43,9 +44,6 @@ main(void)
name[strcspn(name, "\n")] = '\0'; name[strcspn(name, "\n")] = '\0';
} }
// init sorted strings
memcpy(items->rank_sorted, items->rank, M_STR);
// construct matrix of comparisons // construct matrix of comparisons
// NOTE: it doesn't actually store the "reason", // NOTE: it doesn't actually store the "reason",
// it just forces the user to type something out lol // it just forces the user to type something out lol
@ -54,25 +52,22 @@ main(void)
// memcpy(3) with null bytes // memcpy(3) with null bytes
memset(matrix, 0, sizeof(matrix)); memset(matrix, 0, sizeof(matrix));
// memcpy(3) with null bytes
memset(winners, 0, sizeof(winners));
// compare all of the elements // compare all of the elements
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
for (int k = j + 1; k < i; k++) { 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 // read one char, set matrix accordingly
char c = getchar(); char c = getchar();
if (c == '>') { if (c == '>') {
matrix[j][k]++; matrix[j][k]++;
winners[j]++; items->rank[j].score++;
} else if (c == '<') { } else if (c == '<') {
matrix[k][j]++; matrix[k][j]++;
winners[k]++; items->rank[k].score++;
} else { } else {
printf("Setting to " BOLDRED "<" RESET ": %c\n", c); printf("Setting to " BOLDRED "<" RESET ": %c\n", c);
matrix[k][j]++; matrix[k][j]++;
winners[k]++; items->rank[k].score++;
} }
// clear input buffer // 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 // 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"); printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n");
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
printf("%s: %d | %s: %d\n", printf("%s: %d | %s: %d\n",
strings[j], winners[j], items->rank[j].name, items->rank[j].score,
sorted_strings[j], sorted_winners[j]); items->rank_sorted[j].name, items->rank_sorted[j].score);
} }
free(items);
return 0; return 0;
} }
void 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 // sort by value, modify key accordingly
for (int i = 1; i < len; i++) { for (int i = 1; i < len; i++) {
int key = value[i]; int key = to_sort[i].score;
char str_key[M_STR_LEN]; 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; int j = i - 1;
while (j >= 0 && (value[j] < key || matrix[i][j]) ) { while (j >= 0 && (to_sort[j].score < key || matrix[i][j]) ) {
value[j + 1] = value[j]; to_sort[j + 1].score = to_sort[j].score;
strncpy(name[j + 1], name[j], M_STR_LEN); strncpy(to_sort[j + 1].name, to_sort[j].name, M_STR_LEN);
j--; j--;
} }
value[++j] = key; to_sort[++j].score = key;
strncpy(name[j], str_key, M_STR_LEN); strncpy(to_sort[j].name, str_key, M_STR_LEN);
} }
return; return;