Here is the code Here is the try_dogs file which is a Unix shell script to test
ID: 3687458 • Letter: H
Question
Here is the code
Here is the try_dogs file which is a Unix shell script to test the file......
# Program Design
# try_dogs is a Unix shell script that will be used to test project 10.
# To use the script, copy it into the same directory as your scource file
# Set execute permission for the file by issuing the command:
# chmod +x try_dogs
# Compile your program, producing a.out as the executable
# To run the script, type
# ./try_dogs
# The user input from the script will not be shown on the screen.
# Compare the results from your program with the expected results on the test cases.
echo '===================================================='
#
./a.out <<-EndOfInput
a
73
Max
Bulldog
White
a
65
Molly
Golden Retriever
Smith
a
69
Max
Poodle
Johnson
a
65
a
36
Lucy
Yorkshire
Jones
p
s
Buddy
s
Max
q
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'opertion code: a'
echo 'Enter patient number: 73'
echo 'Enter dog name: Max'
echo 'Enter breed: Bulldog'
echo 'Enter owner last name: White'
echo 'Enter operation code: a'
echo 'Enter patient number: 65'
echo 'Enter dog name: Molly'
echo 'Enter breed: Golden Retriever'
echo 'Enter owner last name: Smith'
echo 'Enter operation code: a'
echo 'Enter patient number: 69'
echo 'Enter dog name: Max'
echo 'Enter breed: Poodle'
echo 'Enter owner last name: Johnson'
echo 'opertion code: a'
echo 'Enter player number: 65'
echo 'patient already existed.'
echo 'Enter operation code: a'
echo 'Enter patient number: 36'
echo 'Enter dog name: Lucy'
echo 'Enter breed: Yorkshire'
echo 'Enter owner last name: Jones'
echo 'Enter operation code: p'
echo 'Number Name Breed Owner Last Name'
echo '73 Max Bulldog White'
echo '65 Molly Golden Retriever Smith'
echo '69 Max Poodle Johnson'
echo '36 Lucy Yorkshire Jones'
echo 'Enter operation code: f'
echo 'Enter dog name: Buddy'
echo 'Dog not found.'
echo 'Enter operation code: f'
echo 'Enter dog name: Max'
echo '73 Max Bulldog White'
echo '69 Max Poodle Johnson'
echo 'Enter operation code: q'
From here you will understand what output is required.But here is a picture
The program dogs.c maintains records for canine patients at an animal hospital. Each dog's record has a name, a breed, a patient number, and owner's last name. Complete the program so it uses a dynamically allocated linked list to store the records and contains the following functions: append: ask the user to enter patient number, dog's name, dog's breed, and owner's last name, then add the player to the end of the linked list. 1. It should check whether the dog has already existed by patient number. If so, the function should print a message and exit. If the dog does not exist, allocate memory for the dog, store the data, and append the dog to the end of the linked list. a. b. c. If the list is empty, the function should return the pointer to the newly created dog. d. Otherwise, add the dog to the end of the linked list and return the pointer to the linked ist. search: find the dog by name, print all the dog's information that matches the name. If the dog is not found, print a message. print: print the name and number of all the dogs. clear: when the user exists the program, all the memory allocated for the linked list should be deallocated. 2. 3. 4. Note: use read_line function included in the program for reading in dog names, breeds, and owner last names.Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <ctype.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;
};
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);
/**********************************************************
* 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. *
**********************************************************/
int main(void)
{
char code;
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(" ");
}
}
struct dog *append(struct dog *list){
//add your code here and remove the return NULL; statement
struct dog* temp, *temp2;
temp=(dog *)malloc(sizeof(struct dog));
printf("Enter Patient no.: ");
scanf("%d",&temp->number);
printf("Enter dogs name: ");
read_line(temp->dog_name,20);
printf("Enter dogs breed: ");
read_line(temp->breed,20);
printf("Enter owner last name: ");
read_line(temp->owner_last_name,20);
temp->next=NULL;
if(list==NULL)
{
list=temp;
list->next=NULL;
}
else
{
temp2=list;
while(temp2->next!=NULL)
{
temp2=temp2->next;
printf("%d", temp2->number);
}
temp2->next=temp;
}
return list;
}
void search (struct dog *list)
{
char s[NAME_LEN+1];
bool f=false;
printf("Enter name: ");
read_line(s,20);
while(list!=NULL)
{
if(strcmp(s,list->dog_name)==0)
{
printf("%d %s %s %s ",list->number, list->dog_name, list->breed, list->owner_last_name);
f=true;
}
list=list->next;
}
if(!f) printf("No match Found ");
}
void print(struct dog *list){
printf("Number Name Breed Owner ");
while(list!=NULL)
{
printf("%d %s %s %s ",list->number, list->dog_name, list->breed, list->owner_last_name);
list=list->next;
}
}
void clear(struct dog *list)
{
list=NULL;
exit(0);
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}