C: allergies

This commit is contained in:
2021-08-26 15:05:12 +02:00
parent 5a35db2396
commit 29905d7894
5 changed files with 216 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
#include "allergies.h"
bool is_allergic_to(const allergen_t all, const uint32_t val)
{
return !!(val & (1 << all));
}
allergen_list_t get_allergens(uint32_t val)
{
allergen_list_t list = { .count = 0 };
allergen_t i;
for (i=0; i<ALLERGEN_COUNT; ++i) {
list.allergens[i] = is_allergic_to(i, val);
list.count += list.allergens[i];
}
return list;
}

View File

@@ -0,0 +1,42 @@
#ifndef ALLERGIES_H
#define ALLERGIES_H
#include <stdbool.h>
#include <stdint.h>
typedef enum {
ALLERGEN_EGGS = 0,
ALLERGEN_PEANUTS,
ALLERGEN_SHELLFISH,
ALLERGEN_STRAWBERRIES,
ALLERGEN_TOMATOES,
ALLERGEN_CHOCOLATE,
ALLERGEN_POLLEN,
ALLERGEN_CATS,
ALLERGEN_COUNT,
} allergen_t;
typedef struct {
int count;
bool allergens[ALLERGEN_COUNT];
} allergen_list_t;
// We could use macro...
// #define is_allergic_to(allerg, val) (!!((val) & (1 << (allerg))))
bool is_allergic_to(const allergen_t allergen, const uint32_t value);
allergen_list_t get_allergens(uint32_t value);
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif