PART ONE. Modify roster.c (code below) so that the program is split into three s
ID: 3762721 • Letter: P
Question
PART ONE. Modify roster.c (code 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 players into player.c
2) Create a header file named player.h that contains struct playerdeclaration and prototypes for the functions in player.c. The headerfile 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 theread_line function. The header file should enclose the contents of theheader file in an #ifndef-#endif pair to protect the file.
5) roster.c contains the main function.
6) Include appropriate header files in the source files.
PART TWO. Write a makefile to build the roster program on circe. The makefile shouldcontain the following rules:
1) Build readline.o by compiling readline.c
2) Build player.o by compiling player.c
3) Build roster.o by compiling roster.c
4) Build roster by linking readline.o, player.o, and roster.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 roster.
****THIS IS ROSTER.C****:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 30
struct player{
int number;
char first_name[NAME_LEN+1];
char last_name[NAME_LEN+1];
struct player *next;
};
struct player *append_to_list(struct player *roster);
void find_player(struct player *roster);
void printList(struct player *roster);
void clearList(struct player *roster);
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 player *team_roster = NULL;
printf("Operation Code: a for appending to the roster, f for finding a player"
", p for printing the roster; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': team_roster = append_to_list(team_roster);
break;
case 'f': find_player(team_roster);
break;
case 'p': printList(team_roster);
break;
case 'q': clearList(team_roster);
return 0;
default: printf("Illegal code ");
}
printf(" "); }
}
struct player *append_to_list(struct player *roster){
struct player *x, *temp;
char first[NAME_LEN+1], last[NAME_LEN+1];
int num, found;
x = roster;
printf("Enter the first name of the player: ");
scanf("%s", first);
printf("Enter the last name of the player: ");
scanf("%s", last);
printf("Enter the number of the player: ");
scanf("%i", &num);
found = 0;
while(x != NULL)
{
if(x->number == num)
found = 1;
x = x->next;
}
if(found)
{
printf("Player with this number already exists... ");
return roster;
}
else
{
temp = (struct player *)malloc(sizeof(struct player));
strcpy(temp->first_name, first);
strcpy(temp->last_name, last);
temp->number = num;
temp->next = NULL;
if(roster == NULL)
{
return temp;
}
else
{
x = roster;
while(x->next != NULL)
x = x->next;
x->next = temp;
return roster;
}
}
}
void find_player(struct player *roster){
int num;
printf("Enter the number of the player to search for: ");
scanf("%i", &num);
while(roster != NULL)
{
if(roster->number == num)
{
printf("%s %s", roster->last_name, roster->first_name);
break;
}
roster = roster->next;
}
if(roster == NULL)
printf("The player does not exist... ");
}
void printList(struct player *roster){
while(roster != NULL)
{
printf("%s %s %i ", roster->last_name, roster->first_name, roster->number);
roster = roster->next;
}
}
void clearList(struct player *roster){
struct player *x;
while(roster != NULL)
{
x = roster;
roster = roster->next;
free(x);
}
}
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;
}
Explanation / Answer
// Makefile
all: roster
readline: readline.c
gcc readline.c -o readline.o
player: player.c
gcc player.c -o player.o
roster: roster.c
gcc roster.c -o roster.o
roster: readline.o player.o roster.o
gcc readline.o player.o roster.o -o roster
// roster.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "readline.h"
#include "player.h"
/**********************************************************
* 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 player *team_roster = NULL;
printf("Operation Code: a for appending to the roster, f for finding a player"
", p for printing the roster; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': team_roster = append_to_list(team_roster);
break;
case 'f': find_player(team_roster);
break;
case 'p': printList(team_roster);
break;
case 'q': clearList(team_roster);
return 0;
default: printf("Illegal code ");
}
printf(" "); }
}
// readline.h
#ifndef READLINE_H
#define READLINE_H
#include <ctype.h>
#include <stdio.h>
int read_line(char str[], int n);
#endif
// readline.c
#include "readline.h"
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;
}
// player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 30
struct player{
int number;
char first_name[NAME_LEN+1];
char last_name[NAME_LEN+1];
struct player *next;
};
struct player *append_to_list(struct player *roster);
void find_player(struct player *roster);
void printList(struct player *roster);
void clearList(struct player *roster);
#endif
// player.c
#include "player.h"
struct player *append_to_list(struct player *roster){
struct player *x, *temp;
char first[NAME_LEN+1], last[NAME_LEN+1];
int num, found;
x = roster;
printf("Enter the first name of the player: ");
scanf("%s", first);
printf("Enter the last name of the player: ");
scanf("%s", last);
printf("Enter the number of the player: ");
scanf("%i", &num);
found = 0;
while(x != NULL){
if(x->number == num)
found = 1;
x = x->next;
}
if(found){
printf("Player with this number already exists... ");
return roster;
}
else{
temp = (struct player *)malloc(sizeof(struct player));
strcpy(temp->first_name, first);
strcpy(temp->last_name, last);
temp->number = num;
temp->next = NULL;
if(roster == NULL){
return temp;
}
else{
x = roster;
while(x->next != NULL)
x = x->next;
x->next = temp;
return roster;
}
}
}
void find_player(struct player *roster){
int num;
printf("Enter the number of the player to search for: ");
scanf("%i", &num);
while(roster != NULL){
if(roster->number == num){
printf("%s %s", roster->last_name, roster->first_name);
break;
}
roster = roster->next;
}
if(roster == NULL)
printf("The player does not exist... ");
}
void printList(struct player *roster){
while(roster != NULL){
printf("%s %s %i ", roster->last_name, roster->first_name, roster->number);
roster = roster->next;
}
}
void clearList(struct player *roster){
struct player *x;
while(roster != NULL){
x = roster;
roster = roster->next;
free(x);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.