如何搜索struct C编程数组

时间:2017-07-19 13:33:51

标签: c structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN_LINE 80
#define LEN_NAME 30
#define MAX_LINES 30
#define LEN_POS 5
#define LEN_HAND 5
#define LEN_THROW 5
#define LEN_STATUS 20
#define LEN_MLBSTATUS 5

typedef struct
{
    int jerseyNumber;
    char firstName[LEN_NAME + 1];
    char lastName[LEN_NAME + 1];
    char position[LEN_POS + 1];
    char battingHand[LEN_HAND + 1];
    char throwingHand[LEN_THROW + 1];
    int birthYear, birthMonth, birthDay, heightFeet, heightInches, weight;
    char status[LEN_STATUS + 1];
    char mlbStatus[LEN_MLBSTATUS + 1];


}player_t;

//protype
void displayPlayer(player_t *aPlayerPtr);


int main(void)
{
    FILE * filePtr;
    int index, count;
    char line[LEN_LINE + 1];
    player_t players[MAX_LINES];
    filePtr = fopen("Shrimp.txt","r");
    if (filePtr == NULL)
    {
        printf("Unable to open file.\n");
    }
    else
    {
        index = 0, count;
        while(index < MAX_LINES && fgets(line, LEN_LINE, filePtr))
        {
            if(14 == sscanf(line, "%i %s %s %s %s %s %i %i %i %i %i %i %s %s",
                           &players[index].jerseyNumber, players[index].lastName,
                           players[index].firstName, players[index].position,
                           players[index].battingHand, players[index].throwingHand,
                           &players[index].birthYear, &players[index].birthMonth,
                           &players[index].birthDay, &players[index].heightFeet,
                           &players[index].heightInches, &players[index].weight,
                           players[index].status, players[index].mlbStatus)

               )
            {
                index++;
            }
        }
    fclose(filePtr);
    count = index;

    for(index = 0; index < count; index = index + 1)
    {
        displayPlayer(&players[index]);
    }


    }
    return 0;
}

void displayPlayer(player_t *aPlayerPtr)
{
    printf("Player %i and his name is %s , %s. His weight is %i and position %s. His Status is %s and his mlb status %s \n",
           aPlayerPtr->jerseyNumber, aPlayerPtr->lastName, aPlayerPtr->firstName,
           aPlayerPtr->weight, aPlayerPtr->position, aPlayerPtr->status, aPlayerPtr->mlbStatus
           );
}

好吧所以我设计了一个程序,用于将文件中的信息读入结构数组。我想能够让用户使用姓名或球衣号码搜索球员信息。如果有人能指出我,我将不胜感激在正确的方向。

2 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN_LINE 80
#define LEN_NAME 30
#define MAX_LINES 30
#define LEN_POS 5
#define LEN_HAND 5
#define LEN_THROW 5
#define LEN_STATUS 20
#define LEN_MLBSTATUS 5
#define MAX_PLAYERS 26

typedef struct
{
    int jerseyNumber;
    char firstName[LEN_NAME + 1];
    char lastName[LEN_NAME + 1];
    char position[LEN_POS + 1];
    char battingHand[LEN_HAND + 1];
    char throwingHand[LEN_THROW + 1];
    int birthYear, birthMonth, birthDay, heightFeet, heightInches, weight;
    char status[LEN_STATUS + 1];
    char mlbStatus[LEN_MLBSTATUS + 1];


}player_t;

//protype
void displayPlayer(player_t *aPlayerPtr);


int main(void)
{
    FILE * filePtr;
    int index, count;
    char line[LEN_LINE + 1];
    player_t players[MAX_LINES];
    filePtr = fopen("Shrimp.txt","r");
    if (filePtr == NULL)
    {
        printf("Unable to open file.\n");
    }
    else
    {
        index = 0, count;
        while(index < MAX_LINES && fgets(line, LEN_LINE, filePtr))
        {
            if(14 == sscanf(line, "%i %s %s %s %s %s %i %i %i %i %i %i %s %s",
                           &players[index].jerseyNumber, players[index].firstName,
                           players[index].lastName, players[index].position,
                           players[index].battingHand, players[index].throwingHand,
                           &players[index].birthYear, &players[index].birthMonth,
                           &players[index].birthDay, &players[index].heightFeet,
                           &players[index].heightInches, &players[index].weight,
                           players[index].status, players[index].mlbStatus)

               )
            {
                index++;
            }
        }

    fclose(filePtr);
    count = index;

    for(index = 0; index < count; index = index + 1)
    {
        displayPlayer(&players[index]);
    }

    int jerseyNumber;
    int i;


    printf("Enter jersey number for the player: ");
    scanf("%i",&jerseyNumber);

    for(i=0;i<=MAX_PLAYERS;i++){
        if(jerseyNumber == players[i].jerseyNumber){
            printf("The player's name is %s %s\n",players[i].firstName, players[i].lastName);
        }
    }

    }
    return 0;
}

