From 4cf3fb610af0ee1b6b02516fbae6214c3b92de4b Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 13 Aug 2021 11:53:31 +0200 Subject: [PATCH] C: grade school --- c/grade-school/GNUmakefile | 51 +++++++++++++++++++ c/grade-school/README.md | 76 ++++++++++++++++++++++++++++ c/grade-school/makefile | 37 ++++++++++++++ c/grade-school/src/grade_school.c | 84 +++++++++++++++++++++++++++++++ c/grade-school/src/grade_school.h | 38 ++++++++++++++ 5 files changed, 286 insertions(+) create mode 100644 c/grade-school/GNUmakefile create mode 100644 c/grade-school/README.md create mode 100644 c/grade-school/makefile create mode 100644 c/grade-school/src/grade_school.c create mode 100644 c/grade-school/src/grade_school.h diff --git a/c/grade-school/GNUmakefile b/c/grade-school/GNUmakefile new file mode 100644 index 0000000..ec085d0 --- /dev/null +++ b/c/grade-school/GNUmakefile @@ -0,0 +1,51 @@ +# 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 debugall": build with all predefined tests and debug 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 debugtest + +default: all + +# default is to build with all predefined tests +BUILD := teststall + +include makefile + +all: CFLAGS+=-DTESTALL +all: clean test + +debugall: CFLAGS+=-DDEBUG +debugall: all + +debugtest: CFLAGS+=-DDEBUG +debugtest: test + +mem: CFLAGS+=-DTESTALL +mem: clean memcheck + +unit: CFLAGS+=-DUNIT_TEST +unit: clean std + +debug: CFLAGS+=-DUNIT_TEST -DDEBUG +debug: clean std + +debugtest: CFLAGS+=-DDEBUG +debugtest: test + +std: src/*.c src/*.h + $(CC) $(CFLAGS) src/*.c -o test.out diff --git a/c/grade-school/README.md b/c/grade-school/README.md new file mode 100644 index 0000000..fb39e9b --- /dev/null +++ b/c/grade-school/README.md @@ -0,0 +1,76 @@ +# Grade School + +Given students' names along with the grade that they are in, create a roster +for the school. + +In the end, you should be able to: + +- Add a student's name to the roster for a grade + - "Add Jim to grade 2." + - "OK." +- Get a list of all students enrolled in a grade + - "Which students are in grade 2?" + - "We've only got Jim just now." +- Get a sorted list of all students in all grades. Grades should sort + as 1, 2, 3, etc., and students within a grade should be sorted + alphabetically by name. + - "Who all is enrolled in school right now?" + - "Let me think. We have + Anna, Barb, and Charlie in grade 1, + Alex, Peter, and Zoe in grade 2 + and Jim in grade 5. + So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim" + +Note that all our students only have one name. (It's a small town, what +do you want?) + +## For bonus points + +Did you get the tests passing and the code clean? If you want to, these +are some additional things you could try: + +- If you're working in a language with mutable data structures and your + implementation allows outside code to mutate the school's internal DB + directly, see if you can prevent this. Feel free to introduce additional + tests. + +Then please share your thoughts in a comment on the submission. Did this +experiment make the code better? Worse? Did you learn anything from it? + +## 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 + +A pairing session with Phil Battos at gSchool [http://gschool.it](http://gschool.it) + +## 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 diff --git a/c/grade-school/makefile b/c/grade-school/makefile new file mode 100644 index 0000000..f34535a --- /dev/null +++ b/c/grade-school/makefile @@ -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) diff --git a/c/grade-school/src/grade_school.c b/c/grade-school/src/grade_school.c new file mode 100644 index 0000000..1ee6b91 --- /dev/null +++ b/c/grade-school/src/grade_school.c @@ -0,0 +1,84 @@ +#include "grade_school.h" +#include +#include + +#define STD(i) roster.students[i] +#define NAME(i) roster.students[i].name +#define GRADE(i) roster.students[i].grade + +static roster_t roster; + +void clear_roster() +{ + roster.count=0; +} + +/* testing program expects bool here, but I prefer to return students count, + * it makes more sense for me. + */ +int add_student(char *s, uint8_t g) +{ + int c=roster.count; + int i; + + if (c >= MAX_STUDENTS) + return 0; + // find correct place to insert name + for (i=0; i0)); ++i) + ; + for (int j=c-1; j>=i; --j) /* move rest to right */ + STD(j+1)=STD(j); + NAME(i)=s; /* insert new name */ + GRADE(i)=g; + + return ++roster.count; +} + +/* to avoid this everytime, we could build up one roster per grade while + * adding students, but really overkill here. + */ +roster_t get_grade(uint8_t g) +{ + static roster_t r; + unsigned i, j=0; + + for (i=0; i +#include +//#include + +#define MAX_NAME_LENGTH 20 +#define MAX_STUDENTS 20 + +typedef struct student_s { + uint8_t grade; + char *name; +} student_t; + +typedef struct { + size_t count; + student_t students[MAX_STUDENTS]; +} roster_t; + +void clear_roster(); +int add_student(char *, uint8_t); +roster_t get_grade(uint8_t); +roster_t get_roster(); + +/* See GNUmakefile below for explanation + * https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile + */ +#if defined UNIT_TEST || defined DEBUG +#include +#include +#endif +#ifdef TESTALL +#undef TEST_IGNORE +#define TEST_IGNORE() {} +#endif + +#endif