You are expected to complete this assignment individually. If you need help, you are invited to come to office hours and/or ask questions on piazza. Clarification questions about the assignments may be asked publicly. Once you have specific bugs related to your code, make the posts private.
This homework has several problems. We are also providing you with some resources on printf and error handling for this assignment.
Make a directory, hw5, for your work. Add that directory to your repository.
Then create empty files for all of the code files you are going to need to fill in. This makes it so that even if you're not done, the code will compile. Otherwise, the compiler will say that it can't find the files.
$ touch stack.h $ touch stack.c $ touch queue.h $ touch queue.c $ touch llist.h $ touch llist.c $ touch hw5.c $ touch hw5.h $ touch test_hw5.cNow add those to your repository so that when you commit, it will commit them all.
$ svn add *
You have already read in a file for which you knew the exact length. In this assignment, you are asked to read a file of unknown length. In order to help you out, here is code that reads in a file and then outputs every line to the screen.
#include <stdio.h > #include <stdlib.h> #define BUFSIZ 120 void display_file(char *filename) { FILE *fp; char buf[BUFSIZ] = "Garbage"; int i; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr,"Could not open file %s\n",filename); return (1); } i = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { printf ("Line %4d: %s", i, buf); i++; } fclose(fp); }
You have already practiced implementing linked list functions for individual problems. Now you are going to implement some very generic linked list functions, then use them to implement two classic abstract data structures: stack and queue.
This exercise as a few purposes:
The first step is to implement some linked list functions in c. You will place the code in llist.c. I have already created llist.h and the associated skeleton llist.c file for you to use. Copy and paste it into a file called llist.h. You are expected to implement a linked list slightly differently than before. Notice that there is a separate linked list struct. What we were implementing before as a llist is now called a node. The llist has two pointers - a pointer to the first node (head) and a pointer to the last node (tail). You will implement a linked list that has both a head pointer and a tail pointer. See llist.h for full function headers to tell you what to implement.
Place your test code in test_hw5.c. I have provided a skeleton main file that does nothing. I have also provided a Makefile that contains compilation for llist only, stack (and llist), queue (and llist), and everything together. You may inspect the file to see how to use it. Once you inplement the some test cases, then type the following.
$ make hw5To test it, type:
$ ./hw5
The second step is to implement a stack, building on your linked list structure and operations. Whenever possible, call a function that was already implemented for your linked list. You should not perform any changes directly to the linked list.
Copy this stack.h file into your stack.h file. Then copy the skeleton stack.c code into your stack.c file. Fill in the proper code. Add test cases to test_hw4.c, and you are ready to test.
The third step is to implement a queue, again building on your linked list structure and operations. Whenever possible, call a function that was already implemented for your linked list. You should not perform any changes directly to the linked list.
Copy this queue.h file into your queue.h file. Then copy the skeleton queue.c code into your queue.c file. Fill in the proper code. Add test cases to test_hw4.c, and you are ready to test.
1) It replaces all whitespace at the beginning of a line (whitespace consists of spaces or tabs) with the proper numbe of spaces, as defined by the input parameter. For example, if someone sets the space to 3, then each level of indentation will print 3 spaces.
2) It places all '{' and '}' on their own line at the indentation level of the code before the '{' and after the '}'.
Therefore, a file that looks like this:will become this:int main(){int x; printf("Hi!\n"); int i; for(i=0;i<10;i++){printf("%d\n",i);} if (i > 10) print("The loop worked!\n");}
Note the following simplifications from a full-working solution:int main() { int x; printf("Hi!\n"); int i; for(i=0;i<10;i++) { printf("%d\n",i); } if (i > 10) printf("The loop worked!\n"); }
void auto_indent(char *filename, int num_spaces);
Your code needs to open the filename for reading. In addition, it needs to create a new filename for writing. If the old filename was "file.txt", then the new file is "file_indent.txt". That is, it looks for the period and inserts "_indent" immediately before the period. If there is no period, then "_indent" is appended to the end of the filename.
You do *not* need to hand curly braces that are within quotation marks. This will not be a 100% fully functional program, but it will still be useful!
num_spaces is the number of spaces for each indentation level.
You may solve this in any order compared to check_closures.You are to write a function that checks code for matching closures. That is, it looks for matching '[',']', '(',')', and '{','}'. It ignores everything else in the file, except that it keeps track of the line on which each character was stored.
Place this in a file hw5.c. Place the function prototype into hw5.h. The signature is:
void match_closures(char *filename);
If all of the characters line up, nothing is printed out. If, however, there is interleaving (see below), then an error is printed. The execution would look like this:
int main() { int x; for(i=0;i<10;i++} { printf("%d\n",i); } }
Note the '}' on line 3 that should be a ')'.
When it encounters the '}' on line 3 (we number starting at 0), then it prints out:
Unexpected } encountered on line 3. Expected a match to ( on line 3. Matches with { on line 1.The first line of output is the character it was looking for (and line #).
If there is no open matching one waiting for a match, then do as follows below:
int main() { int x; for(i=0;i<10;i++] { printf("%d\n",i); } }
Note the ']' that should be a ')'.
When it encounters the ']' on line 3 (we number starting at 0), then it prints out:
Unexpected ] encountered on line 3. Expected a match to ( on line 3. No [ currently waiting for a match.
$ svn add hw4.h hw4.c test_*.c
$ svn commit -m "hw4 complete"