2024-08-27 21:58:35 +02:00
|
|
|
/*
|
|
|
|
* (weighted) ranking tool in "The Thinker's Toolkit" by Morgan Jones
|
|
|
|
* Book copyright (c) 1995, 1998 Morgan Jones
|
|
|
|
* This file is public domain.
|
|
|
|
*/
|
|
|
|
|
2024-08-28 00:12:46 +02:00
|
|
|
#include <getopt.h>
|
2024-08-27 21:58:35 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-08-27 23:07:20 +02:00
|
|
|
#include <stdlib.h>
|
2024-08-27 21:58:35 +02:00
|
|
|
|
|
|
|
#include "color.h"
|
|
|
|
#include "rank.h"
|
|
|
|
|
2024-08-28 00:12:46 +02:00
|
|
|
void usage(void);
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"no-color", no_argument, NULL, 'n'},
|
|
|
|
{"weighted", no_argument, NULL, 'w'},
|
|
|
|
{"log-file", required_argument, NULL, 'l'},
|
|
|
|
{NULL, 0, NULL, 0 }
|
|
|
|
};
|
2024-08-27 21:58:35 +02:00
|
|
|
|
|
|
|
int
|
2024-08-28 00:12:46 +02:00
|
|
|
main(int argc, char *argv[])
|
2024-08-27 21:58:35 +02:00
|
|
|
{
|
|
|
|
// allocate memory for Rank
|
|
|
|
struct RankList *items = malloc(sizeof(struct RankList));
|
|
|
|
memset(items, 0, sizeof(struct RankList));
|
2024-08-27 23:07:20 +02:00
|
|
|
if (!items) {
|
2024-08-27 21:58:35 +02:00
|
|
|
printf(BOLDRED "Error: out of memory\n" RESET);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-08-28 00:12:46 +02:00
|
|
|
|
|
|
|
// get options
|
|
|
|
char ch;
|
|
|
|
char weighted = 0;
|
|
|
|
while ((ch = getopt_long(argc, argv, "hnwl:", long_options, NULL)) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
//TODO: disable_color();
|
|
|
|
puts("Not implemented yet");
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
weighted = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
//TODO: set_log_file(optarg);
|
|
|
|
puts("Not implemented yet");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 17:57:27 +02:00
|
|
|
items->rank_count = rsstdin(items->rank);
|
2024-08-29 18:57:25 +02:00
|
|
|
for (int i = 0; i < items->rank_count; i++) {
|
|
|
|
strncpy(items->srank[i].name, items->rank[i], M_STR_LEN);
|
|
|
|
items->srank[i].score = 0;
|
|
|
|
}
|
2024-08-29 00:28:53 +02:00
|
|
|
rank(items->srank, items->rank_count);
|
2024-08-28 00:12:46 +02:00
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
print_ranklist(items);
|
|
|
|
if (weighted) {
|
|
|
|
// weighted ranking
|
|
|
|
puts(BOLDWHITE "Choosing top 3 weights." RESET);
|
2024-08-28 00:12:46 +02:00
|
|
|
|
|
|
|
// leave if any are null
|
2024-08-29 18:57:25 +02:00
|
|
|
if (items->rank_count < 3) {
|
2024-08-28 00:12:46 +02:00
|
|
|
puts(BOLDRED "Error: not enough weights.\n" RESET);
|
2024-08-28 22:15:59 +02:00
|
|
|
goto w_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are now going to rank in a weighted manner for all 3 */
|
|
|
|
|
|
|
|
// init
|
|
|
|
struct WAlloc *mem = malloc(sizeof(struct WAlloc));
|
2024-08-28 23:21:31 +02:00
|
|
|
struct Weighted weighted = mem->w;
|
2024-08-28 22:15:59 +02:00
|
|
|
|
|
|
|
// move the top 3 items from the ranklist here
|
2024-08-28 23:21:31 +02:00
|
|
|
strncpy(weighted.opt1, items->srank[0].name, M_STR);
|
|
|
|
strncpy(weighted.opt2, items->srank[1].name, M_STR);
|
|
|
|
strncpy(weighted.opt3, items->srank[2].name, M_STR);
|
2024-08-28 22:15:59 +02:00
|
|
|
|
|
|
|
char buf[16]; //enough to hold weight
|
2024-08-29 18:57:25 +02:00
|
|
|
float left = 1.0; // 100%
|
2024-08-28 22:15:59 +02:00
|
|
|
char *ptr;
|
2024-08-29 18:57:25 +02:00
|
|
|
|
|
|
|
/* weight 1 */
|
|
|
|
printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(1.0) > " RESET, weighted.opt1);
|
2024-08-28 22:15:59 +02:00
|
|
|
fgets(buf, 16, stdin);
|
2024-08-28 23:21:31 +02:00
|
|
|
weighted.opt1_w = strtof(buf, &ptr);
|
2024-08-29 18:57:25 +02:00
|
|
|
if (weighted.opt1_w >= 1.0 || weighted.opt1_w <= 0.0) {
|
|
|
|
printf(BOLDRED "Error: weight must be between 0 and 1\n" RESET);
|
|
|
|
goto w_end;
|
|
|
|
}
|
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
printf(BOLDWHITE "Using %g for %s" RESET "\n",
|
|
|
|
weighted.opt1_w, weighted.opt1);
|
2024-08-28 22:15:59 +02:00
|
|
|
|
2024-08-29 18:57:25 +02:00
|
|
|
left -= weighted.opt1_w;
|
|
|
|
|
|
|
|
/* weight 2 */
|
2024-08-29 20:00:12 +02:00
|
|
|
printf(BOLDWHITE "Weight for %s\n" BOLDGREEN "(%g) > " RESET, weighted.opt2, left);
|
2024-08-28 22:15:59 +02:00
|
|
|
fgets(buf, 16, stdin);
|
2024-08-28 23:21:31 +02:00
|
|
|
weighted.opt2_w = strtof(buf, &ptr);
|
2024-08-29 18:57:25 +02:00
|
|
|
if (weighted.opt2_w >= 1.0 || weighted.opt2_w <= 0.0) {
|
|
|
|
printf(BOLDRED "Error: weight must be between 0 and 1\n" RESET);
|
|
|
|
goto w_end;
|
|
|
|
}
|
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
printf(BOLDWHITE "Using %g for %s" RESET "\n",
|
|
|
|
weighted.opt2_w, weighted.opt2);
|
2024-08-28 22:15:59 +02:00
|
|
|
|
2024-08-29 18:57:25 +02:00
|
|
|
left -= weighted.opt2_w;
|
|
|
|
|
|
|
|
/* weight 3 */
|
2024-08-29 20:00:12 +02:00
|
|
|
weighted.opt3_w = left;
|
|
|
|
printf(BOLDWHITE "Using %g for %s" RESET "\n",
|
|
|
|
weighted.opt3_w, weighted.opt3);
|
2024-08-29 18:57:25 +02:00
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
// copy memory before ranking
|
|
|
|
int space = sizeof(struct Rank) * items->rank_count;
|
|
|
|
|
2024-08-30 19:30:56 +02:00
|
|
|
printf("\n\n" BOLDWHITE "**Choose items to rank!**" "\n");
|
|
|
|
mem->rl.rank_count = rsstdin(mem->rl.rank);
|
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
// manual copy with for loop
|
2024-09-03 18:27:20 +02:00
|
|
|
for (int i = 0; i < mem->rl.rank_count; i++) {
|
2024-08-30 19:30:56 +02:00
|
|
|
strncpy(mem->rl.srank[0][i].name, mem->rl.rank[i], M_STR_LEN);
|
2024-08-28 22:15:59 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 20:00:12 +02:00
|
|
|
memcpy(mem->rl.srank[1], mem->rl.srank[0], space);
|
|
|
|
memcpy(mem->rl.srank[2], mem->rl.srank[1], space);
|
|
|
|
|
|
|
|
|
2024-08-28 22:15:59 +02:00
|
|
|
printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt1);
|
2024-08-29 00:28:53 +02:00
|
|
|
rank(mem->rl.srank[0], mem->rl.rank_count);
|
2024-08-28 22:15:59 +02:00
|
|
|
printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt2);
|
2024-08-29 00:28:53 +02:00
|
|
|
rank(mem->rl.srank[1], mem->rl.rank_count);
|
2024-08-28 22:15:59 +02:00
|
|
|
printf(BOLDRED "\nRank according to %s\n\n" RESET, weighted.opt3);
|
2024-08-29 00:28:53 +02:00
|
|
|
rank(mem->rl.srank[2], mem->rl.rank_count);
|
2024-08-28 22:15:59 +02:00
|
|
|
|
2024-08-29 18:57:25 +02:00
|
|
|
printf("\n\n" BOLDWHITE "**FINAL RANKING**" "\n"
|
2024-08-29 20:00:12 +02:00
|
|
|
BOLDYELLOW "name | "
|
|
|
|
"%s " BOLDGREEN "(%g)" BOLDWHITE " | "
|
|
|
|
"%s " BOLDGREEN "(%g)" BOLDWHITE " | "
|
|
|
|
"%s " BOLDGREEN "(%g)" BOLDWHITE " | "
|
2024-08-30 19:30:56 +02:00
|
|
|
"total " RESET "\n",
|
2024-08-29 18:57:25 +02:00
|
|
|
weighted.opt1, weighted.opt1_w,
|
|
|
|
weighted.opt2, weighted.opt2_w,
|
|
|
|
weighted.opt3, weighted.opt3_w);
|
2024-08-29 20:00:12 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < mem->rl.rank_count; i++) {
|
|
|
|
// garbage dot product T_T
|
2024-08-30 19:30:56 +02:00
|
|
|
float total =
|
|
|
|
((double) mem->rl.srank[0][i].score) * weighted.opt1_w +
|
|
|
|
((double) mem->rl.srank[1][i].score) * weighted.opt2_w +
|
|
|
|
((double) mem->rl.srank[2][i].score) * weighted.opt3_w;
|
2024-08-29 20:00:12 +02:00
|
|
|
|
|
|
|
printf(BOLDWHITE "%s | "
|
2024-08-30 19:30:56 +02:00
|
|
|
BOLDGREEN "%d*%g " BOLDWHITE "| "
|
|
|
|
BOLDGREEN "%d*%g " BOLDWHITE "| "
|
|
|
|
BOLDGREEN "%d*%g " BOLDWHITE " |"
|
|
|
|
BOLDGREEN "%g " RESET "\n",
|
2024-08-29 20:00:12 +02:00
|
|
|
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);
|
|
|
|
}
|
2024-08-28 22:15:59 +02:00
|
|
|
w_end:
|
|
|
|
free(mem);
|
2024-08-28 00:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(items);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
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 = to_sort[i].score;
|
|
|
|
|
|
|
|
char str_key[M_STR_LEN];
|
|
|
|
strncpy(str_key, to_sort[i].name, M_STR_LEN);
|
|
|
|
|
|
|
|
int j = i - 1;
|
|
|
|
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
to_sort[++j].score = key;
|
|
|
|
strncpy(to_sort[j].name, str_key, M_STR_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2024-08-27 21:58:35 +02:00
|
|
|
|
2024-08-28 23:21:31 +02:00
|
|
|
int
|
2024-08-29 17:57:27 +02:00
|
|
|
rsstdin(char items[M_STR][M_STR_LEN])
|
2024-08-29 00:28:53 +02:00
|
|
|
{
|
2024-08-29 17:57:27 +02:00
|
|
|
puts(BOLDWHITE "Enter items..." RESET);
|
2024-08-27 21:58:35 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < M_STR; i++) {
|
|
|
|
// local buffer for name
|
2024-08-29 17:57:27 +02:00
|
|
|
printf(BOLDGREEN "> " RESET);
|
|
|
|
fgets(items[i], M_STR_LEN, stdin);
|
2024-08-27 21:58:35 +02:00
|
|
|
|
|
|
|
// if its an empty string, stop reading
|
2024-08-29 17:57:27 +02:00
|
|
|
if (items[i][0] == '\n') {
|
|
|
|
items[i][0] = '\0';
|
2024-08-27 21:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove newline
|
2024-08-29 17:57:27 +02:00
|
|
|
char nl = strcspn(items[i], "\n");
|
|
|
|
if (items[i][nl] != '\n' && items[i][nl] != 0) {
|
2024-08-28 23:21:31 +02:00
|
|
|
// it was cut off
|
|
|
|
puts(BOLDRED "Truncated to 255 chars." RESET);
|
2024-08-29 17:57:27 +02:00
|
|
|
items[i][nl] = '\0';
|
2024-08-28 23:21:31 +02:00
|
|
|
}
|
2024-08-29 17:57:27 +02:00
|
|
|
items[i][nl] = '\0';
|
2024-08-27 21:58:35 +02:00
|
|
|
}
|
2024-08-29 00:28:53 +02:00
|
|
|
|
|
|
|
return i;
|
2024-08-28 23:21:31 +02:00
|
|
|
}
|
2024-08-27 21:58:35 +02:00
|
|
|
|
2024-08-28 23:21:31 +02:00
|
|
|
void
|
2024-08-29 00:28:53 +02:00
|
|
|
rank(struct Rank *items, int i)
|
|
|
|
{
|
2024-08-27 21:58:35 +02:00
|
|
|
// construct matrix of comparisons
|
|
|
|
// NOTE: it doesn't actually store the "reason",
|
|
|
|
// it just forces the user to type something out lol
|
2024-08-28 00:12:46 +02:00
|
|
|
char matrix[M_STR][M_STR];
|
2024-08-27 21:58:35 +02:00
|
|
|
|
2024-08-28 00:12:46 +02:00
|
|
|
// memset(3) with null bytes
|
2024-08-27 21:58:35 +02:00
|
|
|
memset(matrix, 0, sizeof(matrix));
|
|
|
|
|
|
|
|
// compare all of the elements
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
for (int k = j + 1; k < i; k++) {
|
2024-08-29 00:28:53 +02:00
|
|
|
printf("%s vs %s (>/<): ", items[j].name, items[k].name);
|
2024-08-27 21:58:35 +02:00
|
|
|
// read one char, set matrix accordingly
|
|
|
|
char c = getchar();
|
|
|
|
if (c == '>') {
|
|
|
|
matrix[j][k]++;
|
2024-08-29 00:28:53 +02:00
|
|
|
items[j].score++;
|
2024-08-27 21:58:35 +02:00
|
|
|
} else if (c == '<') {
|
|
|
|
matrix[k][j]++;
|
2024-08-29 00:28:53 +02:00
|
|
|
items[k].score++;
|
2024-08-27 21:58:35 +02:00
|
|
|
} else {
|
|
|
|
printf("Setting to " BOLDRED "<" RESET ": %c\n", c);
|
|
|
|
matrix[k][j]++;
|
2024-08-29 00:28:53 +02:00
|
|
|
items[k].score++;
|
2024-08-27 21:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear input buffer
|
|
|
|
while (getchar() != '\n');
|
|
|
|
|
|
|
|
// ask the user to explain their choice; but we don't care what
|
|
|
|
// they say
|
|
|
|
printf(BOLDWHITE "Explain: " RESET);
|
|
|
|
|
|
|
|
while (getchar() != '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// insertion sort: the array **should be** nearly sorted
|
2024-08-29 00:28:53 +02:00
|
|
|
isort(matrix, items, i);
|
2024-08-28 00:12:46 +02:00
|
|
|
}
|
2024-08-27 21:58:35 +02:00
|
|
|
|
2024-08-28 00:12:46 +02:00
|
|
|
void
|
|
|
|
print_ranklist(struct RankList *items) {
|
2024-08-27 21:58:35 +02:00
|
|
|
printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n");
|
2024-08-28 00:12:46 +02:00
|
|
|
for (int j = 0; j < items->rank_count; j++) {
|
2024-08-27 21:58:35 +02:00
|
|
|
printf("%s: %d | %s: %d\n",
|
2024-08-29 17:57:27 +02:00
|
|
|
items->rank[j], items->rank_count - j - 1,
|
2024-08-28 23:21:31 +02:00
|
|
|
items->srank[j].name, items->srank[j].score);
|
2024-08-27 21:58:35 +02:00
|
|
|
}
|
2024-08-28 00:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void) {
|
|
|
|
puts(BOLDWHITE "Usage: weightedrank [options]\n"
|
|
|
|
"Options:\n" RESET
|
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
" -n, --no-color disable colors\n"
|
|
|
|
" -w, --weighted use weighted rankings\n"
|
|
|
|
" -l, --log-file FILE write log to FILE\n");
|
2024-08-28 23:21:31 +02:00
|
|
|
|
|
|
|
exit(0);
|
2024-08-27 21:58:35 +02:00
|
|
|
}
|