Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives 1. To learn how to design and implement functions for program develop

ID: 3692739 • Letter: O

Question

Objectives
1. To learn how to design and implement functions for program development
2. To reinforce how to use pass by value and pass by reference variables
3. To learn how to use structures

Problem: You are now building a program that will store a collection of dragons for your friend’s team. Your friend will need to be able to do the following:

1) Add a dragon to his team
2) Remove a dragon from his team
3) Search for a particular dragon
4) List all of the dragons of a particular color on the team

To make the testing of the program easier, your program will read in all of its input from a file called “dragon.txt”. Your friend’s team of dragon cannot exceed 1000 dragons.

You must complete all options:

(1) Adding dragons only
(2) Adding and removing dragons only
(3) Adding, removing, and searching dragons only
(4) All options implemented: adding, removing, searching, and listing dragons

Input Specification (dragon.txt)
The first line of the file will contain a single positive integer representing the number of updates to the dragon team. (Note: We assume that at the beginning of the program, the team has no dragons on it.) Each following line will have directions for a single update to the dragon team. The formats for these lines are as follows:

The very first value on each of these lines will be a string from the following subset: {ADD, REMOVE, SEARCH, LIST}. This string indicates which operation (corresponding to the list in the problem statement) is being designated.

If the operation is to add a dragon (ADD), then the line will contain the following information after ADD, separated by spaces:

NAME COLOR

The name and color of the dragon will be strings that contain only alphabetic letters and underscores. None of these strings will exceed 39 characters. No dragon will share a name with another dragon, though there may be multiple dragons of the same color.

If the operation is to remove a dragon (REMOVE), then the line will only contain the exact name of the dragon after REMOVE.

If the operation is to search for a dragon (SEARCH), then the line will only contain the exact name of the dragon that is being searched for.

If the operation is to list all of the dragons by a particular color (LIST), then the line will only contain the desired color after LIST.

Output Specification
All output should go to the screen. Separate the output for each command with one blank line. Here is the format for output for each command:

For adding a dragon, simply write out a statement of the form:
X the Y dragon has been added to the team.

For removing a dragon, simply write out a statement of the form:
X the Y dragon has been removed from the team.

For both of these, X represents the name of the dragon and Y represents the dragon’s color. You may assume that remove operations in the input file will always be valid. Thus, if a remove is ever requested, the dragon in question is guaranteed to be on the team.

For searching for a dragon, output one of two statements, depending on whether or not the designated dragon was found on the team:
X the dragon is currently on the team.
X the dragon is NOT currently on the team.

For searching for all the dragons of a particular color, output a single line of the form:
Y dragons:
Y represents the color requested. Each following line should list the name of one dragon of that color on the team. List each dragon exactly once.

Implementation Restrictions
You must use the following constants:
#define MAX_LENGTH 40
#define MAX_TEAM 1000

You must use the following structures to store information:

struct dragon {
char name[MAX_LENGTH];
char color[MAX_LENGTH];
};

struct collection {
struct dragon team[MAX_TEAM];
int num_dragons;
};

It is not sufficient to simply have the structures in your program, you must use them store information and process operations for the dragon team.

Though function prototypes will not be provided, it is expected that you follow good programming design and create several functions with well-specified tasks related to the solution of this problem.

Hints
Use pass by reference parameters for your functions. This will ensure that any change made to the dragon team in the function is reflected in main. Inside the function, remember to access the components using the -> syntax for structures.

Use the prewritten string functions when dealing with strings:
• use strcpy whenever you want to copy the contents of one string into another, instead of the equals sign.
• use strcmp to determine which operation is being requested.

Whenever you add a dragon to the team, make sure you add it to the first open slot on the team.

Whenever you remove a dragon from the team, make sure you copy the dragon in the last spot on the team into the vacated spot. For example, if the dragon to be removed is in index 3 and the number of dragons on the team before removal is 7, then the dragon in index 6 (the last filled index) should be copied into the dragon spot in index 3. Subsequently, the number of dragons on the team should be updated to 6.


Sample Input File (dragon.txt)
12
ADD TOOTHFUL BLACK
ADD SPITFIRE RED
SEARCH FURY
ADD FURY RED
ADD ICICLE BLUE
ADD NIGHTWING BLACK
LIST BLACK
REMOVE SPITFIRE
ADD STARFALL BLUE
LIST RED
LIST GREEN
LIST BLUE

Sample Output (corresponding to input file)
TOOTHFUL the BLACK dragon has been added to the team.

SPITFIRE the RED dragon has been added to the team.

FURY the dragon is NOT currently on the team.

FURY the RED dragon has been added to the team.

ICICLE the BLUE dragon has been added to the team.

NIGHTWING the BLACK dragon has been added to the team.

BLACK dragons:
TOOTHFUL
NIGHTWING

SPITFIRE the RED dragon has been removed from the team.

STARFALL the BLUE dragon has been added to the team.

RED dragons:
FURY

GREEN dragons:

BLUE dragons:
ICICLE
STARFALL

Complete without the use of malloc, fgets, menus.

Explanation / Answer


//Constants
#define MAX_LENGTH 40
#define MAX_TEAM 100


//Libraries
#include <stdio.h>
#include <string.h>

