initial commit
This commit is contained in:
34
c/resistor-color/GNUmakefile
Normal file
34
c/resistor-color/GNUmakefile
Normal file
@@ -0,0 +1,34 @@
|
||||
# the original 'makefile' has a flaw:
|
||||
# 1) it overrides CFLAGS
|
||||
# 2) it does not pass extra "FLAGS" to $(CC) that could come from environment
|
||||
#
|
||||
# It means we need to edit 'makefile' for different builds (DEBUG, etc...),
|
||||
# which is not practical at all.
|
||||
#
|
||||
# To use this makefile:
|
||||
# "make": build with all predefined tests
|
||||
# "make unit": build standalone (unit) test
|
||||
# "make debug": build standalone test with debugging code
|
||||
#
|
||||
# Original 'makefile' targets can be used (make test, etc...)
|
||||
|
||||
.PHONY: default all unit debug std
|
||||
|
||||
default: all
|
||||
|
||||
# default is to build with all predefined tests
|
||||
BUILD := teststall
|
||||
|
||||
include makefile
|
||||
|
||||
all: CFLAGS+=-DTESTALL
|
||||
all: clean test
|
||||
|
||||
unit: CFLAGS+=-DUNIT_TEST
|
||||
unit: clean std
|
||||
|
||||
debug: CFLAGS+=-DUNIT_TEST -DDEBUG
|
||||
debug: clean std
|
||||
|
||||
std: src/*.c src/*.h
|
||||
$(CC) $(CFLAGS) src/*.c -o test.out
|
73
c/resistor-color/README.md
Normal file
73
c/resistor-color/README.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# Resistor Color
|
||||
|
||||
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
|
||||
For this exercise, you need to know two things about them:
|
||||
|
||||
* Each resistor has a resistance value.
|
||||
* Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
|
||||
|
||||
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
|
||||
Each band has a position and a numeric value.
|
||||
|
||||
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
|
||||
|
||||
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
|
||||
|
||||
These colors are encoded as follows:
|
||||
|
||||
- Black: 0
|
||||
- Brown: 1
|
||||
- Red: 2
|
||||
- Orange: 3
|
||||
- Yellow: 4
|
||||
- Green: 5
|
||||
- Blue: 6
|
||||
- Violet: 7
|
||||
- Grey: 8
|
||||
- White: 9
|
||||
|
||||
The goal of this exercise is to create a way:
|
||||
- to look up the numerical value associated with a particular color band
|
||||
- to list the different band colors
|
||||
|
||||
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.
|
||||
|
||||
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code)
|
||||
|
||||
## Getting Started
|
||||
|
||||
Make sure you have read the "Guides" section of the
|
||||
[C track][c-track] on the Exercism site. This covers
|
||||
the basic information on setting up the development environment expected
|
||||
by the exercises.
|
||||
|
||||
## Passing the Tests
|
||||
|
||||
Get the first test compiling, linking and passing by following the [three
|
||||
rules of test-driven development][3-tdd-rules].
|
||||
|
||||
The included makefile can be used to create and run the tests using the `test`
|
||||
task.
|
||||
|
||||
make test
|
||||
|
||||
Create just the functions you need to satisfy any compiler errors and get the
|
||||
test to fail. Then write just enough code to get the test to pass. Once you've
|
||||
done that, move onto the next test.
|
||||
|
||||
As you progress through the tests, take the time to refactor your
|
||||
implementation for readability and expressiveness and then go on to the next
|
||||
test.
|
||||
|
||||
Try to use standard C99 facilities in preference to writing your own
|
||||
low-level algorithms or facilities by hand.
|
||||
|
||||
## Source
|
||||
|
||||
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[c-track]: https://exercism.io/my/tracks/c
|
||||
[3-tdd-rules]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
|
38
c/resistor-color/makefile
Normal file
38
c/resistor-color/makefile
Normal file
@@ -0,0 +1,38 @@
|
||||
### If you wish to use extra libraries (math.h for instance),
|
||||
### add their flags here (-lm in our case) in the "LIBS" variable.
|
||||
|
||||
LIBS = -lm
|
||||
|
||||
###
|
||||
CFLAGS = -std=c99
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -pedantic
|
||||
CFLAGS += -Werror
|
||||
CFLAGS += -Wmissing-declarations
|
||||
CFLAGS += -DUNITY_SUPPORT_64
|
||||
|
||||
ASANFLAGS = -fsanitize=address
|
||||
ASANFLAGS += -fno-common
|
||||
ASANFLAGS += -fno-omit-frame-pointer
|
||||
|
||||
.PHONY: test
|
||||
test: tests.out
|
||||
@./tests.out
|
||||
|
||||
.PHONY: memcheck
|
||||
memcheck: test/*.c src/*.c src/*.h
|
||||
@echo Compiling $@
|
||||
@$(CC) $(ASANFLAGS) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o memcheck.out $(LIBS)
|
||||
@./memcheck.out
|
||||
@echo "Memory check passed"
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf *.o *.out *.out.dSYM
|
||||
|
||||
tests.out: test/*.c src/*.c src/*.h
|
||||
echo toto $(CFLAGS)
|
||||
@echo Compiling $@
|
||||
@$(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS)
|
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