added stub for ranking implementation

This commit is contained in:
Om Raheja 2024-08-26 13:26:05 -04:00
parent 34ceed6323
commit f41f1b9862
3 changed files with 116 additions and 0 deletions

View File

@ -1,2 +1,18 @@
# critical-thinking
On August 25, 2024 I finished reading "The Thinker's Toolkit" by Morgan D. Jones.
Little did I realize it when I finished the book, but the techniques I learned and practiced while reading the book were so powerful that I ended up using them all the time.
All. The. Time.
Critical thinking and problem solving is a **very powerful** skill. Unfortunately, it is not taught to us.
If you want to use some of the tools he taught us about, you can use my software to make it easier to understand and use. Especially laborious processes like ranking.
My code is public domain, licensed under the [0BSD](https://spdx.org/licenses/0BSD.html) license.
Book copyright (c) 1995, 1998 Morgan Jones
## Install
tbd

22
color.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef COLOR_H
#define COLOR_H
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#endif

78
rank.c Normal file
View File

@ -0,0 +1,78 @@
/*
* 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"
#define M_STR 255
int
main(void)
{
// maximum M_STR strings, 255 bytes each
char strings[M_STR][255] = { 0 };
// read strings
printf(BOLDWHITE "Enter strings: (empty line when done) \n" RESET);
int i;
for (i = 0; i < M_STR; i++) {
fgets(strings[i], 255, stdin);
// 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';
}
// 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];
printf("%d: %d\n", i, sizeof(winners));
// 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 == '>') {
matrix[j][k] = 1;
winners[j]++;
} else if (c == '<') {
matrix[j][k] = 0;
winners[k]++;
} else {
printf("Setting to " BOLDRED "<" RESET ": %c\n", c);
matrix[j][k] = 0;
}
}
}
// print out the results
for (int j = 0; j < i; j++) {
printf("%s: %d\n", strings[j], winners[j]);
}
return 0;
}