Question
1.) Modify the below program dealer.c so that the program is split into three source files and two header files. A) Put all functions related
1.) Modify the below program dealer.c so that the program is split into three source files and two header files.
A) Put all functions related to operations on the list of cars into car.c.
B) 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.
C) Put the read_line function in a separate file named readline.c.
D) 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.
E) dealer.c contains the main function.
F) Include appropriate header files in the source files.
2.) Write a makefile to build the program on student cluster. The makefile should contain the following rules:
A) Build readline.o by compiling readline.c.
B) Build car.o by compiling car.c.
C) Build dealer.o by compiling dealer.c
D) Build dealer by linking readline.o, car.o, and dealer.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 dealer.
#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) && (ptr->city_mpg == newCar->city_mpg) && (ptr->highway_mpg == newCar->highway_mpg) && (ptr->quantity == newCar->quantity) ) { 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("Car Model: %s, Make: %s, Color: %s, Year: %d, quantity: %d ", c->model, c->make, c->color, c->year, c->quantity); found = 1; } ptr = ptr->next; } if(!found) { printf("No matching car found. "); } //add your code here } void printList(struct car *list){ struct car* ptr = list; while(ptr != NULL) { struct car *c = ptr; printf("Car Model: %s, Make: %s, Color: %s, Year: %d, quantity: %d", c->model, c->make, c->color, c->year, 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); } printf("List cleared "); } 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
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