From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 34_put_together/Makefile | 20 +++++++++++ 34_put_together/README | 41 ++++++++++++++++++++++ 34_put_together/count_values | Bin 0 -> 61264 bytes 34_put_together/counts.c | 1 + 34_put_together/counts.h | 1 + 34_put_together/counts.o | Bin 0 -> 57304 bytes 34_put_together/grade.txt | 45 +++++++++++++++++++++++++ 34_put_together/kv.c | 1 + 34_put_together/kv.h | 1 + 34_put_together/kv.o | Bin 0 -> 58944 bytes 34_put_together/kvs1.txt | 21 ++++++++++++ 34_put_together/kvs2.txt | 12 +++++++ 34_put_together/list1a.txt | 11 ++++++ 34_put_together/list1a.txt.ans | 6 ++++ 34_put_together/list1a.txt.counts | 6 ++++ 34_put_together/list1b.txt | 10 ++++++ 34_put_together/list1b.txt.ans | 7 ++++ 34_put_together/list1b.txt.counts | 7 ++++ 34_put_together/list2a.txt | 5 +++ 34_put_together/list2a.txt.ans | 5 +++ 34_put_together/list2b.txt | 8 +++++ 34_put_together/list2b.txt.ans | 5 +++ 34_put_together/list2c.txt | 8 +++++ 34_put_together/list2c.txt.ans | 5 +++ 34_put_together/main.c | 69 ++++++++++++++++++++++++++++++++++++++ 34_put_together/main.c~ | 49 +++++++++++++++++++++++++++ 34_put_together/main.o | Bin 0 -> 57952 bytes 34_put_together/outname.c | 1 + 34_put_together/outname.h | 1 + 34_put_together/outname.o | Bin 0 -> 53104 bytes 34_put_together/vgcore.49132 | Bin 0 -> 6467584 bytes 34_put_together/vgcore.49206 | Bin 0 -> 6467584 bytes 32 files changed, 346 insertions(+) create mode 100644 34_put_together/Makefile create mode 100644 34_put_together/README create mode 100755 34_put_together/count_values create mode 120000 34_put_together/counts.c create mode 120000 34_put_together/counts.h create mode 100644 34_put_together/counts.o create mode 100644 34_put_together/grade.txt create mode 120000 34_put_together/kv.c create mode 120000 34_put_together/kv.h create mode 100644 34_put_together/kv.o create mode 100644 34_put_together/kvs1.txt create mode 100644 34_put_together/kvs2.txt create mode 100644 34_put_together/list1a.txt create mode 100644 34_put_together/list1a.txt.ans create mode 100644 34_put_together/list1a.txt.counts create mode 100644 34_put_together/list1b.txt create mode 100644 34_put_together/list1b.txt.ans create mode 100644 34_put_together/list1b.txt.counts create mode 100644 34_put_together/list2a.txt create mode 100644 34_put_together/list2a.txt.ans create mode 100644 34_put_together/list2b.txt create mode 100644 34_put_together/list2b.txt.ans create mode 100644 34_put_together/list2c.txt create mode 100644 34_put_together/list2c.txt.ans create mode 100644 34_put_together/main.c create mode 100644 34_put_together/main.c~ create mode 100644 34_put_together/main.o create mode 120000 34_put_together/outname.c create mode 120000 34_put_together/outname.h create mode 100644 34_put_together/outname.o create mode 100644 34_put_together/vgcore.49132 create mode 100644 34_put_together/vgcore.49206 (limited to '34_put_together') diff --git a/34_put_together/Makefile b/34_put_together/Makefile new file mode 100644 index 0000000..9281640 --- /dev/null +++ b/34_put_together/Makefile @@ -0,0 +1,20 @@ +CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 +SRCS=$(wildcard *.c) +OBJS=$(patsubst %.c, %.o, $(SRCS)) +PROGRAM=count_values + +$(PROGRAM): $(OBJS) + gcc $(CFLAGS) -o $@ $(OBJS) + +%.o: %.c + gcc -c $(CFLAGS) $< + +clean: + rm -f $(OBJS) $(PROGRAM) *~ + +counts.o: counts.h +outname.o: outname.h +kv.o: kv.h +main.o: kv.h +main.o: outname.h +main.o: counts.h diff --git a/34_put_together/README b/34_put_together/README new file mode 100644 index 0000000..b438ead --- /dev/null +++ b/34_put_together/README @@ -0,0 +1,41 @@ +We are now ready for the last piece: + - compute the counts of values that appear in a particular input file + +And then to put all the pieces together. Before you start, notice that +we have placed symlinks (short for "symbolic links") to the files you worked on +in the previous problems. Symbolic links basically mean that when you open +the file, it will open the file it links to (so opening counts.c will open +../27_counts/counts.c ). + +For this problem, you will start by writing the function: + + counts_t * countFile(const char * filename, kvarray_t * kvPairs) + +in the main.c file. Note that you will find it quite helpful to use several of the functions +you wrote in the previous problems (in fact, much of the work should already be done). + +Once you have written countFile, it is time to write your main function. You +will also write this function in the main.c file. Note that we have already +written the high-level algorithm for the main function as comments, so you can +just translate this algorithm to code. You will also need to add some error +checking. + +Once you finish writing these two functions, you should have a working program! +Compile, test, and debug it. + +We have provided two test cases, and the correct output for them. + +The first test case: + ./count_values kvs1.txt list1a.txt list1b.txt + +should produce two output files (list1a.txt.counts and list1b.txt.counts). +The correct contents can be found in list1a.txt.ans and list1b.txt.ans. + +The second test case: + ./count_values kvs2.txt list2a.txt list2b.txt list2c.txt + +should produce 3 files (named appropriately) and the answers can be found in +similarly named .ans files. + +Use git add . to submit your work. + diff --git a/34_put_together/count_values b/34_put_together/count_values new file mode 100755 index 0000000..772972a Binary files /dev/null and b/34_put_together/count_values differ diff --git a/34_put_together/counts.c b/34_put_together/counts.c new file mode 120000 index 0000000..6315a63 --- /dev/null +++ b/34_put_together/counts.c @@ -0,0 +1 @@ +../33_counts/counts.c \ No newline at end of file diff --git a/34_put_together/counts.h b/34_put_together/counts.h new file mode 120000 index 0000000..12e8e77 --- /dev/null +++ b/34_put_together/counts.h @@ -0,0 +1 @@ +../33_counts/counts.h \ No newline at end of file diff --git a/34_put_together/counts.o b/34_put_together/counts.o new file mode 100644 index 0000000..2ef87cd Binary files /dev/null and b/34_put_together/counts.o differ diff --git a/34_put_together/grade.txt b/34_put_together/grade.txt new file mode 100644 index 0000000..9b57db2 --- /dev/null +++ b/34_put_together/grade.txt @@ -0,0 +1,45 @@ +Grading at Sat 25 Dec 2021 08:12:05 PM UTC +Attempting to compile: +rm -f outname.o counts.o main.o kv.o count_values *~ +gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 outname.c +gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 counts.c +gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 main.c +gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 kv.c +gcc -Wall -Werror -std=gnu99 -pedantic -ggdb3 -o count_values outname.o counts.o main.o kv.o +################################################# +testcase1: +testcase1 passed, your program successfully indicated a failure + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase2: +Your file matched the expected output +Comparing file list1a.txt.counts with answer +Your output is correct +Comparing file list1b.txt.counts with answer +Your file matched the expected output +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase3: +Your file matched the expected output +Comparing file list2a.txt.counts with answer +Your output is correct +Comparing file list2b.txt.counts with answer +Your file matched the expected output +Your output is correct +Comparing file list2c.txt.counts with answer +Your file matched the expected output +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase4: +Your file matched the expected output +Comparing file list3a.txt.counts with answer +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean + +Overall Grade: A diff --git a/34_put_together/kv.c b/34_put_together/kv.c new file mode 120000 index 0000000..2c9f389 --- /dev/null +++ b/34_put_together/kv.c @@ -0,0 +1 @@ +../32_kvs/kv.c \ No newline at end of file diff --git a/34_put_together/kv.h b/34_put_together/kv.h new file mode 120000 index 0000000..11e307b --- /dev/null +++ b/34_put_together/kv.h @@ -0,0 +1 @@ +../32_kvs/kv.h \ No newline at end of file diff --git a/34_put_together/kv.o b/34_put_together/kv.o new file mode 100644 index 0000000..c3fef3a Binary files /dev/null and b/34_put_together/kv.o differ diff --git a/34_put_together/kvs1.txt b/34_put_together/kvs1.txt new file mode 100644 index 0000000..a51bda3 --- /dev/null +++ b/34_put_together/kvs1.txt @@ -0,0 +1,21 @@ +apple=red +apricot=yellow +avacado=green +banana=yellow +blueberry=blue +carrot=orange +cherry=red +cranberry=red +dates=brown +eggplant=purple +grapefruit=pink +grapes=purple +kiwi=green +lime=green +orange=orange +peach=yellow +pineapple=yellow +plums=purple +pomegranate=red +strawberry=red +tomato=red diff --git a/34_put_together/kvs2.txt b/34_put_together/kvs2.txt new file mode 100644 index 0000000..14b41db --- /dev/null +++ b/34_put_together/kvs2.txt @@ -0,0 +1,12 @@ +Jean Luc Picard=Captain +Will Riker=Commander +Beverly Crusher=Commander +Data=Lt. Commander +Geordi LaForge=Lt. Commander +Worf=Lt. Commander +Deanna Troi=Commander +Tasha Yar=Lieutenant +Wesley Crusher=Ensign +Ro Laren=Ensign +Reginald Barclay=Lieutenant +Guinan=Bartender diff --git a/34_put_together/list1a.txt b/34_put_together/list1a.txt new file mode 100644 index 0000000..aea3f25 --- /dev/null +++ b/34_put_together/list1a.txt @@ -0,0 +1,11 @@ +apple +avacado +raspberry +blueberry +cherry +eggplant +grapes +peach +pineapple +strawberry +fig diff --git a/34_put_together/list1a.txt.ans b/34_put_together/list1a.txt.ans new file mode 100644 index 0000000..2c282ef --- /dev/null +++ b/34_put_together/list1a.txt.ans @@ -0,0 +1,6 @@ +red: 3 +green: 1 +blue: 1 +purple: 2 +yellow: 2 + : 2 diff --git a/34_put_together/list1a.txt.counts b/34_put_together/list1a.txt.counts new file mode 100644 index 0000000..2c282ef --- /dev/null +++ b/34_put_together/list1a.txt.counts @@ -0,0 +1,6 @@ +red: 3 +green: 1 +blue: 1 +purple: 2 +yellow: 2 + : 2 diff --git a/34_put_together/list1b.txt b/34_put_together/list1b.txt new file mode 100644 index 0000000..db512c9 --- /dev/null +++ b/34_put_together/list1b.txt @@ -0,0 +1,10 @@ +kiwi +lime +pineapple +pomegranate +raisins +apple +blueberry +carrot +cherry +dates diff --git a/34_put_together/list1b.txt.ans b/34_put_together/list1b.txt.ans new file mode 100644 index 0000000..868b5d0 --- /dev/null +++ b/34_put_together/list1b.txt.ans @@ -0,0 +1,7 @@ +green: 2 +yellow: 1 +red: 3 +blue: 1 +orange: 1 +brown: 1 + : 1 diff --git a/34_put_together/list1b.txt.counts b/34_put_together/list1b.txt.counts new file mode 100644 index 0000000..868b5d0 --- /dev/null +++ b/34_put_together/list1b.txt.counts @@ -0,0 +1,7 @@ +green: 2 +yellow: 1 +red: 3 +blue: 1 +orange: 1 +brown: 1 + : 1 diff --git a/34_put_together/list2a.txt b/34_put_together/list2a.txt new file mode 100644 index 0000000..ae4e862 --- /dev/null +++ b/34_put_together/list2a.txt @@ -0,0 +1,5 @@ +Will Riker +Worf +Tasha Yar +Guinan +Ro Laren diff --git a/34_put_together/list2a.txt.ans b/34_put_together/list2a.txt.ans new file mode 100644 index 0000000..bcdabbc --- /dev/null +++ b/34_put_together/list2a.txt.ans @@ -0,0 +1,5 @@ +Commander: 1 +Lt. Commander: 1 +Lieutenant: 1 +Bartender: 1 +Ensign: 1 diff --git a/34_put_together/list2b.txt b/34_put_together/list2b.txt new file mode 100644 index 0000000..219ed60 --- /dev/null +++ b/34_put_together/list2b.txt @@ -0,0 +1,8 @@ +Jean Luc Picard +Q +Geordi LaForge +Some guy in a red shirt +Reginald Barclay +Beverly Crusher +Data +Worf diff --git a/34_put_together/list2b.txt.ans b/34_put_together/list2b.txt.ans new file mode 100644 index 0000000..b2abd8e --- /dev/null +++ b/34_put_together/list2b.txt.ans @@ -0,0 +1,5 @@ +Captain: 1 +Lt. Commander: 3 +Lieutenant: 1 +Commander: 1 + : 2 diff --git a/34_put_together/list2c.txt b/34_put_together/list2c.txt new file mode 100644 index 0000000..f6e8463 --- /dev/null +++ b/34_put_together/list2c.txt @@ -0,0 +1,8 @@ +Data +Geordi LaForge +Jean Luc Picard +Tasha Yar +Ro Laren +Spock +James T Kirk +Katherine Janeway diff --git a/34_put_together/list2c.txt.ans b/34_put_together/list2c.txt.ans new file mode 100644 index 0000000..3f33b39 --- /dev/null +++ b/34_put_together/list2c.txt.ans @@ -0,0 +1,5 @@ +Lt. Commander: 2 +Captain: 1 +Lieutenant: 1 +Ensign: 1 + : 3 diff --git a/34_put_together/main.c b/34_put_together/main.c new file mode 100644 index 0000000..6e4f508 --- /dev/null +++ b/34_put_together/main.c @@ -0,0 +1,69 @@ +#include "counts.h" +#include "kv.h" +#include "outname.h" +#include +#include +#include + +counts_t *countFile(const char *filename, kvarray_t *kvPairs) { + FILE *f = fopen(filename, "r"); + if (f == NULL) { + fprintf(stderr, "Could not open file!\n"); + return NULL; + } + + counts_t *c = createCounts(); + + char *line = NULL; + size_t sz; + + while (getline(&line, &sz, f) >= 0) { + strtok(line, "\n"); + addCount(c, lookupValue(kvPairs, line)); + } + free(line); + + if (fclose(f) != 0) { + fprintf(stderr, "Could not close file!\n"); + return NULL; + } + return c; +} + +int main(int argc, char **argv) { + // read the key/value pairs from the file named by argv[1] (call the result + // kv) + if (argc == 1) { + fprintf(stderr, "no parameters\n"); + return EXIT_FAILURE; + } + kvarray_t *kv = readKVs(argv[1]); + + // count from 2 to argc (call the number you count i) + for (int i = 2; i < argc; i++) { + // count the values that appear in the file named by argv[i], using kv as + // the key/value pair + // (call this result c) + counts_t * c = countFile(argv[i], kv); + + // compute the output file name from argv[i] (call this outName) + char *outName = computeOutputFileName(argv[i]); + + // open the file named by outName (call that f) + FILE *f = fopen(outName, "w"); + + // print the counts from c into the FILE f + printCounts(c, f); + + // close f + + // free the memory for outName and c + freeCounts(c); + free(outName); + } + + // free the memory for kv + freeKVs(kv); + + return EXIT_SUCCESS; +} diff --git a/34_put_together/main.c~ b/34_put_together/main.c~ new file mode 100644 index 0000000..4a3ac74 --- /dev/null +++ b/34_put_together/main.c~ @@ -0,0 +1,49 @@ +#include +#include +#include +#include "kv.h" +#include "counts.h" +#include "outname.h" + +counts_t * countFile(const char * filename, kvarray_t * kvPairs) { + FILE *f = fopen(filename, "r"); + if (f == NULL) { + fprintf(stderr, "Could not open file!\n"); + return NULL; + } + + counts_t * c = createCounts(); + + if (fclose(f) != 0) { + fprintf(stderr, "Could not close file!\n"); + return NULL; + } + return c; +} + +int main(int argc, char ** argv) { + //WRITE ME (plus add appropriate error checking!) + //read the key/value pairs from the file named by argv[1] (call the result kv) + + //count from 2 to argc (call the number you count i) + + //count the values that appear in the file named by argv[i], using kv as the key/value pair + // (call this result c) + + //compute the output file name from argv[i] (call this outName) + + + //open the file named by outName (call that f) + + //print the counts from c into the FILE f + + //close f + + //free the memory for outName and c + + + + //free the memory for kv + + return EXIT_SUCCESS; +} diff --git a/34_put_together/main.o b/34_put_together/main.o new file mode 100644 index 0000000..5754813 Binary files /dev/null and b/34_put_together/main.o differ diff --git a/34_put_together/outname.c b/34_put_together/outname.c new file mode 120000 index 0000000..d4e49b9 --- /dev/null +++ b/34_put_together/outname.c @@ -0,0 +1 @@ +../29_outname/outname.c \ No newline at end of file diff --git a/34_put_together/outname.h b/34_put_together/outname.h new file mode 120000 index 0000000..782a064 --- /dev/null +++ b/34_put_together/outname.h @@ -0,0 +1 @@ +../29_outname/outname.h \ No newline at end of file diff --git a/34_put_together/outname.o b/34_put_together/outname.o new file mode 100644 index 0000000..bc7aa49 Binary files /dev/null and b/34_put_together/outname.o differ diff --git a/34_put_together/vgcore.49132 b/34_put_together/vgcore.49132 new file mode 100644 index 0000000..f65d775 Binary files /dev/null and b/34_put_together/vgcore.49132 differ diff --git a/34_put_together/vgcore.49206 b/34_put_together/vgcore.49206 new file mode 100644 index 0000000..43d8df9 Binary files /dev/null and b/34_put_together/vgcore.49206 differ -- cgit v1.2.3