small bug in output but everything else fixed

This commit is contained in:
Om Raheja 2024-08-29 14:00:12 -04:00
parent d8b87a0de0
commit e04c6e3a21
2 changed files with 46 additions and 26 deletions

Binary file not shown.

View File

@ -65,10 +65,10 @@ main(int argc, char *argv[])
} }
rank(items->srank, items->rank_count); rank(items->srank, items->rank_count);
if (!weighted) {
// print the rank list
print_ranklist(items); print_ranklist(items);
} else { if (weighted) {
// weighted ranking
puts(BOLDWHITE "Choosing top 3 weights." RESET);
// leave if any are null // leave if any are null
if (items->rank_count < 3) { if (items->rank_count < 3) {
@ -90,7 +90,6 @@ main(int argc, char *argv[])
char buf[16]; //enough to hold weight char buf[16]; //enough to hold weight
float left = 1.0; // 100% float left = 1.0; // 100%
char *ptr; char *ptr;
// ask for weights
/* weight 1 */ /* weight 1 */
printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(1.0) > " RESET, weighted.opt1); printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(1.0) > " RESET, weighted.opt1);
@ -101,14 +100,13 @@ main(int argc, char *argv[])
goto w_end; goto w_end;
} }
if (ptr != NULL) { printf(BOLDWHITE "Using %g for %s" RESET "\n",
printf(BOLDWHITE "Using %f" RESET "\n", weighted.opt1_w); weighted.opt1_w, weighted.opt1);
}
left -= weighted.opt1_w; left -= weighted.opt1_w;
/* weight 2 */ /* weight 2 */
printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(%f) > " RESET, weighted.opt2, left); printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(%g) > " RESET, weighted.opt2, left);
fgets(buf, 16, stdin); fgets(buf, 16, stdin);
weighted.opt2_w = strtof(buf, &ptr); weighted.opt2_w = strtof(buf, &ptr);
if (weighted.opt2_w >= 1.0 || weighted.opt2_w <= 0.0) { if (weighted.opt2_w >= 1.0 || weighted.opt2_w <= 0.0) {
@ -116,24 +114,27 @@ main(int argc, char *argv[])
goto w_end; goto w_end;
} }
if (ptr != NULL) { printf(BOLDWHITE "Using %g for %s" RESET "\n",
printf(BOLDWHITE "Using %f" RESET "\n", weighted.opt2_w); weighted.opt2_w, weighted.opt2);
}
left -= weighted.opt2_w; left -= weighted.opt2_w;
/* weight 3 */ /* weight 3 */
printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(%f) > " RESET, weighted.opt3, left); weighted.opt3_w = left;
fgets(buf, 16, stdin); printf(BOLDWHITE "Using %g for %s" RESET "\n",
weighted.opt3_w = strtof(buf, &ptr); weighted.opt3_w, weighted.opt3);
if (weighted.opt3_w >= 1.0 || weighted.opt3_w <= 0.0) {
printf(BOLDRED "Error: weight must be between 0 and 1\n" RESET); // copy memory before ranking
goto w_end; int space = sizeof(struct Rank) * items->rank_count;
// manual copy with for loop
for (int i = 0; i < items->rank_count; i++) {
strncpy(mem->rl.srank[0][i].name, items->rank[i], M_STR_LEN);
} }
if (ptr != NULL) { memcpy(mem->rl.srank[1], mem->rl.srank[0], space);
printf(BOLDWHITE "Using %f" RESET "\n", weighted.opt3_w); memcpy(mem->rl.srank[2], mem->rl.srank[1], space);
}
printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt1); printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt1);
rank(mem->rl.srank[0], mem->rl.rank_count); rank(mem->rl.srank[0], mem->rl.rank_count);
@ -142,15 +143,34 @@ main(int argc, char *argv[])
printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt3); printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt3);
rank(mem->rl.srank[2], mem->rl.rank_count); rank(mem->rl.srank[2], mem->rl.rank_count);
// now do some vector math :)
// nothing too performance intensive, don't need to optimize
printf("\n\n" BOLDWHITE "**FINAL RANKING**" "\n" printf("\n\n" BOLDWHITE "**FINAL RANKING**" "\n"
"%s " BOLDGREEN "(%f)" BOLDWHITE " | " BOLDYELLOW "name | "
"%s " BOLDGREEN "(%f)" BOLDWHITE " | " "%s " BOLDGREEN "(%g)" BOLDWHITE " | "
"%s " BOLDGREEN "(%f)" RESET "\n\n", "%s " BOLDGREEN "(%g)" BOLDWHITE " | "
"%s " BOLDGREEN "(%g)" BOLDWHITE " | "
"total " RESET "\n\n",
weighted.opt1, weighted.opt1_w, weighted.opt1, weighted.opt1_w,
weighted.opt2, weighted.opt2_w, weighted.opt2, weighted.opt2_w,
weighted.opt3, weighted.opt3_w); weighted.opt3, weighted.opt3_w);
for (int i = 0; i < mem->rl.rank_count; i++) {
// garbage dot product T_T
int total = mem->rl.srank[0][i].score * weighted.opt1_w +
mem->rl.srank[1][i].score * weighted.opt2_w +
mem->rl.srank[2][i].score * weighted.opt3_w;
printf(BOLDWHITE "%s | "
BOLDGREEN "%d*%g " BOLDWHITE "|"
BOLDGREEN "%d*%g " BOLDWHITE "|"
BOLDGREEN "%d*%g " BOLDWHITE "|"
BOLDGREEN "%d " RESET "\n",
mem->rl.srank[0][i].name,
mem->rl.srank[0][i].score, weighted.opt1_w,
mem->rl.srank[1][i].score, weighted.opt2_w,
mem->rl.srank[2][i].score, weighted.opt3_w,
total);
}
w_end: w_end:
free(mem); free(mem);
} }