commit 2e899f840e850d1f6fc442e9759e4a952e0ee682 Author: Xamora Date: Sun Dec 1 05:52:45 2024 +0100 starting diff --git a/01/_.h b/01/_.h new file mode 100644 index 0000000..61fdba0 --- /dev/null +++ b/01/_.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + +char** str_split(char* a_str, const char a_delim); + +int str_index(char *str, char c); + +int number_of_element(char *str, char c); + +long gmc(long a, long b); +long lmc(long a, long b); +long lmcs(int array[], int n); diff --git a/01/build.sh b/01/build.sh new file mode 100755 index 0000000..b115098 --- /dev/null +++ b/01/build.sh @@ -0,0 +1 @@ +gcc -g -Wall -Wextra -include_.h *.c diff --git a/01/main.c b/01/main.c new file mode 100644 index 0000000..c3c8116 --- /dev/null +++ b/01/main.c @@ -0,0 +1,16 @@ +#include "_.h" + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + for (int i = 0; inputs[i] != NULL; i++) { + + } + + return 0; +} diff --git a/01/main.txt b/01/main.txt new file mode 100644 index 0000000..e69de29 diff --git a/01/mylib.c b/01/mylib.c new file mode 100644 index 0000000..a8a4205 --- /dev/null +++ b/01/mylib.c @@ -0,0 +1,84 @@ +#include "_.h" + +// NOT MY CODE +// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c +char** str_split(char* str, const char a_delim) +{ + char *a_str = strdup(str); + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + count += last_comma < (a_str + strlen(a_str) - 1); + + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + free(a_str); + return result; +} + +int str_index(char *str, char c) { + + int i = 0; + while (str[i]) + if (str[i++] == c) + return i; + + return -1; +} + +int number_of_element(char *str, char c) { + int nbr = 0; + for (int i = 0; str[i] != '\0'; i++) + if (str[i] == c) + nbr++; + return nbr; +} + +long gmc(long a, long b) { + if (b == 0) + return a; + return gmc(b, a % b); +} + +long lmc(long a, long b) { + return (a / gmc(a, b)) * b; +} + +long lmcs(int array[], int n) { + long result = array[0]; + for (int i = 1; i < n; i++) + result = lmc(result, array[i]); + return result; +} diff --git a/01/test.txt b/01/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..edfa93a --- /dev/null +++ b/start.sh @@ -0,0 +1,5 @@ +#!/bin/sh +test "$1" == "" && exit 1 +cp -r template "$1" || exit 1 +cd "$1" || exit 1 +chmod +x *.sh || exit 1 diff --git a/template/_.h b/template/_.h new file mode 100644 index 0000000..61fdba0 --- /dev/null +++ b/template/_.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + +char** str_split(char* a_str, const char a_delim); + +int str_index(char *str, char c); + +int number_of_element(char *str, char c); + +long gmc(long a, long b); +long lmc(long a, long b); +long lmcs(int array[], int n); diff --git a/template/build.sh b/template/build.sh new file mode 100644 index 0000000..b115098 --- /dev/null +++ b/template/build.sh @@ -0,0 +1 @@ +gcc -g -Wall -Wextra -include_.h *.c diff --git a/template/main.c b/template/main.c new file mode 100644 index 0000000..c3c8116 --- /dev/null +++ b/template/main.c @@ -0,0 +1,16 @@ +#include "_.h" + +int main(int argc, char **argv) { + + if (argc != 2) + return 1; + + char *input = argv[1]; + char **inputs = str_split(strdup(input), '\n'); + + for (int i = 0; inputs[i] != NULL; i++) { + + } + + return 0; +} diff --git a/template/main.txt b/template/main.txt new file mode 100644 index 0000000..e69de29 diff --git a/template/mylib.c b/template/mylib.c new file mode 100644 index 0000000..a8a4205 --- /dev/null +++ b/template/mylib.c @@ -0,0 +1,84 @@ +#include "_.h" + +// NOT MY CODE +// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c +char** str_split(char* str, const char a_delim) +{ + char *a_str = strdup(str); + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + count += last_comma < (a_str + strlen(a_str) - 1); + + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + free(a_str); + return result; +} + +int str_index(char *str, char c) { + + int i = 0; + while (str[i]) + if (str[i++] == c) + return i; + + return -1; +} + +int number_of_element(char *str, char c) { + int nbr = 0; + for (int i = 0; str[i] != '\0'; i++) + if (str[i] == c) + nbr++; + return nbr; +} + +long gmc(long a, long b) { + if (b == 0) + return a; + return gmc(b, a % b); +} + +long lmc(long a, long b) { + return (a / gmc(a, b)) * b; +} + +long lmcs(int array[], int n) { + long result = array[0]; + for (int i = 1; i < n; i++) + result = lmc(result, array[i]); + return result; +} diff --git a/template/test.txt b/template/test.txt new file mode 100644 index 0000000..e69de29