C: grade school
This commit is contained in:
51
c/grade-school/GNUmakefile
Normal file
51
c/grade-school/GNUmakefile
Normal file
@@ -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
|
76
c/grade-school/README.md
Normal file
76
c/grade-school/README.md
Normal file
@@ -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
|
37
c/grade-school/makefile
Normal file
37
c/grade-school/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)
|
84
c/grade-school/src/grade_school.c
Normal file
84
c/grade-school/src/grade_school.c
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include "grade_school.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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; i<c && (GRADE(i)<g || (GRADE(i)==g && strcmp(s, NAME(i))>0)); ++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<roster.count && GRADE(i)<=g; ++i) {
|
||||||
|
if (GRADE(i)==g)
|
||||||
|
r.students[j++]=STD(i);
|
||||||
|
}
|
||||||
|
r.count=j;
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
roster_t get_roster()
|
||||||
|
{
|
||||||
|
return roster;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* See GNUmakefile below for explanation
|
||||||
|
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||||
|
*/
|
||||||
|
#ifdef UNIT_TEST
|
||||||
|
static void print_roster()
|
||||||
|
{
|
||||||
|
unsigned i;
|
||||||
|
printf("======== roster size: %lu\n", roster.count);
|
||||||
|
for (i=0; i<roster.count; ++i)
|
||||||
|
printf("roster(%02d): [%d]%s\n", i, GRADE(i), NAME(i));
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
int arg=1;
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (; arg<ac-1; ++arg, ++arg) {
|
||||||
|
i=atoi(av[arg]);
|
||||||
|
add_student(av[arg+1], i);
|
||||||
|
}
|
||||||
|
print_roster();
|
||||||
|
}
|
||||||
|
#endif
|
38
c/grade-school/src/grade_school.h
Normal file
38
c/grade-school/src/grade_school.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef GRADE_SCHOOL_H
|
||||||
|
#define GRADE_SCHOOL_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
//#include <stdbool.h>
|
||||||
|
|
||||||
|
#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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
#ifdef TESTALL
|
||||||
|
#undef TEST_IGNORE
|
||||||
|
#define TEST_IGNORE() {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user