diff options
author | Haidong Ji | 2022-04-15 15:51:30 -0500 |
---|---|---|
committer | Haidong Ji | 2022-04-15 15:51:30 -0500 |
commit | 442a49ad5a48d417345959b903ae6a6d32d55759 (patch) | |
tree | c7127bb497e5e439018b1915e0136eec2c9cb124 /33_counts |
Excellent fundamentals and displine training, many tools and techniques
exercises: gdb, emacs, valgrind, git
Diffstat (limited to '33_counts')
-rw-r--r-- | 33_counts/Makefile | 12 | ||||
-rw-r--r-- | 33_counts/README | 58 | ||||
-rw-r--r-- | 33_counts/counts.c | 56 | ||||
-rw-r--r-- | 33_counts/counts.c~ | 31 | ||||
-rw-r--r-- | 33_counts/counts.h | 23 | ||||
-rw-r--r-- | 33_counts/counts.h~ | 20 | ||||
-rw-r--r-- | 33_counts/counts.o | bin | 0 -> 57304 bytes | |||
-rwxr-xr-x | 33_counts/counts_test | bin | 0 -> 54216 bytes | |||
-rw-r--r-- | 33_counts/counts_test.c | 18 | ||||
-rw-r--r-- | 33_counts/counts_test.o | bin | 0 -> 55720 bytes | |||
-rw-r--r-- | 33_counts/grade.txt | 19 |
11 files changed, 237 insertions, 0 deletions
diff --git a/33_counts/Makefile b/33_counts/Makefile new file mode 100644 index 0000000..700ebd4 --- /dev/null +++ b/33_counts/Makefile @@ -0,0 +1,12 @@ +CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 +OBJS=counts.o counts_test.o +PROGRAM=counts_test + +$(PROGRAM): $(OBJS) + gcc $(CFLAGS) -o $@ $(OBJS) + +%.o: %.c counts.h + gcc -c $(CFLAGS) $< + +clean: + rm -f $(OBJS) $(PROGRAM) *~ diff --git a/33_counts/README b/33_counts/README new file mode 100644 index 0000000..986af21 --- /dev/null +++ b/33_counts/README @@ -0,0 +1,58 @@ +For this problem, we will address the following tasks: + + - print the counts to a file + - free the memory for the counts + +We'll note that we are not going to write the part of this program where +we read the input file and compute the counts until the next problem. However, we will +still want to be able to test our code. We can do this, by having a main +function which constructs the counts from a hard coded set of data, skipping +the details of the actual program (this is an example of a test scaffold). + +Our test scaffold can benefit from some functionality that (if we think a bit ahead) +will be useful to abstract out into a couple functions, so we can re-use that code +in the next problem. (Abstracting all of this code out into function is also good because +it hides the implementation details: none of the code in the main function +we provide cares what the names/types of the fields in the counts_t structure +are, which you will make shortly). + +First, go to counts.h. Here, you will find two empty struct declarations. You will +need to fill these in. The first should reflect the information about one count. +That is, for some particular string, how many times have we seen it so far. +The second, should have an array of the first, as well as the size of that array. +You should also include a field in this struct to count unknown names. + +Next, you should go to counts.c, and write the four functions there. + +The first, createCounts should allocate memory for a counts_t structure, and initialize +it to represent that nothing has been counted yet. + +The next function, addCount, should increment the count for the corresponding name. Note +that name will be NULL in the case of something that is unknown, so your code must account +for this case. + +The third function, printCounts takes a counts_t and prints that information to +the FILE outFile. Recall from the description of the entire problem, that this +function should print in the format: + +Captain: 1 +Commander: 2 +Lt. Commander: 1 +<unknown> : 1 + +These should appear in the order that the name is first added, with unknown always +appearing last. + +***If there are no unknown values, you should not print a line for unknown. That +is, you should NEVEr print +<unknown> : 0 + + +Finally, you should write freeCounts, which should free all the memory associated with +a counts_t. + +We have provided a main in countsTestc which creates a counts_t (using your createCounts +function), adds some names to it (using your addCount function), prints the result +to stdout (using your printCounts) function, then frees the memory (using your freeCounts). + +Test and debug these functions before proceeding. diff --git a/33_counts/counts.c b/33_counts/counts.c new file mode 100644 index 0000000..c69f9aa --- /dev/null +++ b/33_counts/counts.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "counts.h" +counts_t * createCounts(void) { + counts_t * c = malloc(sizeof(* c)); + c->countArray = NULL; + c->arraySize = 0; + c->count_of_unknowns = 0; + return c; +} + +void addCount(counts_t * c, const char * name) { + if (name == NULL) { + c->count_of_unknowns++; + return; + } + + for (int i = 0; i < c->arraySize; i++) { + if (strcmp(name, c->countArray[i].string) == 0) { + c->countArray[i].count++; + return; + } + } + + one_count_t * newCount = malloc(sizeof(one_count_t)); + newCount->string = malloc((strlen(name) + 1) * sizeof(char)); + strcpy(newCount->string, name); + newCount->count = 1; + c->countArray = realloc(c->countArray, (c->arraySize + 1) * sizeof(one_count_t)); + c->countArray[c->arraySize] = *newCount; + c->arraySize++; + free(newCount); + +} +void printCounts(counts_t * c, FILE * outFile) { + for (int i = 0; i < c->arraySize; i++) { + fprintf(outFile, "%s: %d\n", c->countArray[i].string, c->countArray[i].count); + } + + if (c->count_of_unknowns > 0) { + fprintf(outFile, "%s: %d\n", "<unknown> ", c->count_of_unknowns); + } + + if(fclose(outFile) != 0) { + fprintf(stderr, "oh no, couldn't close the file!\n"); + } +} + +void freeCounts(counts_t * c) { + for (int i = 0; i < c->arraySize; i++) { + free(c->countArray[i].string); + } + free(c->countArray); + free(c); +} diff --git a/33_counts/counts.c~ b/33_counts/counts.c~ new file mode 100644 index 0000000..819726a --- /dev/null +++ b/33_counts/counts.c~ @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "counts.h" +counts_t * createCounts(void) { + counts_t c = malloc(sizeof(counts_t)); + c.countArray = NULL; + c.arraySize = 0; + c.count_of_unknowns = 0; +} + +void addCount(counts_t * c, const char * name) { + if (name == NULL) { + c.count_of_unknowns++; + return; + } + + for (int i = 0; i < c.arraySize; i++) { + if (strcmp(name, c.countArray[i].string) == 0) { + c.countArray[i].count++; + return; + } + } +} +void printCounts(counts_t * c, FILE * outFile) { + //WRITE ME +} + +void freeCounts(counts_t * c) { + //WRITE ME +} diff --git a/33_counts/counts.h b/33_counts/counts.h new file mode 100644 index 0000000..cf0783c --- /dev/null +++ b/33_counts/counts.h @@ -0,0 +1,23 @@ +#include <stdio.h> +#ifndef __COUNTS_H__ +#define __COUNTS_H__ +struct _one_count_t { + char * string; + int count; +}; +typedef struct _one_count_t one_count_t; + +struct _counts_t { + one_count_t * countArray; + int arraySize; + int count_of_unknowns; +}; +typedef struct _counts_t counts_t; + +counts_t * createCounts(void); +void addCount(counts_t * c, const char * name); +void printCounts(counts_t * c, FILE * outFile); + +void freeCounts(counts_t * c); + +#endif diff --git a/33_counts/counts.h~ b/33_counts/counts.h~ new file mode 100644 index 0000000..18f28c4 --- /dev/null +++ b/33_counts/counts.h~ @@ -0,0 +1,20 @@ +#ifndef __COUNTS_H__ +#define __COUNTS_H__ +struct _one_count_t { + //DEFINE ME + +}; +typedef struct _one_count_t one_count_t; + +struct _counts_t { + //DEFINE ME +}; +typedef struct _counts_t counts_t; + +counts_t * createCounts(void); +void addCount(counts_t * c, const char * name); +void printCounts(counts_t * c, FILE * outFile); + +void freeCounts(counts_t * c); + +#endif diff --git a/33_counts/counts.o b/33_counts/counts.o Binary files differnew file mode 100644 index 0000000..00cf023 --- /dev/null +++ b/33_counts/counts.o diff --git a/33_counts/counts_test b/33_counts/counts_test Binary files differnew file mode 100755 index 0000000..fd87286 --- /dev/null +++ b/33_counts/counts_test diff --git a/33_counts/counts_test.c b/33_counts/counts_test.c new file mode 100644 index 0000000..905d918 --- /dev/null +++ b/33_counts/counts_test.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "counts.h" + +#define NUM_TESTS 12 +int main(void) { + char * testData[NUM_TESTS] = {"apple", "banana", NULL,"apple", + "frog","sword","bear",NULL, + "frog","apple", "zebra", "knight"}; + counts_t * testCounts= createCounts(); + for(int i =0; i < NUM_TESTS; i++) { + addCount(testCounts,testData[i]); + } + printCounts(testCounts, stdout); + freeCounts(testCounts); + return EXIT_SUCCESS; +} diff --git a/33_counts/counts_test.o b/33_counts/counts_test.o Binary files differnew file mode 100644 index 0000000..6da765e --- /dev/null +++ b/33_counts/counts_test.o diff --git a/33_counts/grade.txt b/33_counts/grade.txt new file mode 100644 index 0000000..844521f --- /dev/null +++ b/33_counts/grade.txt @@ -0,0 +1,19 @@ +Grading at Sat 25 Dec 2021 01:42:38 AM UTC +Attempting to compile: +################################################# +testcase1: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase2: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase3: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean + +Overall Grade: A |