From f89d098c9a82142eb2927d46752bbff3d02f3b35 Mon Sep 17 00:00:00 2001 From: Om Raheja Date: Tue, 27 Aug 2024 17:07:20 -0400 Subject: [PATCH] made rank work with struct --- rank.h | 8 ++++---- weightedrank | Bin 0 -> 34096 bytes weightedrank.c | 47 ++++++++++++++++++++++------------------------- 3 files changed, 26 insertions(+), 29 deletions(-) create mode 100755 weightedrank diff --git a/rank.h b/rank.h index 2171f2f..931cc25 100644 --- a/rank.h +++ b/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 diff --git a/weightedrank b/weightedrank new file mode 100755 index 0000000000000000000000000000000000000000..d77595d2d187ec96bf4ae861e59a8e098480aa06 GIT binary patch literal 34096 zcmeI5e{56N702((ON#T0geF}e0rJY)DuxyaMJ1#OI5k_C5LVw*j!OS)*&rcGO+6C16z1*&ZhKeV;dCWn${-*ew{ zcrm4w)_?ZLxzfqI=iGbGJ@<3Zd->~L|Ni>5PYQ%k1WFcU7DUSy;-Jt_6JiczEkxHh zG(6h4rE%kWvQ95@@O0BH8u>Ydj=J8|*xWR|S&ny1w+U0vftds$$)f8)*Y2RH;F0O? z>O;n2nr0Y%NT+p8m2453s-vRofne|Uz?3P|pJSEbZ@1ZlbgMs`=?@HL(e>6q(C_wk zOzG3t8#n#^&1@&#TCBdwuIs(tU2boi?)J9(q(0r>z-lADuTy6f(ldP+EyiEkhwnjj zeN97CgYI1a#3o}m9A*;WDNDvvg^`5zsu-f{9X`u8+}2+w`pV)k#+#bB1C_kAg6GXV zdwks!Uu|5oE}bKxjJTjvSxQNPLh;D_k(v<8g$Pl6w98d!c+k|Vo)f|WGqITs>48rD z-Z>z|FrN944G?Qdues1_%&7NZd|8H#)R_^4wZz-WmwC z(VDZ`8bD0B84Kdsv?maBc@{si7;(9`qZ7(~5c1y%J~l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O1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#W zOn?b60Vco%m;e)C0!)AjFaajO1a3<}L(2xpAqf4}xp5Z&Dkzpq0_1Or8&>0Y#f1a6 z0XWhYW1NBu1eALquq$7Kgdnd%eggRu#dz#fnb;3*5b!6 z-s2bLb=>CA+Ovo3rWSAo(^??d(%MCe-rnNwruLhbj@}l3n_+5mdwYzU-#g{2y~7m@ z2)*6!atR&v*3K5c&^;~P-M&_#_xRo3V7q`ne`}z}Ypzexjo*2v3ldaubT|H7^`NSI zI(64>S8H$3rFXho+FX8->ki;z2T|z3Wf0;{WBgT5R9=lh!;2K$zJNk2{}KfE=7`cd zL#N+YC!Owgp!P3A@T96N_yCO&HnnuUhVzI8`*V@I&X zMq>T{X4>_?6bxerU&2z-dtM3~(ndKbvOq6OUgY6IdozRzkc5wDSrVaP`OLpFn#^Wp zm&@JJi3>>RUKFwWUqAiMf0n%T{BKSzc)$D;?W9Zn#_6y0|9k#F>sQP_C0g(y|?<-9=`wN=e37M#hOQ}1NG-#zdSOc9+=GE zeq;IhD`k_;3pF1U{HUhmccbh7I%~l{t~G5-+`0Gdre9xw@#DVB>s-mZ-`KUZ^MRcg wKhOUcZ9mj@boJ9`*4AJ6{-Vf*(ebO#{A~GMZ}mO>T-k|t9^M?S{(}kazgJSHuK)l5 literal 0 HcmV?d00001 diff --git a/weightedrank.c b/weightedrank.c index 1c4423b..79249ed 100644 --- a/weightedrank.c +++ b/weightedrank.c @@ -6,11 +6,12 @@ #include #include +#include #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;