void displayPlayer(player_t *aPlayerPtr)
{
    printf("JERSEY: %i PLAYER NAME: %s %s POSITION: %s BATTING HAND: %s THROWING HAND: %s BIRTHDAY: %i/%i/%i HEIGHT: %i'%i WEIGHT: %i STATUS: %s MLB 40-STATUS: %s\n\n",
           aPlayerPtr->jerseyNumber,aPlayerPtr->firstName,aPlayerPtr->lastName,aPlayerPtr->position,
           aPlayerPtr->battingHand,aPlayerPtr->throwingHand,aPlayerPtr->birthMonth,aPlayerPtr->birthDay,
           aPlayerPtr->birthYear,aPlayerPtr->heightFeet,aPlayerPtr->heightInches,aPlayerPtr->weight,
           aPlayerPtr->status,aPlayerPtr->mlbStatus
           );
}

回去看后,我设法使用泽西号码。使用这段代码

int jerseyNumber;
int i;


printf("Enter jersey number for the player: ");
scanf("%i",&jerseyNumber);

for(i=0;i<=MAX_PLAYERS;i++){
    if(jerseyNumber == players[i].jerseyNumber){
        printf("The player's name is %s %s\n",players[i].firstName, players[i].lastName);
    }
}

虽然现在我仍然在试图弄清楚如何使用姓名或职位

答案 1 :(得分:-1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN_LINE 80
#define LEN_NAME 30
#define MAX_LINES 30
#define LEN_POS 5
#define LEN_HAND 5
#define LEN_THROW 5
#define LEN_STATUS 10
#define LEN_MLBSTATUS 5
#define MAX_PLAYERS 26

typedef struct
{
    int jerseyNumber;
    char firstName[LEN_NAME + 1];
    char lastName[LEN_NAME + 1];
    char position[LEN_POS + 1];
    char battingHand[LEN_HAND + 1];
    char throwingHand[LEN_THROW + 1];
    int birthYear, birthMonth, birthDay, heightFeet, heightInches, weight;
    char status[LEN_STATUS + 1];
    char mlbStatus[LEN_MLBSTATUS + 1];


} player_t;

//protypes for all functions
void displayPlayer(player_t *aPlayerPtr);
void displayWelcome(void);
void resultsProvided(void);
void singleDisplay(player_t *aPlayerPtr);

int main(void)
{
    FILE * filePtr;
    int index, count,i;
    char line[LEN_LINE + 1];
    char userChoice;
    char repeat;
    player_t players[MAX_LINES];
    filePtr = fopen("Shrimp.txt","r");
    if (filePtr == NULL)
    {
        printf("Unable to open file.\n");
    }
    else
    {
        index = 0;
        while(index < MAX_LINES && fgets(line, LEN_LINE, filePtr))
        {
            if(14 == sscanf(line, "%i %30s %30s %5s %5s %5s %i %i %i %i %i %i %10s %5s",
                            &players[index].jerseyNumber, players[index].firstName,
                            players[index].lastName, players[index].position,
                            players[index].battingHand, players[index].throwingHand,
                            &players[index].birthYear, &players[index].birthMonth,
                            &players[index].birthDay, &players[index].heightFeet,
                            &players[index].heightInches, &players[index].weight,
                            players[index].status, players[index].mlbStatus)

              )
            {
                index++;
            }
        }

        fclose(filePtr);
        count = index;

        displayWelcome();
        do
        {
            printf("Choose how you would like to search.\nEnter A to display all players information.\
                \nEnter N to search a player based on name.\nEnter J to search a player based on jersey number.\
                \nEnter P to search a player based on position.\nEnter your choice: ");
            scanf("%c",&userChoice);

            if(userChoice == 'A' || userChoice == 'a'){
            for(index = 0; index < count; index = index + 1)
            {
                displayPlayer(&players[index]);
            }
            }

            if(userChoice == 'J' || userChoice == 'j'){
            int jerseyNumber;

            printf("Enter jersey number for the player: ");
            scanf("%i",&jerseyNumber);

            for(i=0; i<=MAX_PLAYERS; i++)
            {
                if(jerseyNumber == players[i].jerseyNumber)
                {
                    singleDisplay(&players[i]);

                }
            }
            }
            if(userChoice == 'N' || userChoice == 'n'){
            char playerName[LEN_NAME + 1];

            printf("Enter name for the player: ");
            scanf("%s",playerName);

            for(i=0; i<=MAX_PLAYERS; i++)
            {
                if(strcmp(playerName, players[i].firstName)== 0)
                {
                    singleDisplay(&players[i]);

                }
            }
            }
            if(userChoice == 'P' || userChoice == 'p'){
            char playerPosition[LEN_NAME + 1];

            printf("Enter position for the player: ");
            scanf("%s",playerPosition);

            for(i=0; i<=MAX_PLAYERS; i++)
            {
                if(strcmp(playerPosition, players[i].position)== 0)
                {
                    singleDisplay(&players[i]);

                }
            }

            }
            printf ("Would you like to search again?\nEnter Y or y for Yes or any other character to exit this program!\n");
            scanf ("\n%c", &repeat);
        }
        while (repeat == 'Y' || repeat == 'y');
    }
    resultsProvided();

    return 0;
}

void displayPlayer(player_t *aPlayerPtr)
{
    printf("JERSEY: %i PLAYER NAME: %s %s POSITION: %s BATTING HAND: %s THROWING HAND: %s BIRTHDAY: %i/%i/%i HEIGHT: %i'%i WEIGHT: %i STATUS: %s MLB 40-STATUS: %s\n\n",
           aPlayerPtr->jerseyNumber,aPlayerPtr->firstName,aPlayerPtr->lastName,aPlayerPtr->position,
           aPlayerPtr->battingHand,aPlayerPtr->throwingHand,aPlayerPtr->birthMonth,aPlayerPtr->birthDay,
           aPlayerPtr->birthYear,aPlayerPtr->heightFeet,aPlayerPtr->heightInches,aPlayerPtr->weight,
           aPlayerPtr->status,aPlayerPtr->mlbStatus
          );
}

void displayWelcome(void)
{
    printf("Welcome to Adrian's Baseball Program!\n");

}

void resultsProvided(void)
{
    printf("\nResults provided by Adrian.\n");
}

void singleDisplay(player_t *aPlayerPtr)
{
     printf("JERSEY: %i\nPLAYER NAME: %s %s\nPOSITION: %s\nBATTING HAND: %s\nTHROWING HAND: %s\
            \nBIRTHDAY: %i/%i/%i\nHEIGHT: %i'%i\nWEIGHT: %i\nSTATUS: %s\nMLB 40-STATUS: %s\n\n",
           aPlayerPtr->jerseyNumber,aPlayerPtr->firstName,aPlayerPtr->lastName,aPlayerPtr->position,
           aPlayerPtr->battingHand,aPlayerPtr->throwingHand,aPlayerPtr->birthMonth,aPlayerPtr->birthDay,
           aPlayerPtr->birthYear,aPlayerPtr->heightFeet,aPlayerPtr->heightInches,aPlayerPtr->weight,
           aPlayerPtr->status,aPlayerPtr->mlbStatus
          );
}

我能够获得几乎所有工作所期望的while循环,允许用户反复搜索。我也无法让其他语句正常工作。

    if(userChoice == 'J' || userChoice == 'j'){
    int jerseyNumber;

    printf("Enter jersey number for the player: ");
    scanf("%i",&jerseyNumber);

    for(i=0; i<=MAX_PLAYERS; i++)
    {
        if(jerseyNumber == players[i].jerseyNumber)
        {
            singleDisplay(&players[i]);

        }else{
            printf("The jersey number entered is not found.");
        }
    }
    }

它循环,即使我在数组中输入一个数字,它也输出这样的东西(第18号是文件中的第一个球衣号码)

 Welcome to Adrian's Baseball Program!
Choose how you would like to search.
Enter A to display all players information.
Enter N to search a player based on name.
Enter J to search a player based on jersey number.
Enter P to search a player based on position.
Enter your choice: J
Enter jersey number for the player: 18
JERSEY: 18
PLAYER NAME: Matt Tomshaw
POSITION: P
BATTING HAND: R
THROWING HAND: L
BIRTHDAY: 12/17/1988
HEIGHT: 6'2
WEIGHT: 200
STATUS: Active
MLB 40-STATUS: No

未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未输入球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未输入球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未输入球衣号码。球衣号码未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未找到输入的球衣号码。未输入球衣号码。输入的球衣号码为没找到。球衣号码进入没找到红色。你想练习另一个号码吗?     输入Y或y表示是或任何其他字符退出该程序!