For mac gcc terminal. Please show the output with the script shell that will be
ID: 3687607 • Letter: F
Question
For mac gcc terminal. Please show the output with the script shell that will be provided. DO NOT ANSWER THE QUESTION IF YOUR CODE DOSEN'T COMPLILE.
Here is the code
#include
#include
#include
#include
#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
return NULL;
}
void search (struct dog *list)
{
//add your code here
}
void print(struct dog *list){
//add your code here
}
void clear(struct dog *list)
{
//add your code here
}
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;
}
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'
But here is a pic of the output just in case..
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.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);
void read_line(char str[], int n);
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)
{
struct dog *last = list;
struct dog *new_dog;
struct dog *p, *q;
int dog_number = 0;
printf("Enter patient's number: ");
scanf(" %d", &dog_number);
for(p = list; p != NULL; p = p->next)
{
if(dog_number == p->number)
{
printf("Patients number already exists...exiting now ");
exit(0);
}
}
new_dog = malloc(sizeof(struct dog));
new_dog->number = dog_number;
printf("Enter dog's name: ");
read_line(new_dog->dog_name, NAME_LEN+1);
new_dog->dog_name[0] = toupper(new_dog->dog_name[0]);
printf("Enter dog's breed: ");
read_line(new_dog->breed, NAME_LEN+1);
new_dog->breed[0] = toupper(new_dog->breed[0]);
printf("Enter owner's last name: ");
read_line(new_dog->owner_last_name, NAME_LEN+1);
new_dog->owner_last_name[0] = toupper(new_dog->owner_last_name[0]);
new_dog->next = NULL;
if(list == NULL)
{
return new_dog;
}
while(last->next != NULL)
{
last = last->next;
}
last->next = new_dog;
return list;
}
void search (struct dog *list)
{
char name_search[NAME_LEN+1];
struct dog *p;
int count=0,count1=0;
printf("Enter name to search: ");
scanf(" %s", name_search);
name_search[0] = toupper(name_search[0]);
for(p = list; p != NULL; p = p->next)
{
if(strcmp(name_search, p->dog_name) != 0)
{
count=count+1;
}
else
{
printf("%d %s %s %s ", p->number, p->dog_name, p->breed, p->owner_last_name);
count1=count1+1;
}
}
printf(" ");
if(count>=0 && count1==0)
{
printf("dog not found");
}
}
void print(struct dog *list)
{
struct dog *p;
printf("Number: Name: Breed: Owner's Last Name: ");
for(p = list; p != NULL; p = p->next)
{
printf("%d %s %s %s ", p->number, p->dog_name, p->breed, p->owner_last_name);
}
printf(" ");
}
void clear(struct dog *list)
{
struct dog *p;
while(list != NULL)
{
p = list;
list = list->next;
if( p!= NULL)
{
free(p);
}
}
printf("list is empty now");
}
void 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] = '';
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.