Question
1. (60 points) Modify Project 9 dogs.c program so that the program is split into three source files and two header files. 1) Put all
1. (60 points) Modify Project 9 dogs.c program so that the program is split into three source files and two header files. 1) Put all functions related to operations on the list of dogs into dogs.c 2) Create a header file named dogs.h that contains struct dog declaration and prototypes for the functions in dogs.c. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file. 3) Put the read_line function is in a separate file named readline.c. 4) Create a header file named readline.h that contains a prototype for the read_line function. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file. 5) records.c contains the main function. 6) Include appropriate header files in the source files. 2. (40 points) Write a makefile to build the part 1 program on student cluster. The makefile should contain the following rules: 1) Build readline.o by compiling readline.c 2) Build dogs.o by compiling dogs.c 3) Build records.o by compiling records.c 4) Build records by linking readline.o, dogs.o, and records.o Each rule should include the name of the target file, dependencies among files, and the command to be executed. The makefile should name the executable file for the program records. Before you submit: 1. (part 1) Compile with the following command and test with try_dogs gcc Wall dogs.c readline.c records.c 2. (part 2) Be sure your makefile contains the information necessary to build the dogs program. Test your makefile: make records ./records 3. Submit dogs.c, dogs.h, readline.c, readline.h, records.c for part 1 and makefile for part 2.
#ifndef dogs_h #define dogs_h #define NAME_LEN 30 struct dog{ int number; char dog_name[NAME_LEN+1]; char owner_last_name[NAME_LEN+1]; char breed[NAME_LEN+1]; struct dog *next; };
/*function prototypes*/ struct dog *append(struct dog *list); void search(struct dog *list); void print(struct dog *list); void clear(struct dog *list); int read_line(char str[], int n); #endif /********************************************************** * main: Prompts the user to enter an operation code, * * then calls a function to perform the requested * * action. Repeats until the user enters the * * command 'q'. Prints an error message if the user * * enters an illegal code. * **********************************************************/ #include
struct dog *dog_list = NULL; printf("Operation Code: a for appending to the list, s for finding a dog" ", p for printing the list; q for quit. "); for (;;) { printf("Enter operation code: "); scanf(" %c", &code); while (getchar() != ' ') /* skips to end of line */ ; switch (code) { case 'a': dog_list = append(dog_list); break; case 's': search(dog_list); break; case 'p': print(dog_list); break; case 'q': clear(dog_list); return 0; default: printf("Illegal code "); } printf(" "); } }
/************************************************************ * append: Prompts the user for informaiton about a dog and * * then append the dog structure to the end of the list. * * Prints an error message and returns prematurely if the * * dog already exists by its patient number or memory * * could not be allocated for the dog structure. * * *********************************************************/ #include
new_node = malloc(sizeof(struct dog)); if (new_node == NULL) { printf("Database is full; can't add more dogs. "); return list; }
printf("Enter dog's patient number: "); scanf("%d", &new_node->number);
for (cur = list;cur != NULL;cur = cur->next) if (cur != NULL && new_node->number == cur->number) { printf("Patient already exists. "); free(new_node); return list; }
printf("Enter dog's name: "); read_line(new_node->dog_name, NAME_LEN); printf("Enter dog's breed: "); read_line(new_node->breed, NAME_LEN); printf("Enter owner's last name: "); read_line(new_node->owner_last_name, NAME_LEN); new_node->next = NULL;
if(list == NULL) { list = new_node; return list; } else{ for(cur = list; cur->next!= NULL; cur = cur->next); cur->next = new_node; return list; }
}
/*********************************************************** * search: Prompts the user to enter a dog's name, then * * looks up dog(s) by name in the list. Prints the all the * * informaiton of the dogs with the name if found. * * Otherwise, prints a message. * * ********************************************************/
void search (struct dog *list) { char search_name[NAME_LEN+1]; struct dog *p; int found =0; printf("Enter dog's name: "); read_line(search_name, NAME_LEN);
for(p=list; p != NULL; p = p->next) { if(strcmp(search_name, p->dog_name)==0){ found = 1; printf("%d\t", p->number); printf("%s\t", p->dog_name); printf("%s\t", p->breed); printf("%s ", p->owner_last_name); } } if(!found) printf("dog not found. ");
}
/************************************************************ * print: Prints a listing of all dogs in the list, showing * * the dog's patient number, name, breed, and owner's last * * name. * * *********************************************************/
void print(struct dog *list){
struct dog *p;
printf("Dog Number\tDog Name\t" "Dog Breed\tOwner Last Name "); for (p = list; p != NULL; p = p->next) printf("%d\t\t%s\t\t%s\t\t%s ", p->number, p->dog_name,p->breed, p->owner_last_name);
} /*************************************************************** * clear: Clears the entire linked list. It begins at the head * * of the list and frees memory allocated for each node of the * * linked list. * * ************************************************************/
void clear(struct dog *list) { struct dog *p;
while(list!=NULL) { p = list; list = list->next; if(p!=NULL) free(p); }
}
/*************************************************************** * read_line: Skips leading white-space characers, then reads * * the remainder of the input line and stores it in str. * * Truncate the line if its length exceeds n. Returns the * * number of characters stored. * * ************************************************************/ #ifndef readline_h #define readline_h #define NAME_LEN 30 int read_line(char str[], int n); #endif #include
while (isspace(ch = getchar())) ; str[i++] = ch; while ((ch = getchar()) != ' ') { if (i < n) str[i++] = ch; } str[i] = '\0'; return i; }
this is what i did so far and when i do gcc -Wall dogs.c records.c and readline.c it says that the records and readline files are not available and so are readline.h and dog.h. can anybody please help me
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started