initial commit
This commit is contained in:
41
c/resistor-color-trio/GNUmakefile
Normal file
41
c/resistor-color-trio/GNUmakefile
Normal file
@@ -0,0 +1,41 @@
|
||||
# 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.
|
||||
# - Also, it does not allow to run all tests without editing the test source
|
||||
# code.
|
||||
#
|
||||
# To use this makefile (GNU make only):
|
||||
# "make": build with all predefined tests (without editing test source code)
|
||||
# "make mem": perform memcheck with all tests enabled
|
||||
# "make unit": build standalone (unit) test
|
||||
# "make debug": build standalone test with debugging code
|
||||
#
|
||||
# Original 'makefile' targets can be used (test, memcheck, clean, ...)
|
||||
|
||||
.PHONY: default all mem unit debug std
|
||||
|
||||
default: all
|
||||
|
||||
# default is to build with all predefined tests
|
||||
BUILD := teststall
|
||||
|
||||
include makefile
|
||||
|
||||
all: CFLAGS+=-DTESTALL
|
||||
all: clean test
|
||||
|
||||
mem: CFLAGS+=-DTESTALL
|
||||
mem: clean memcheck
|
||||
|
||||
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
|
83
c/resistor-color-trio/README.md
Normal file
83
c/resistor-color-trio/README.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# Resistor Color Trio
|
||||
|
||||
If you want to build something using a Raspberry Pi, you'll probably use _resistors_. For this exercise, you need to know only three 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 acts as a digit of a number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
|
||||
In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take 3 colors as input, and outputs the correct value, in ohms.
|
||||
The color bands are encoded as follows:
|
||||
|
||||
* Black: 0
|
||||
* Brown: 1
|
||||
* Red: 2
|
||||
* Orange: 3
|
||||
* Yellow: 4
|
||||
* Green: 5
|
||||
* Blue: 6
|
||||
* Violet: 7
|
||||
* Grey: 8
|
||||
* White: 9
|
||||
|
||||
In `resistor-color duo` you decoded the first two colors. For instance: orange-orange got the main value `33`.
|
||||
The third color stands for how many zeros need to be added to the main value. The main value plus the zeros gives us a value in ohms.
|
||||
For the exercise it doesn't matter what ohms really are.
|
||||
For example:
|
||||
|
||||
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
|
||||
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
|
||||
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
|
||||
|
||||
(If Math is your thing, you may want to think of the zeros as exponents of 10. If Math is not your thing, go with the zeros. It really is the same thing, just in plain English instead of Math lingo.)
|
||||
|
||||
This exercise is about translating the colors into a label:
|
||||
|
||||
> "... ohms"
|
||||
|
||||
So an input of `"orange", "orange", "black"` should return:
|
||||
|
||||
> "33 ohms"
|
||||
|
||||
When we get more than a thousand ohms, we say "kiloohms". That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.
|
||||
So an input of `"orange", "orange", "orange"` should return:
|
||||
|
||||
> "33 kiloohms"
|
||||
|
||||
## 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/1549](https://github.com/exercism/problem-specifications/issues/1549)
|
||||
|
||||
## 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
|
37
c/resistor-color-trio/makefile
Normal file
37
c/resistor-color-trio/makefile
Normal file
@@ -0,0 +1,37 @@
|
||||
### 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 Compiling $@
|
||||
@$(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS)
|
62
c/resistor-color-trio/src/resistor_color_trio.c
Normal file
62
c/resistor-color-trio/src/resistor_color_trio.c
Normal 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
|
45
c/resistor-color-trio/src/resistor_color_trio.h
Normal file
45
c/resistor-color-trio/src/resistor_color_trio.h
Normal 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
|
Reference in New Issue
Block a user