From 4838718a581e061ca2f5b2dbb4b82d5317b0e278 Mon Sep 17 00:00:00 2001 From: celso Date: Fri, 6 Jan 2023 00:52:30 -0300 Subject: [PATCH] added interface and renamed functions --- src/skomb.c | 8 ++++---- src/skomb.h | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/skomb.c b/src/skomb.c index 63e3f1b..46dfba6 100644 --- a/src/skomb.c +++ b/src/skomb.c @@ -44,7 +44,7 @@ int main(int argc, char **argv){ case 2: if (number >= 3 && number <= 17){ pair *pairs = malloc(sizeof(pair) * 4); - int len = perm2(number, not_allowed, not_allowed_len, pairs); + int len = skomb2(number, not_allowed, not_allowed_len, pairs); printf("%d:\n", number); for (int i = 0; i < len; i++){ printf("\t%d + %d\n", pairs[i].x, pairs[i].y); @@ -59,7 +59,7 @@ int main(int argc, char **argv){ case 3: if (number >= 6 && number <= 24){ trio *trios = malloc(sizeof(trio) * 9); - int len = perm3(number, not_allowed, not_allowed_len, trios); + int len = skomb3(number, not_allowed, not_allowed_len, trios); printf("%d:\n", number); for (int i = 0; i < len; i++){ printf("\t%d + %d + %d\n", trios[i].x, trios[i].y, trios[i].z); @@ -74,7 +74,7 @@ int main(int argc, char **argv){ case 4: if (number >= 10 && number <= 30){ quartet *quartets = malloc(sizeof(quartet) * 12); - int len = perm4(number, not_allowed, not_allowed_len, quartets); + int len = skomb4(number, not_allowed, not_allowed_len, quartets); printf("%d:\n", number); for (int i = 0; i < len; i++){ printf("\t%d + %d + %d + %d\n", quartets[i].x, quartets[i].y, quartets[i].z, quartets[i].w); @@ -89,7 +89,7 @@ int main(int argc, char **argv){ case 5: if (number >= 15 && number <= 35){ quintet *quintets = malloc(sizeof(quintet) * 12); - int len = perm5(number, not_allowed, not_allowed_len, quintets); + int len = skomb5(number, not_allowed, not_allowed_len, quintets); printf("%d:\n", number); for (int i = 0; i < len; i++){ printf("\t%d + %d + %d + %d + %d\n", quintets[i].x, quintets[i].y, quintets[i].z, quintets[i].w, quintets[i].v); diff --git a/src/skomb.h b/src/skomb.h index 795eb86..a506c51 100644 --- a/src/skomb.h +++ b/src/skomb.h @@ -42,12 +42,12 @@ typedef struct quintet{ int v; } quintet; -void readline(char *line, int len); -int splitc_toi(char *line, int sep, int *dest); -int perm2(int number, int *not_allowed, int* not_allowed_len, pair *pairs); -int perm3(int number, int *not_allowed, int* not_allowed_len, trio *trios); -int perm4(int number, int *not_allowed, int* not_allowed_len, quartet *quartets); -int perm5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets); +void readline(char *line, int len); /* read len ammount of characters from stdin into line */ +int splitc_toi(char *line, int sep, int *dest); /* split a char array of numbers into an int array, separator used between numbes must be provided in the function call (e.g. commas, spaces, etc.) */ +int skomb2(int number, int *not_allowed, int* not_allowed_len, pair *pairs); /* stores valid combinations that sum up to number respecting an int array of not allowed numbers with it's length in a pair array */ +int skomb3(int number, int *not_allowed, int* not_allowed_len, trio *trios); /* same as above but with trios instead */ +int skomb4(int number, int *not_allowed, int* not_allowed_len, quartet *quartets); /* same as above but with quartets instead */ +int skomb5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets); /* same as above but with quintets instead */ void readline(char *line, int len){ int i = 0; @@ -77,7 +77,7 @@ int splitc_toi(char *line, int sep, int *dest){ return j; } -int perm2(int number, int* not_allowed, int* not_allowed_len, pair* dest){ +int skomb2(int number, int* not_allowed, int* not_allowed_len, pair* dest){ int i, j, k; int skip; k = 0; @@ -101,7 +101,7 @@ int perm2(int number, int* not_allowed, int* not_allowed_len, pair* dest){ return k; } -int perm3(int number, int *not_allowed, int *not_allowed_len, trio *trios){ +int skomb3(int number, int *not_allowed, int *not_allowed_len, trio *trios){ int i, j, k; int skip, len; k = 0; @@ -121,7 +121,7 @@ int perm3(int number, int *not_allowed, int *not_allowed_len, trio *trios){ && i < number){ not_allowed[*not_allowed_len] = i; (*not_allowed_len)++; - len = perm2(number-i, not_allowed, not_allowed_len, pairs); + len = skomb2(number-i, not_allowed, not_allowed_len, pairs); if (len > 0){ for (j = 0; j < len; j++){ if (i < pairs[j].x && i < pairs[j].y){ @@ -140,7 +140,7 @@ int perm3(int number, int *not_allowed, int *not_allowed_len, trio *trios){ return k; } -int perm4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets){ +int skomb4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets){ int i, j, k; int skip, len; k = 0; @@ -159,7 +159,7 @@ int perm4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets) if (i != number && number - i < 25 && number - i > 5){ not_allowed[*not_allowed_len] = i; (*not_allowed_len)++; - len = perm3(number - i, not_allowed, not_allowed_len, trios); + len = skomb3(number - i, not_allowed, not_allowed_len, trios); if (len > 0){ for (j = 0; j < len; j++){ if (i < trios[j].x && i < trios[j].y && i < trios[j].z){ @@ -179,7 +179,7 @@ int perm4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets) return k; } -int perm5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets){ +int skomb5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets){ int i, j, k; int skip, len; k = 0; @@ -198,7 +198,7 @@ int perm5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets) if (i != number && number - i < 31 && number - i > 9){ not_allowed[*not_allowed_len] = i; (*not_allowed_len)++; - len = perm4(number-i, not_allowed, not_allowed_len, quartets); + len = skomb4(number-i, not_allowed, not_allowed_len, quartets); if (len > 0){ for (j = 0; j < len; j++){ if (i < quartets[j].x && i < quartets[j].y && i < quartets[j].z && i < quartets[j].w){