made it work for the most part

This commit is contained in:
Om Raheja 2024-08-29 11:57:27 -04:00
parent 9281747d54
commit 179c9e125d
3 changed files with 17 additions and 17 deletions

7
rank.h
View File

@ -13,14 +13,14 @@ struct Rank
struct RankList struct RankList
{ {
int rank_count; int rank_count;
struct Rank rank[M_STR]; char rank[M_STR][M_STR_LEN];
struct Rank srank[M_STR]; struct Rank srank[M_STR];
}; };
struct WRankList struct WRankList
{ {
int rank_count; int rank_count;
struct Rank rank[M_STR]; char rank[M_STR][M_STR_LEN];
struct Rank srank[3][M_STR]; struct Rank srank[3][M_STR];
}; };
@ -46,6 +46,7 @@ void isort(char [][M_STR], struct Rank[], int);
void rank(struct Rank *, int); void rank(struct Rank *, int);
void print_ranklist(struct RankList *); void print_ranklist(struct RankList *);
int rrlstdin(struct Rank[]); /* read string from stdin */
int rsstdin(char [][M_STR_LEN]);
#endif #endif

Binary file not shown.

View File

@ -58,7 +58,8 @@ main(int argc, char *argv[])
} }
} }
items->rank_count = rrlstdin(items->rank); items->rank_count = rsstdin(items->rank);
memcpy(items->srank, items->rank, sizeof(struct Rank) * items->rank_count);
rank(items->srank, items->rank_count); rank(items->srank, items->rank_count);
if (!weighted) { if (!weighted) {
@ -153,31 +154,29 @@ isort(char matrix[][M_STR], struct Rank to_sort[], int len) {
} }
int int
rrlstdin(struct Rank items[]) rsstdin(char items[M_STR][M_STR_LEN])
{ {
printf(BOLDWHITE "Enter items...\n" RESET); puts(BOLDWHITE "Enter items..." RESET);
int i; int i;
for (i = 0; i < M_STR; i++) { for (i = 0; i < M_STR; i++) {
// local buffer for name // local buffer for name
char *name = items[i].name; printf(BOLDGREEN "> " RESET);
fgets(items[i], M_STR_LEN, stdin);
puts(BOLDGREEN "> " RESET);
fgets(name, M_STR_LEN, stdin);
// if its an empty string, stop reading // if its an empty string, stop reading
if (name[0] == '\n') { if (items[i][0] == '\n') {
name[0] = '\0'; items[i][0] = '\0';
break; break;
} }
// remove newline // remove newline
char nl = strcspn(name, "\n"); char nl = strcspn(items[i], "\n");
if (nl != '\n' || nl != 0) { if (items[i][nl] != '\n' && items[i][nl] != 0) {
// it was cut off // it was cut off
puts(BOLDRED "Truncated to 255 chars." RESET); puts(BOLDRED "Truncated to 255 chars." RESET);
name[nl] = '\0'; items[i][nl] = '\0';
} }
name[nl] = '\0'; items[i][nl] = '\0';
} }
return i; return i;
@ -232,7 +231,7 @@ print_ranklist(struct RankList *items) {
printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n"); printf(BOLDRED "Original: " RESET " | " BOLDGREEN "Sorted: " RESET "\n\n");
for (int j = 0; j < items->rank_count; j++) { for (int j = 0; j < items->rank_count; j++) {
printf("%s: %d | %s: %d\n", printf("%s: %d | %s: %d\n",
items->rank[j].name, items->rank[j].score, items->rank[j], items->rank_count - j - 1,
items->srank[j].name, items->srank[j].score); items->srank[j].name, items->srank[j].score);
} }
} }