init
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
*.html
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int *division(int input, int by)
|
||||
{
|
||||
int value = input / by;
|
||||
int *ptr = &value;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int *ptr1 = division(42, 2);
|
||||
printf("the 1st value is %d\n", *ptr1);
|
||||
int *ptr2 = division(12, 0);
|
||||
printf("the 2nd value is %d\n", *ptr2);
|
||||
free(ptr1);
|
||||
free(ptr2);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int **allocate_matrix(int rows, int cols)
|
||||
{
|
||||
int **matrix = malloc(rows * sizeof(int *));
|
||||
if (!matrix)
|
||||
return NULL;
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
matrix[i] = malloc(cols * sizeof(int));
|
||||
if (!matrix[i])
|
||||
{
|
||||
for (int j = 0; j < i; j++)
|
||||
free(matrix[j]);
|
||||
free(matrix);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void free_matrix(int **matrix, int rows)
|
||||
{
|
||||
if (!matrix)
|
||||
return;
|
||||
free(matrix);
|
||||
return;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int rows = 5;
|
||||
int cols = 5;
|
||||
int **matrix = allocate_matrix(rows, cols);
|
||||
if (!matrix)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols; j++)
|
||||
{
|
||||
matrix[i][j] = i * cols + j;
|
||||
}
|
||||
}
|
||||
|
||||
free_matrix(matrix, rows);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct pair_list
|
||||
{
|
||||
const char *key;
|
||||
char *value;
|
||||
struct pair_list *next;
|
||||
};
|
||||
|
||||
struct hash_map
|
||||
{
|
||||
struct pair_list **data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
size_t hash(const char *key)
|
||||
{
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
uint32_t hash = 2166136261;
|
||||
uint32_t prime = 16777619;
|
||||
|
||||
while (*key)
|
||||
{
|
||||
hash ^= *key;
|
||||
hash *= prime;
|
||||
key++;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
struct hash_map *hash_map_init(size_t size)
|
||||
{
|
||||
struct hash_map *res = malloc(sizeof(struct hash_map));
|
||||
if (!res)
|
||||
return NULL;
|
||||
res->data = calloc(size, sizeof(struct pair_list *));
|
||||
if (!res->data)
|
||||
{
|
||||
free(res);
|
||||
return NULL;
|
||||
}
|
||||
res->size = size;
|
||||
return res;
|
||||
}
|
||||
|
||||
bool hash_map_insert(struct hash_map *hash_map, const char *key, char *value,
|
||||
bool *updated)
|
||||
{
|
||||
if (!hash_map || hash_map->size == 0 || !key)
|
||||
return false;
|
||||
|
||||
size_t hashed_val = hash(key) % hash_map->size;
|
||||
struct pair_list *elt = hash_map->data[hashed_val];
|
||||
while (elt)
|
||||
{
|
||||
if (strcmp(key, elt->key) == 0)
|
||||
break;
|
||||
elt = elt->next;
|
||||
}
|
||||
if (!elt)
|
||||
{
|
||||
struct pair_list *new = calloc(1, sizeof(struct pair_list));
|
||||
if (!new)
|
||||
return false;
|
||||
new->key = key;
|
||||
new->value = value;
|
||||
struct pair_list *res = hash_map->data[hashed_val];
|
||||
new->next = res;
|
||||
hash_map->data[hashed_val] = new;
|
||||
if (updated)
|
||||
*updated = false;
|
||||
return true;
|
||||
}
|
||||
elt->value = value;
|
||||
if (updated)
|
||||
*updated = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void hash_map_free(struct hash_map *hash_map)
|
||||
{
|
||||
if (!hash_map)
|
||||
return;
|
||||
for (size_t i = 0; i < hash_map->size; i++)
|
||||
{
|
||||
struct pair_list *elt = hash_map->data[i];
|
||||
while (elt)
|
||||
{
|
||||
struct pair_list *n = elt->next;
|
||||
free(elt);
|
||||
elt = n;
|
||||
}
|
||||
}
|
||||
free(hash_map->data);
|
||||
free(hash_map);
|
||||
hash_map->data = NULL;
|
||||
}
|
||||
|
||||
void hash_map_dump(struct hash_map *hash_map)
|
||||
{
|
||||
if (!hash_map)
|
||||
return;
|
||||
for (size_t i = 0; i < hash_map->size; i++)
|
||||
{
|
||||
struct pair_list *elt = hash_map->data[i];
|
||||
while (elt)
|
||||
{
|
||||
printf("%s: %s", elt->key, elt->value);
|
||||
elt = elt->next;
|
||||
if (elt)
|
||||
printf(", ");
|
||||
else
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *hash_map_get(const struct hash_map *hash_map, const char *key)
|
||||
{
|
||||
if (!hash_map || hash_map->size == 0)
|
||||
return NULL;
|
||||
size_t hashed_val = hash(key) % hash_map->size;
|
||||
struct pair_list *elt = hash_map->data[hashed_val];
|
||||
while (elt)
|
||||
{
|
||||
if (strcmp(key, elt->key) == 0)
|
||||
break;
|
||||
elt = elt->next;
|
||||
}
|
||||
if (elt)
|
||||
return elt->value;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool hash_map_remove(struct hash_map *hash_map, const char *key)
|
||||
{
|
||||
if (!hash_map || hash_map->size == 0)
|
||||
return false;
|
||||
size_t hashed_val = hash(key) % hash_map->size;
|
||||
struct pair_list *elt = hash_map->data[hashed_val];
|
||||
struct pair_list *prev = NULL;
|
||||
while (elt)
|
||||
{
|
||||
if (strcmp(key, elt->key) == 0)
|
||||
break;
|
||||
prev = elt;
|
||||
elt = elt->next;
|
||||
}
|
||||
if (!elt)
|
||||
return false;
|
||||
if (prev)
|
||||
prev->next = elt->next;
|
||||
else
|
||||
hash_map->data[hashed_val] = elt->next;
|
||||
free(elt);
|
||||
printf("removed %s", elt->value);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
if (!map)
|
||||
return 1;
|
||||
|
||||
bool updated;
|
||||
hash_map_insert(map, "key1", "value1", &updated);
|
||||
hash_map_insert(map, "key2", "value2", &updated);
|
||||
hash_map_insert(map, "key1", "new_value1", &updated);
|
||||
|
||||
hash_map_dump(map);
|
||||
|
||||
const char *val = hash_map_get(map, "key1");
|
||||
if (val)
|
||||
printf("Retrieved key1: %s\n", val);
|
||||
|
||||
hash_map_remove(map, "key2");
|
||||
hash_map_dump(map);
|
||||
|
||||
hash_map_free(map);
|
||||
|
||||
printf("Everything is ok!\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char shift(char c, int by)
|
||||
{
|
||||
int start = 0;
|
||||
if(c >= 'a' && c <= 'z')
|
||||
{
|
||||
start = 'a';
|
||||
}
|
||||
else if(c >= 'A' && c <= 'Z')
|
||||
{
|
||||
start = 'A';
|
||||
}
|
||||
else return c;
|
||||
c -= start;
|
||||
c += by;
|
||||
c %= 26;
|
||||
return c + start;
|
||||
}
|
||||
|
||||
char *reverse_shifted(char *input, int by)
|
||||
{
|
||||
if(!input)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int len = strlen(input);
|
||||
char *res = calloc(len, sizeof(char));
|
||||
if(!res)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int start = 0;
|
||||
int end = len + 1;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
char val = input[end--];
|
||||
val = shift(val, by);
|
||||
res[start++] = val;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *test1 = "!qxsbdc k cs csrd";
|
||||
char *res1 = reverse_shifted(test1, 42);
|
||||
if(!res1)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
puts(res1);
|
||||
free(res1);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
vous connaissez le café monster redbull ?
|
||||
Perso je ne recommande pas, c'est pas bon
|
||||
Sinon il y a le coca monster (blanche)
|
||||
crèpes à la redbull c'est pas mauvais, faut mettre un peu plus de rhum dedans par contre 1/2 bouteille
|
||||
Il y a aussi le Holy pomme verte Redbull pour un max de caffeine
|
||||
|
||||
Le truc de fou qui est vraiment bien c'est l'eau, c'est gratuit, pas trop mauvais mais je deconseille, ça n'a pas de goût
|
||||
Reference in New Issue
Block a user