2024-08-26 19:26:05 +02:00
|
|
|
/*
|
|
|
|
* Ranking tool in "The Thinker's Toolkit" by Morgan Jones
|
|
|
|
* Book copyright (c) 1995, 1998 Morgan Jones
|
|
|
|
* This file is public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "color.h"
|
|
|
|
|
2024-08-26 21:17:01 +02:00
|
|
|
#define M_STR 255
|
|
|
|
#define M_STR_LEN 255
|
2024-08-26 19:26:05 +02:00
|
|
|
|
2024-08-26 22:21:27 +02:00
|
|
|
void isort(char [][M_STR], char [][M_STR_LEN], int *, int);
|
2024-08-26 19:47:15 +02:00
|
|
|
|
2024-08-26 19:26:05 +02:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
// maximum M_STR strings, 255 bytes each
|
2024-08-26 21:17:01 +02:00
|
|
|
char strings[M_STR][M_STR_LEN] = { 0 };
|
2024-08-26 19:26:05 +02:00
|
|
|
|
|
|
|
// read strings
|
2024-08-26 19:47:15 +02:00
|
|
|
printf(BOLDWHITE "Enter a ranking (empty line when done) \n" RESET);
|
2024-08-26 19:26:05 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < M_STR; i++) {
|
2024-08-26 21:17:01 +02:00
|
|
|
fgets(strings[i], M_STR_LEN, stdin);
|
|
|
|
|
2024-08-26 19:26:05 +02:00
|
|
|
// if its an empty string, stop reading
|
|
|
|
if (strings[i][0] == '\n') {
|
|
|
|
strings[i][0] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove newline
|
|
|
|
strings[i][strcspn(strings[i], "\n")] = '\0';
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:17:01 +02:00
|
|
|
// init sorted strings
|
2024-08-26 22:21:27 +02:00
|
|
|
char sorted_strings[i][M_STR_LEN];
|
2024-08-26 21:17:01 +02:00
|
|
|
memcpy(sorted_strings, strings, sizeof(sorted_strings));
|
|
|
|
|
2024-08-26 19:26:05 +02:00
|
|
|
// construct matrix of comparisons
|
|
|
|
// NOTE: it doesn't actually store the "reason",
|
|
|
|
// it just forces the user to type something out lol
|
|
|
|
char matrix[i][i];
|
|
|
|
|
|
|
|
// memcpy(3) with null bytes
|
|
|
|
memset(matrix, 0, sizeof(matrix));
|
|
|
|
|
|
|
|
// store the winners
|
|
|
|
int winners[i];
|
2024-08-26 19:47:15 +02:00
|
|
|
int sorted_winners[i];
|
2024-08-26 19:26:05 +02:00
|
|
|
|
|
|
|
// 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]);
|
|
|
|
// read one char, set matrix accordingly
|
|
|
|
char c = getchar();
|
|
|
|
if (c == '>') {
|
2024-08-26 22:21:27 +02:00
|
|
|
matrix[j][k]++;
|
2024-08-26 19:26:05 +02:00
|
|
|
winners[j]++;
|
|
|
|
} else if (c == '<') {
|
2024-08-26 22:21:27 +02:00
|
|
|
matrix[k][j]++;
|
2024-08-26 19:26:05 +02:00
|
|
|
winners[k]++;
|
|
|
|
} else {
|
|
|
|
printf("Setting to " BOLDRED "<" RESET ": %c\n", c);
|
2024-08-26 22:21:27 +02:00
|
|
|
matrix[k][j]++;
|
2024-08-26 19:47:15 +02:00
|
|
|
winners[k]++;
|
2024-08-26 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 19:47:15 +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');
|
2024-08-26 19:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 22:21:27 +02:00
|
|
|
memcpy(sorted_winners, winners, sizeof(sorted_winners));
|
2024-08-26 21:17:01 +02:00
|
|
|
|
2024-08-26 19:47:15 +02:00
|
|
|
// insertion sort: the array **should be** nearly sorted
|
2024-08-26 22:21:27 +02:00
|
|
|
isort(matrix, sorted_strings, sorted_winners, i);
|
2024-08-26 19:47:15 +02:00
|
|
|
|
2024-08-26 23:49:28 +02:00
|
|
|
printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n");
|
2024-08-26 19:26:05 +02:00
|
|
|
for (int j = 0; j < i; j++) {
|
2024-08-26 23:49:28 +02:00
|
|
|
printf("%s: %d | %s: %d\n",
|
|
|
|
strings[j], winners[j],
|
|
|
|
sorted_strings[j], sorted_winners[j]);
|
2024-08-26 19:26:05 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-08-26 19:47:15 +02:00
|
|
|
|
|
|
|
void
|
2024-08-26 22:21:27 +02:00
|
|
|
isort(char matrix[][M_STR], char name[][M_STR_LEN], int *value, int len) {
|
2024-08-26 19:47:15 +02:00
|
|
|
// sort by value, modify key accordingly
|
2024-08-26 21:17:01 +02:00
|
|
|
for (int i = 1; i < len; i++) {
|
|
|
|
int key = value[i];
|
2024-08-26 22:21:27 +02:00
|
|
|
|
|
|
|
char str_key[M_STR_LEN];
|
|
|
|
strncpy(str_key, name[i], M_STR_LEN);
|
2024-08-26 21:17:01 +02:00
|
|
|
|
|
|
|
int j = i - 1;
|
|
|
|
|
2024-08-26 23:49:28 +02:00
|
|
|
while (j >= 0 && (value[j] < key || matrix[i][j]) ) {
|
2024-08-26 22:21:27 +02:00
|
|
|
value[j + 1] = value[j];
|
|
|
|
strncpy(name[j + 1], name[j], M_STR_LEN);
|
2024-08-26 23:49:28 +02:00
|
|
|
|
2024-08-26 21:17:01 +02:00
|
|
|
j--;
|
|
|
|
}
|
|
|
|
value[++j] = key;
|
2024-08-26 23:49:28 +02:00
|
|
|
strncpy(name[j], str_key, M_STR_LEN);
|
2024-08-26 21:17:01 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 19:47:15 +02:00
|
|
|
return;
|
|
|
|
}
|