Question: Modify dealer.c (code provided below) so that the program is split into three source files and two header files. 1) Put all functions related to

Modify dealer.c (code provided below) 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 cars into car.c.

2) Create a header file named car.h that contains struct car declaration and prototypes for the functions in car.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 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) dealer.c contains the main function.

6) Include appropriate header files in the source files.

Code for dealer.c:

#include #include #include #include

#define LEN 30

struct car{ char make[LEN+1]; char model[LEN+1]; char color[LEN+1]; int year; int city_mpg; int highway_mpg; int quantity; struct car *next; };

struct car *append_to_list(struct car *list); void find_car(struct car *list); void printList(struct car *list); void clearList(struct car *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 car *car_list = NULL; printf("Operation Code: a for appending to the list, f for finding a car, 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': car_list = append_to_list(car_list); break; case 'f': find_car(car_list); break; case 'p': printList(car_list); break; case 'q': clearList(car_list); return 0; default: printf("Illegal code "); } printf(" "); } }

struct car *append_to_list(struct car *list) { struct car *newCar = (struct car*)malloc(sizeof(struct car)); newCar->next = NULL;

printf(" Enter Make: "); read_line(newCar->make, LEN); printf(" Enter Model: "); read_line(newCar->model, LEN); printf(" Enter Color: "); read_line(newCar->color, LEN); printf(" Enter year: "); scanf("%d", &newCar->year); printf(" Enter City MPG: "); scanf("%d", &newCar->city_mpg); printf(" Enter Highway MPG: "); scanf("%d", &newCar->highway_mpg); printf(" Enter Quantity: "); scanf("%d", &newCar->quantity);

if(list == NULL) { return newCar; } else { struct car* ptr = list; while(1) { if((strcmp(ptr->make, newCar->make)==0) && (strcmp(ptr->model, newCar->model)==0) &&(strcmp(ptr->color, newCar->color)==0) &&(ptr->year == newCar->year)) { printf(" Car already exists in the list."); return list; } if(ptr->next != NULL) { ptr = ptr->next; } else { break; } }

ptr->next = newCar;

return list; }

return NULL; }

void find_car(struct car * list) { char userModel[LEN+1], userMake[LEN+1]; printf(" Enter make to search for: "); read_line(userMake, LEN); printf(" Enter model to search for: "); read_line(userModel, LEN);

int found = 0; struct car* ptr = list; while(ptr != NULL) { if((strcmp(ptr->make, userMake)==0) &&(strcmp(ptr->model, userModel)==0)) { struct car *c = ptr;

printf(" Color: %s, Year: %d, City_mpg: %d, Highway_mpg: %d, Quantity: %d", c->color, c->year, c->city_mpg, c->highway_mpg,c->quantity); found = 1; }

ptr = ptr->next; }

if(!found) { printf(" Car not found."); } }

/* Name of the function: printList Purpose of the function: Print the car's make, model, and manufacture year, and color of all the cars. Meaning of each parameter: struct car *list points to the struct car Description of return value: No return value because function is void type. Description of side effects: None. */ void printList(struct car *list) { struct car* ptr = list;

printf(" Make\tModel\t Color\tYear\tCity\tHighway\tQuantity\t"); while(ptr != NULL) { struct car *c = ptr;

printf(" %s\t%-9s%s\t%d\t%d\t%d\t%d", c->make, c->model, c->color, c->year, c->city_mpg, c->highway_mpg, c->quantity); ptr = ptr->next; } }

void clearList(struct car *list) { struct car* ptr = list;

while(ptr != NULL) { struct car *c = ptr; ptr = ptr->next; free(c); } }

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] = '\0'; return i; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!