. 我建议你从课本或在线教程开始学习这门语言。C教材:。在线教程:。 //included libraries #include <stdio.h> #include <string.h> //constants #defi
. 我建议你从课本或在线教程开始学习这门语言。C教材:。在线教程:。. 我建议你从课本或在线教程开始学习这门语言。C教材:。在线教程:。 //included libraries #include <stdio.h> #include <string.h> //constants #defi,c,file,function,struct,structure,C,File,Function,Struct,Structure,. 我建议你从课本或在线教程开始学习这门语言。C教材:。在线教程:。 //included libraries #include <stdio.h> #include <string.h> //constants #define MAX_LENGTH 40 #define MAX_TEAM 1000 //function prototypes int add_dragon(char name, char color, int num_dragons, int
//included libraries
#include <stdio.h>
#include <string.h>
//constants
#define MAX_LENGTH 40
#define MAX_TEAM 1000
//function prototypes
int add_dragon(char name, char color, int num_dragons, int Dragon_array);
//structures
struct dragon {
char name[MAX_LENGTH];
char color[MAX_LENGTH];
};
struct collection {
struct dragon team[MAX_TEAM];
int num_dragons;
};
//main function
int main() {
//list variables in main function
int i, j, num_dragons, choice = 0;
//set up file
FILE * ifp = fopen("dragon.txt", "r");
//check to see if there is a file
if(ifp == NULL){
printf("Error! No file can be found!");
return 1;
}
printf("Welcome! You are about to start collecting dragons!\n");
//while loop for repeating the menu
while(num_dragons < MAX_TEAM){
printf("What would you like to do to your team of dragons?\n\n");
printf("\t 1- Add to team\n");
printf("\t 2- Remove from team\n");
printf("\t 3- Search for dragon on team\n");
printf("\t 4- List dragons\n");
scanf("%d", & choice);//enter choice
//conditions to prompt for a valid choice
while(choice > 4)
scanf("%d", &choice);
while(choice < 1)
scanf("%d", &choice);
//enter dragon name and color
fscanf(ifp, "%s", &num_dragons);
}
return 0;
}
//Pre_condition: choivr us set to zero
//Post_condition allows user to select a choice
//Preconditions: menu is set up and variables are declared
//postConditions: will allow user to add a dragon to their team
int add_dragon(char name, char color, int num_dragons, int Dragon_array){
//variables
int i, j;
//if(num_dragons < MAX_TEAM)//check if numer of dragons doesn't exceed max size of the team{
Dragon_array [MAX_TEAM] [MAX_TEAM];
for(i = 0; i < MAX_TEAM; i ++)
for(j = 0; j < MAX_TEAM; j ++){
num_dragons ++;
}
printf("%s the %s dragon has been added to the team\n", name, color);// confirm that the dragon was added to team
return num_dragons;
}
// note:
// 1) there is a lot of code missing from main
// 2) most functions to handle the menu choice value are missing
// 3) the function: add_dragon() is never called
//included header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//constants
#define MAX_LENGTH (40)
#define MAX_TEAM (1000)
//function prototypes
int add_dragon(char* pName, char* pColor );
//structures
struct dragon
{
char name[MAX_LENGTH];
char color[MAX_LENGTH];
};
struct collection
{
struct dragon team[MAX_TEAM];
int num_dragons;
};
// global instance of struct collection.
// you 'may' want to place this instance in main
// then pass a pointer to myCollection to each of the subroutines
struct collection myCollection;
//main function
int main()
{
//list variables in main function
int choice = 0;
// initialize to no dragons
myCollection.num_dragons = 0;
memset( myCollection.team, 0x00, MAX_TEAM*sizeof(structDragon) );
//set up file
FILE * ifp = fopen("dragon.txt", "r");
if(ifp == NULL)
{ // then, fopen failed
perror("fopen failed for reading dragon.txt");
return 1;
}
// implied else, fopen for read successful
printf("Welcome! You are about to start collecting dragons!\n");
//while loop for repeating the menu
while(myCollection.num_dragons < MAX_TEAM)
{
printf("What would you like to do to your team of dragons?\n\n");
printf("\t 1- Add to team\n");
printf("\t 2- Remove from team\n");
printf("\t 3- Search for dragon on team\n");
printf("\t 4- List dragons\n");
do
{
if( 1 != scanf(" %d", & choice) )//enter choice // note leading ' ' in format string
{
perror( "scanf failed while getting choice");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
} while( (4<choice) &&(1>choice) );
//enter dragon name and color
//fscanf(ifp, " %s", &num_dragons);
// !!! code seems to be missing here !!!
} // end while
return 0;
} // end function: main
//Pre_condition: choivr us set to zero
//Post_condition allows user to select a choice
//Preconditions: menu is set up and variables are declared
//postConditions: will allow user to add a dragon to their team
int add_dragon( char* pName, char* pColor )
{
//variables
if( MAX_TEAM > myCollection.num_dragons )
{ // then, room for another dragon
strcpy( myCollection.team[myCollection.num_dragons].name, pName );
strcpy( myCollection.team[myCollection.num_dragons].color, pColor );
myCollection.num_dragons++;
} // end if
printf("%s the %s dragon has been added to the team\n", pName, pColor);// confirm that the dragon was added to team
return myCollection.num_dragons;
} // end function: add_dragon