initial commit
This commit is contained in:
44
c/resistor-color/src/resistor_color.c
Normal file
44
c/resistor-color/src/resistor_color.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "resistor_color.h"
|
||||
|
||||
/* V1: initial version
|
||||
* V2: add error code in enum
|
||||
*/
|
||||
static resistor_band_t mapping[]={
|
||||
BLACK,
|
||||
BROWN,
|
||||
RED,
|
||||
ORANGE,
|
||||
YELLOW,
|
||||
GREEN,
|
||||
BLUE,
|
||||
VIOLET,
|
||||
GREY,
|
||||
WHITE,
|
||||
ERROR
|
||||
};
|
||||
|
||||
resistor_band_t color_code(resistor_band_t color)
|
||||
{
|
||||
return color>=BLACK && color<=WHITE? color: ERROR;
|
||||
}
|
||||
|
||||
resistor_band_t *colors()
|
||||
{
|
||||
return mapping;
|
||||
}
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int arg=1, n;
|
||||
|
||||
for (; arg<ac; ++arg) {
|
||||
n=atoi(av[arg]);
|
||||
printf("color(%d)=%d\n", n, color_code(n));
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
26
c/resistor-color/src/resistor_color.h
Normal file
26
c/resistor-color/src/resistor_color.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef RESISTOR_COLOR_H
|
||||
#define RESISTOR_COLOR_H
|
||||
|
||||
typedef enum {
|
||||
BLACK=0,
|
||||
BROWN,
|
||||
RED,
|
||||
ORANGE,
|
||||
YELLOW,
|
||||
GREEN,
|
||||
BLUE,
|
||||
VIOLET,
|
||||
GREY,
|
||||
WHITE,
|
||||
ERROR=-1,
|
||||
} resistor_band_t;
|
||||
|
||||
extern resistor_band_t color_code(resistor_band_t);
|
||||
extern resistor_band_t *colors();
|
||||
|
||||
#ifdef TESTALL
|
||||
#undef TEST_IGNORE
|
||||
#define TEST_IGNORE() {}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user