我有这个项目。我有得分,match1,match2的数组,我有球员的名字和他们的国家缩写。那么我有这些缩写和国家的长名称。
我可以将玩家名称和国家缩写放在一起,我也可以将国家缩写和国家长名称放在一起。
但是我不知道如何把国家长名和球员名字放在一个数组中。所以,我创建了数组(char name_and_country [PLAYERS] [LENGTH_NAME]),我希望这个数组是名称和国家;所以,如果我打印说:
char name_and_country [PLAYERS] [LENGTH_NAME] = {“David Beckham England”,“Wayne Rooney England”等等。
任何人都可以帮助我吗? 提前谢谢!
#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]);
}
答案 0 :(得分: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]);
}
}