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,21 @@
# 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.
#
# I hope this will be use-able for next exercises...
#include gmsl
include makefile
manual=-DUNIT_TEST
debug=$(manual) -DDEBUG
.PHONY: manual debug
manual debug: src/*.c src/*.h
$(CC) $($@) src/*.c -o $@.out
#debug: src/*.c src/*.h
# $(CC) $(DEBUG) src/*.c -o $@.out

View File

@@ -0,0 +1,50 @@
# Armstrong Numbers
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
For example:
- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
Write some code to determine whether a number is an Armstrong number.
## 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
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
## 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

View File

@@ -0,0 +1,35 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.
###
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)

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include "armstrong_numbers.h"
static inline int power(int n, int p) {
int res=n;
/* useless here
* if (p==0)
* return 1;
*/
while (--p)
res*=n;
return res;
}
bool is_armstrong_number(int candidate)
{
int p=1, r=0, tmp=candidate;
while (tmp/=10)
p++;
for (tmp=candidate; tmp; tmp /=10)
r+=power(tmp%10, p);
return r==candidate;
}
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1, n;
for (; arg<ac; ++arg) {
n=atoi(av[arg]);
printf("armstrong(%d)=%d\n", n, is_armstrong_number(n));
}
}
#endif

View File

@@ -0,0 +1,8 @@
#ifndef ARMSTRONG_NUMBERS
#define ARMSTRONG_NUMBERS
#include <stdbool.h>
bool is_armstrong_number(int candidate);
#endif