C: allergies
This commit is contained in:
18
c/allergies/src/allergies.c
Normal file
18
c/allergies/src/allergies.c
Normal 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;
|
||||
}
|
42
c/allergies/src/allergies.h
Normal file
42
c/allergies/src/allergies.h
Normal 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
|
Reference in New Issue
Block a user