首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >字符串C的分组数组

字符串C的分组数组
EN

Stack Overflow用户
提问于 2016-09-08 03:51:47
回答 7查看 1.5K关注 0票数 3

我已经做了一个字符串数组,我正在尝试将一个字符串数组分组到类别中。

到目前为止,我的代码如下所示:

代码语言:javascript
运行
复制
#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;
}

上面印着这个:

代码语言:javascript
运行
复制
Canada
Cycling
Canada
Swimming
India
Swimming
New Mexico
Cycling
New Mexico
Cycling
New Mexico
Swimming

但是,我正试图将体育项目与各个国家的统计数字结合起来,我想这样做:

代码语言:javascript
运行
复制
Canada
    Cycling  1
    Swimming 1

India
    Swimming 1

New Mexico
    Cycling  2
    Swimming 1

我正在考虑用数组中的每个i+2元素对国家进行分类,并使用strcmp删除重复的国家字符串,但我不知道如何与每个国家一起进行体育计数。

我只是不知道该怎么做。任何帮助都将不胜感激。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-09-08 05:26:03

给你一个数组,我可以看到这个国家的名字是可以替代的。如果这是可用的格式数据,您可以按照下面的代码。

代码语言:javascript
运行
复制
#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;
}

基本上,这就是我在这里要做的:

  1. 将第一个索引存储在变量中。
  2. 在每个偶数迭代中,检查国家名称是否等于存储的名称。如果不更新名称。
  3. 在每一次奇数迭代中,您只需打印出名称。
  4. 运动名称存储在变量中,int变量sports_count保存计数。
  5. 如果新国家到达,请先打印体育名称,并以体育和相关变量的名义进行强制性更新。
  6. 最后一个运动名称被打印在循环之外。 输出加拿大自行车1游泳1印度游泳1新墨西哥自行车2游泳1
票数 1
EN

Stack Overflow用户

发布于 2016-09-08 05:04:21

解决方案取决于您想采取什么样的方法。保持单个字符数组(结果*在您的代码中)将不会使您的数据动态。本质上,您需要使用字典数据结构来存储(如果需要的话是嵌套的)对。在C语言中,我会使用结构来使其模块化。

首先,你需要一个结构来存储体育及其计数(比如说奖牌数量)。

代码语言:javascript
运行
复制
struct sport {
  char *sport_name;
  int medal_count;
  //Any other details you want to store
};

然后,一个国家可以进行多种运动。因此,我们需要建立国家结构。

代码语言:javascript
运行
复制
struct Country{
  char *country_name;
  struct sport* results;
  //Any other details you want to store
};

现在让我们创建一个国家数据数组。

代码语言:javascript
运行
复制
#define NO_OF_COUNTRIES 3  //You may fix this or make it dynamic
struct Country country_data[NO_OF_COUNTRIES]; 

您现在可以相应地填写数据。希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2016-09-08 04:45:15

考虑使用城市和国家列表而不是字符串数组。

下面的代码解释了两种结构和两种方法的最简单实现--添加新元素和搜索元素。

尝试这段代码,然后学习它:

代码语言:javascript
运行
复制
#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,只需从末尾读取初始数组(这是最简单的方法):

代码语言:javascript
运行
复制
// filling list of countries with sublists of cityes
int i;
for (i = nelements-2; i >=0 ; i -= 2)
   {
   . . .
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39382328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档