我已经做了一个字符串数组,我正在尝试将一个字符串数组分组到类别中。
到目前为止,我的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[]) {
char *results[] = {"Canada", "Cycling", "Canada", "Swimming", "India", "Swimming", "New Mexico",
"Cycling", "New Mexico", "Cycling", "New Mecico", "Swimming"};
int nelements, i, country_count;
nelements = sizeof(results) / sizeof(results[0]);
for (i = 0 ; i < nelements; i++) {
printf("%s\n", results[i]);
}
return 0;
}上面印着这个:
Canada
Cycling
Canada
Swimming
India
Swimming
New Mexico
Cycling
New Mexico
Cycling
New Mexico
Swimming但是,我正试图将体育项目与各个国家的统计数字结合起来,我想这样做:
Canada
Cycling 1
Swimming 1
India
Swimming 1
New Mexico
Cycling 2
Swimming 1我正在考虑用数组中的每个i+2元素对国家进行分类,并使用strcmp删除重复的国家字符串,但我不知道如何与每个国家一起进行体育计数。
我只是不知道该怎么做。任何帮助都将不胜感激。
发布于 2016-09-08 05:26:03
给你一个数组,我可以看到这个国家的名字是可以替代的。如果这是可用的格式数据,您可以按照下面的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *results[] = {"Canada", "Cycling", "Canada", "Swimming", "India","Swimming", "New Mexico",
"Cycling", "New Mexico", "Cycling", "New Mexico", "Swimming"};
int nelements, i, sport_count=0,country_change =0;
char country[50];char sport[50];
strcpy(country,results[0]);
printf("%s\n", country);
strcpy(sport,results[1]);
nelements = sizeof(results) / sizeof(results[0]);
for (i = 1 ; i < nelements; i++)
{
if(((i%2)==0) && (strcmp(country,results[i])))
{
//sport_count++;
printf("\t%s %d\n", sport,sport_count);
country_change =1;
strcpy(country,results[i]);
printf("%s\n", country);
}
else if((i%2)==1)
{
if(country_change)
{
strcpy(sport,results[i]);
country_change = 0;
sport_count = 0;
}
if(!strcmp(sport,results[i]))
{
sport_count++;
}
else
{
printf("\t%s %d\n", sport,sport_count);
strcpy(sport,results[i]);
sport_count = 1;
}
//strcpy(country,results[i]);
}
}
printf("\t%s %d\n", sport,sport_count);
return 0;
}基本上,这就是我在这里要做的:
发布于 2016-09-08 05:04:21
解决方案取决于您想采取什么样的方法。保持单个字符数组(结果*在您的代码中)将不会使您的数据动态。本质上,您需要使用字典数据结构来存储(如果需要的话是嵌套的)对。在C语言中,我会使用结构来使其模块化。
首先,你需要一个结构来存储体育及其计数(比如说奖牌数量)。
struct sport {
char *sport_name;
int medal_count;
//Any other details you want to store
};然后,一个国家可以进行多种运动。因此,我们需要建立国家结构。
struct Country{
char *country_name;
struct sport* results;
//Any other details you want to store
};现在让我们创建一个国家数据数组。
#define NO_OF_COUNTRIES 3 //You may fix this or make it dynamic
struct Country country_data[NO_OF_COUNTRIES]; 您现在可以相应地填写数据。希望这能有所帮助。
发布于 2016-09-08 04:45:15
考虑使用城市和国家列表而不是字符串数组。
下面的代码解释了两种结构和两种方法的最简单实现--添加新元素和搜索元素。
尝试这段代码,然后学习它:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct city
{
struct city * next;
char * cityName;
int counter;
};
struct country
{
struct country * next;
char * coutryName;
struct city * cities;
};
struct country * findCountry(struct country * coutries, char * country)
{
struct country * searchResult = NULL;
while (coutries != NULL)
{
if (strcmp(country, coutries->coutryName) == 0)
{
searchResult = coutries;
break;
}
coutries = coutries->next;
}
return searchResult;
}
struct country * addCountry(struct country * coutries, char * country)
{
struct country * newCountry = malloc(sizeof(struct country));
newCountry->next = coutries;
newCountry->coutryName = country;
newCountry->cities = NULL;
return newCountry;
}
struct city * findCity(struct city * cities, char * city)
{
struct city * searchResult = NULL;
while (cities != NULL)
{
if (strcmp(city, cities->cityName) == 0)
{
searchResult = cities;
break;
}
cities = cities->next;
}
return searchResult;
}
struct city * addCity(struct city * cities, char * city)
{
struct city * newCity = malloc(sizeof(struct city));
newCity->cityName = city;
newCity->next = cities;
newCity->counter = 0;
return newCity;
}
int main(void)
{
char *results[] = { "Canada", "Cycling", "Canada", "Swimming", "India", "Swimming", "New Mexico",
"Cycling", "New Mexico", "Cycling", "New Mexico", "Swimming" };
struct country * countries = NULL;
int nelements = sizeof(results) / sizeof(results[0]);
// filling list of countries with sublists of cityes
int i;
for (i = 0; i < nelements; i+=2)
{
struct country * pCountry = findCountry(countries, results[i]);
if (!pCountry)
{
countries = addCountry(countries, results[i]);
pCountry = countries;
}
struct city * pCity = findCity(pCountry->cities, results[i+1]);
if (!pCity)
{
pCountry->cities = addCity(pCountry->cities, results[i + 1]);
pCity = pCountry->cities;
}
pCity->counter++;
}
// reading cities from all countries
struct country * pCountry = countries;
while (pCountry != NULL)
{
printf("%s\n",pCountry->coutryName);
struct city * pCity = pCountry->cities;
while (pCity != NULL)
{
printf(" %s %d\n", pCity->cityName, pCity->counter);
pCity = pCity->next;
}
printf("\n");
pCountry = pCountry->next;
}
return 0;
}注意:在您的代码中,上一个"New Mexico"类似于"New Mecico",在我的代码中,这个错误类型是固定的。
更新
注2:因为我在列表的开头添加了元素,所以国家和城市的顺序与源数组中第一次提到的顺序相反。
如果订单很重要,您有两个选择:
1)重写代码,将新项添加到列表的末尾(这是很长的路)
2)在for-loop中重写main,只需从末尾读取初始数组(这是最简单的方法):
// filling list of countries with sublists of cityes
int i;
for (i = nelements-2; i >=0 ; i -= 2)
{
. . .https://stackoverflow.com/questions/39382328
复制相似问题