0

我有这个项目。我有分数数组,match1,match2,我有球员的名字和他们国家的缩写。然后我有这些缩写和国家的长名称。

我可以把球员的名字和国家的缩写放在一起,我也可以把国家的缩写和国家的长名放在一起。

但是我不知道如何将国家长名和球员姓名放在一个数组中。

所以,我创建了数组 (char name_and_country[PLAYERS][LENGTH_NAME]) 并且我希望在这个数组中是名称和国家;所以如果我打印说:

char name_and_country[PLAYERS][LENGTH_NAME]={ “大卫贝克汉姆英格兰”,“韦恩鲁尼英格兰”....等等。

任何人都可以帮助我吗?先感谢您!

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20


int main (void)

{   int match1[PLAYERS] = { 0,1,3,2,4};
    int match2[PLAYERS] = { 0,4,0,0,1};
    int goals[PLAYERS] ;

    char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
    char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
    char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
    char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
    char name_and_country_code[PLAYERS][LENGTH_NAME];
    char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
    char name_and_country[PLAYERS][LENGTH_NAME];
    int i, first =1, second= 2;

 for(i=0; i < PLAYERS; i++)
    {
        strcpy (name_and_country_code[i], name[i]);
        strcat (name_and_country_code[i], "  " );
        strcat (name_and_country_code[i], country_abbreviations[i]);
        goals[i]= match1[i] + match2[i];
        printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
    }
4

1 回答 1

1

使用本准则。我已将我的代码添加到您的代码中,结果如下。我运行它,它工作正常..

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include<string.h>
#define PLAYERS 5

#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20


int main (void)

{   int match1[PLAYERS] = { 0,1,3,2,4};
        int match2[PLAYERS] = { 0,4,0,0,1};
        int goals[PLAYERS] ;

        char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
        char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
        char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
        char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
        char name_and_country_code[PLAYERS][LENGTH_NAME];
        char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
        char name_and_country[PLAYERS][LENGTH_NAME];
        int i, first =1, second= 2;

        for(i=0; i < PLAYERS; i++)
        {
                strcpy (name_and_country_code[i], name[i]);
                strcat (name_and_country_code[i], "  " );
                strcat (name_and_country_code[i], country_abbreviations[i]);
                goals[i]= match1[i] + match2[i];
                printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
        }
        for(i=0; i < PLAYERS; i++)
        {
                strcpy (name_and_country[i], name[i]);
                strcat (name_and_country[i], "  " );
                char country[LENGTH_COUNTRY];
                strcpy(country,"DEFAULT COUNTRY"); // Used when player has a invalid country code
                int j;
                for(j=0;j<NUM_COUNTRIES;j++)
                {
                        if(strcmp(country_abbreviations[i],country_code[j])==0)
                                strcpy(country,country_name[j]);
                }
                strcat(name_and_country[i],country);
                printf("%s\n",name_and_country[i]);
        }
}
于 2013-06-26T05:27:57.863 回答