initial commit

This commit is contained in:
2021-08-08 21:11:22 +02:00
commit fe7136d801
130 changed files with 6858 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
#include "resistor_color_trio.h"
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
static const unsigned p10[] = {
1e0, 1e1, 1e2, 1e3, 1e4,
1e5, 1e6, 1e7, 1e8, 1e9,
};
static const unsigned unit[] = {
p10[9], p10[6], p10[3], p10[0]
};
#define S(a) ((int)(sizeof(a)/sizeof(*a)))
#define RB_OK(b) ((b)>=BLACK && (b)<=WHITE)
static resistor_value_t ui2val(unsigned value)
{
resistor_value_t res={0, OHMS};
int i;
for (i=0; i<S(unit); ++i) {
if (!(value%unit[i])) {
res.value=value/unit[i];
res.unit=i;
break;
}
}
return res;
}
resistor_value_t color_code(resistor_band_t *colors)
{
resistor_band_t c1=*colors, c2=*(colors+1), c3=*(colors+2);
resistor_value_t res={RB_ERR, UN_ERR};
if (RB_OK(c1) && RB_OK(c2) && RB_OK(c3))
res=ui2val((c1*10+c2) * p10[c3]);
return res;
}
/* See GNUmakefile below for explanation
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1;
resistor_band_t i[3];
resistor_value_t res;
for (; arg<ac-2; arg+=3) {
*i=atoi(av[arg]);
*(i+1)=atoi(av[arg+1]);
*(i+2)=atoi(av[arg+2]);
res=color_code(i);
printf("color(%d, %d, %d)=%d, %d\n", i[0], i[1], i[2], res.value, res.unit);
}
}
#endif

View File

@@ -0,0 +1,45 @@
#ifndef RESISTOR_COLOR_TRIO_H
#define RESISTOR_COLOR_TRIO_H
typedef enum {
BLACK=0,
BROWN,
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
VIOLET,
GREY,
WHITE,
RB_ERR=-1,
} resistor_band_t;
/* max=white-white-white =99 000 000 000=99 Giga
* warning: overflow before that (untested) !
*/
typedef enum {
GIGAOHMS=0,
MEGAOHMS,
KILOOHMS,
OHMS,
UN_ERR=-1
} unit_t;
typedef struct {
resistor_band_t value;
unit_t unit;
} resistor_value_t;
extern resistor_value_t color_code(resistor_band_t *);
/* Note: For explanation on section below, see 'GNUfilename' included in
* link below :
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif