简体   繁体   English

如何比较和注册复制的数组值

[英]How to compare and register array values which are replicated

I am having trouble interpreting the logic of this exercise... 我在解释此练习的逻辑时遇到麻烦。

The exercise asks me to register 5 "brands" in a structure and the output must show how many times each brand repeated, if it has been registered more than once of course. 练习要求我在一个结构中注册5个“品牌”,并且输出结果必须显示每个品牌重复了多少次(如果已注册了多个)。

#include <stdio.h>
#include <stdlib.h>

#define C 5

typedef struct      
{
    int id;
    char brands[30];
} Something;


Something a[C];
int main() 
{
    int i=0, j=0;

    //input
    for(i=0;i<C;i++)
    {
        a[i].id = i;

        printf("BRAND: ");
        fflush(stdin);
        gets(a[i].brands);
    }

    for(i=0;i<C;i++)
    {
        for(j=0;j<C;j++)
        {
            if (strcmp(a[i].brands, a[j].brands)==0)
                // 
        }
    }
    return 0;
}

The brand inputs values are not constant, it could be anything. 品牌输入值不是恒定的,可以是任何值。

So I was thinking to look through a search, comparing whether there is equal brand and incrementing a counter for each. 因此,我想通过搜索进行比较,比较是否有相等的品牌,并为每个品牌增加一个计数器。 (This counter is where I stuck, since I dont know how many different brands will be in the registry)... (因为我不知道注册表中会有多少个不同的品牌,所以这个计数器是我停留的地方)...

Eg 1 例如1
Inputs 输入项

Ford
Ferrari
Ford
Nissan
Nissan

Output should be like this: 输出应如下所示:

Ford 2x
Ferrari 1x
Nissan 2x


Eg 2 例如2
Inputs 输入项

Ford
Ford
Ford
Ford
Nissan

Output: 输出:

Ford 4x
Nissan 1x

There are many ways to achieve what you want. 有很多方法可以实现您想要的。 Below are some pointers that will hopefully help you reach a solution. 以下是一些指针,希望可以帮助您找到解决方案。

The first step would be to include a counter in your structure. 第一步是在您的结构中包含一个计数器。

typedef struct      
{
    int id;
    char brands[30];
    unsigned int count;
} Something;

Initialise all the count fields to 0. The brands field only contains a valid string if the count is greater than 0. Since a is a global variable, all the fields are automatically initialised to 0 so there is no extra code needed. 将所有count字段初始化为0。如果count大于0,则brands字段仅包含有效字符串。由于a是全局变量,因此所有字段都会自动初始化为0,因此不需要额外的代码。

Then each time you read an input the code would search through a from the beginning. 然后,每次您读取输入内容时,代码都会从头开始搜索a The search logic would be 搜索逻辑将是

for each 'a' entry
    if (count field > 0)
        if (brand field is same as input)
            // Found a match
            increment count field
            break out of loop
        // else will check next entry by continuing the loop
    else
        // Reached end of valid entries. Hence no match.
        Create a new entry. Copy input into the brand field. 
        Set count field to 1.
        break out of loop

I have deliberately shown pseudo code to leave the C code as an exercise for you. 我特意展示了伪代码,以保留C代码作为练习。 But basically the idea is (as stated in my earlier comment) to search the existing valid entries after reading each input (you don't need two seperate arrays). 但基本上,这个想法是(如我之前的评论中所述)在读取每个输入后搜索现有的有效条目(您不需要两个单独的数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM