2017-07-19 44 views
-2
#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 
      ); 
} 

好吧,所以我设计了一个程序来从文件中读取信息到一个结构数组中。我想能够让用户搜索玩家信息,或者使用姓名或球衣号码。如果有人能指出我正确的方向,我将不胜感激。如何通过struct C编程数组搜索

+2

请显示您的尝试。 –

+10

咦?循环和比较?问题在哪里? –

+1

你的代码中'index = 0,count'是什么意思? – Sma

回答

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 
      ); 
} 

走去,看着它后,我设法使它工作使用的球衣number.Using这段代码

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); 
    } 
} 

虽然我现在被困在试图找出如何做到这一点使用名称或位置

+0

提示,询问用户他们想要查找什么。然后根据他们的选择调用lookupbyxxxx。你已经有了lookupbynumber(最后的for循环) – pm100

-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循环,允许用户搜索repeatedly.Also我有麻烦else语句才能正常工作。

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 

输入的球衣号码是找不到的。没有找到球衣号码输入。没有找到球衣号码输入。没有找到球衣号码输入。没有找到球衣号码输入。没有找到球衣号码。没有找到球衣号码。球衣号码输入的号码没有找到。输入的球衣号码没有找到。输入的球衣号码没有找到。输入的球衣号码没有找到。球衣号码ent找不到任何球衣号码。没有找到球衣号码输入。没有找到球衣号码输入。没有找到球衣号码。没有找到球衣号码。输入的球衣号码是不是。没有找到。没有找到球衣号码。没有找到球衣号码。找不到球衣号码。没有找到球衣号码。没有找到球衣号码。没有找到球衣号码输入。没有找到球衣号码输入。输入的球衣号码没有找到。输入的球衣号码没有找到。你想练习另一个号码吗? 输入Y或y代表Yes或任何其他角色退出此程序!