//Structures
struct dragon {
    char name[MAX_LENGTH];
    char color[MAX_LENGTH];
    };
struct collection {
    struct dragon team[MAX_TEAM];
    int num_dragons;
};
//Function Protoypes

void search_dragon(struct collection * team1, char * name);
void add_dragon(struct collection * team1, char * name, char * color);
void list_dragon(struct collection * team1, char * color);
void remove_dragon(struct collection * team1, char * name);

//Main function

int main(){
char color[MAX_LENGTH], name[MAX_LENGTH];
char commands[MAX_LENGTH];
struct collection dteam;
int num_commands=0;
dteam.num_dragons=0;

// Open file
    //make sure the textfile named "dragon.txt" is in the same directory as the program.
    FILE * ifp = fopen("dragon.txt", "r");
    int i;

    if(ifp == NULL) {
        printf("Failure to open file. Verify file path. ");
            return 1;}

    fscanf(ifp, "%d", &num_commands);


    for (i=0; i<num_commands; i++) {
        fscanf (ifp, "%s", commands);


    //String compare to ADD
    if (strcmp(commands,"ADD")== 0)
            {
                fscanf(ifp,"%s",name);
                fscanf(ifp, "%s", color);
                add_dragon(&dteam, name, color);
            }

    //String Compare to LIST.

    if (strcmp(commands,"LIST")== 0)
            {
                fscanf (ifp, "%s", color);
                list_dragon(&dteam, color);
            }

    //String Compare to SEARCH.

    if (strcmp(commands,"SEARCH")==0)
            {
                fscanf(ifp,"%s", name);
                search_dragon(&dteam, name);
            }

    //String compare to REMOVE.

    if (strcmp(commands,"REMOVE")== 0)
            {
                fscanf(ifp,"%s",name);
                remove_dragon(&dteam, name);
            }

    }
        fclose(ifp);
    return 0;

}

//Add a Dragon

    void add_dragon(struct collection * team1, char * name, char * color)
    {
    int i;

    printf(" %s the %s dragon has been added to the team. ",name, color);


    strcpy(team1->team[team1->num_dragons].name, name);
    strcpy(team1->team[team1->num_dragons].color, color);

    team1->num_dragons++;


    return;

    }

//Remove a Dragon

void remove_dragon(struct collection * team1, char * name)
    {
        int i;

    for(i=0; i<team1->num_dragons; i++)
        {
        if (strcmp(team1->team[i].name, name)== 0){

        printf(" %s the %s dragon has been removed from the team. ",team1->team[i].name, team1->team[i].color);

        strcpy(team1->team[i].name, team1->team[team1->num_dragons-1].name);
        strcpy(team1->team[i].color, team1->team[team1->num_dragons-1].color);
        }

    }
    team1->num_dragons--;
        return;
    }

//Search for a dragon

void search_dragon(struct collection * team1, char * name)
    {
    int i, flag=0;
        for(i=0; i<team1->num_dragons; i++)
        {
            if (strcmp(team1->team[i].name, name)== 0)
            {
                printf("%s the dragon is currently on the team ", name);
                flag=1;
            }
        }
        if (flag==0)
        {
            printf("%s the dragon is NOT currently on the team ", name);
        }
    return;
}


//List dragon

   void list_dragon(struct collection * team1, char * color)
   {
       int i;

    printf(" %s dragons: ", color);
        for(i=0; i<team1->num_dragons; i++)
           {
               if (strcmp(team1->team[i].color, color)== 0)

                printf("%s ", team1->team[i].name);
           }
        return;
    }

dragon.txt
12
ADD TOOTHFUL BLACK
ADD SPITFIRE RED
SEARCH FURY
ADD FURY RED
ADD ICICLE BLUE
ADD NIGHTWING BLACK
LIST BLACK
REMOVE SPITFIRE
ADD STARFALL BLUE
LIST RED
LIST GREEN
LIST BLUE


sample output

TOOTHFUL the BLACK dragon has been added to the team.                                                                                                       
                                                                                                                                                            
                                                                                                                                                            
SPITFIRE the RED dragon has been added to the team.                                                                                                         
                                                                                                                                                            
FURY the dragon is NOT currently on the team                                                                                                                
                                                                                                                                                            
                                                                                                                                                            
FURY the RED dragon has been added to the team.                                                                                                             
                                                                                                                                                            
                                                                                                                                                            
ICICLE the BLUE dragon has been added to the team.                                                                                                          
                                                                                                                                                            
                                                                                                                                                            
NIGHTWING the BLACK dragon has been added to the team.                                                                                                      
BLACK dragons:                                                                                                                                              
TOOTHFUL                                                                                                                                                    
NIGHTWING                                                                                                                                                   
                                                                                                                                                            
SPITFIRE the RED dragon has been removed from the team.                                                                                                     
                                                                                                                                                            
                                                                                                                                                            
STARFALL the BLUE dragon has been added to the team.                                                                                                        
                                                                                                                                                            
                                                                                                                                                            
RED dragons:                                                                                                                                                
FURY                                                                                                                                                        
                                                                                                                                                            
GREEN dragons:                                                                                                                                              
                                                                                                                                                            
BLUE dragons:                                                                                                                                               

ICICLE
STARFALL