简体   繁体   English

如何使用 struct 计算每个 char 数组的计数时间?

[英]How do I calculate count times for every char array with struct?

struct Page{
 char data[50];
 int count = 0; // ?
};
Page page[50];

I don't know how to save count for every char array.我不知道如何保存每个 char 数组的计数。

for exmaple:例如:

input: a c b
page[i].data[0] = a; // a_count = 1
page[i].data[1] = c; // c_count = 1
page[i].data[2] = b; // b_count = 1

my ideas is我的想法是

paga[i].data[0].count++;

but, I don't know how to implement with struct.但是,我不知道如何用 struct 来实现。

Instead of a single integer count , you need an array to solve this issue.您需要一个数组来解决此问题,而不是单个 integer count So you can define Page in the following way:因此,您可以通过以下方式定义Page

typedef struct Page
{
    char data[50];
    int countChar[TOTAL_CHARS];
} Page;

Now, we can't simply initialize an array inside a struct like we can in a function.现在,我们不能像在 function 中那样简单地在结构中初始化数组。 So we have to manually initialize countChar[] with 0 .所以我们必须手动初始化countChar[]0 However, there is a trick that can save you from this tiresome process.但是,有一个技巧可以让您摆脱这个令人厌烦的过程。 And the trick is to use a macro like this:诀窍是使用这样的宏:

#define NEW_PAGE { "", {0} }

and use it in the following way:并以下列方式使用它:

Page page[50] = NEW_PAGE;

Now, all you have to do is map the character to the index of countChar[] and increment its value by 1. This can be done in the following way:现在,您所要做的就是将字符 map 到countChar[]的索引并将其值增加 1。这可以通过以下方式完成:

page[0].countChar[ch - 'a']++;

Here, ch is the character from input.这里, ch是输入中的字符。 Considering all the input will be lowercase letters, subtracting ch with 'a' will produce the required index that represent frequency of character ch .考虑到所有输入都是小写字母,用'a'减去ch将产生表示字符ch频率的所需索引。 If the possible value of ch were all ASCII characters, we would simply replace 'a' with '\0' and change size of countChar[] accordingly.如果ch的可能值都是 ASCII 字符,我们只需将'a'替换为'\0'并相应地更改countChar[]的大小。

Here's a code that tests this idea:这是一个测试这个想法的代码:

#include<stdio.h>
#define NEW_PAGE { "", {0} }
#define TOTAL_CHARS 26

typedef struct Page
{
    char data[50];
    int countChar[TOTAL_CHARS];
} Page;

int main()
{
    Page page[50] = NEW_PAGE;
    char input[] = "hello world";
    char ch;
    int i = 0;
    int frequency;

    while(input[i] != '\0')
    {
        ch = input[i];
        page[0].data[i] = ch;
        page[0].countChar[ch - 'a']++;
        i++;
    }

    for(i=0; i<TOTAL_CHARS; i++)
    {
        frequency = page[0].countChar[i];
        if(frequency != 0)
            printf("%c is present %d times\n", ('a'+i), frequency);
    }
    return 0;
}

Instead of having a single int , you could have an array of integers of length 26. This would hold the count of each lowercase letter in the array.您可以拥有一个长度为 26 的整数数组,而不是单个int 。这将保存数组中每个小写字母的计数。

The integer values for lowercase letters range from 97 - 122 .小写字母的 integer 值范围为97 - 122 To correspond each letter to an index in a 26 length array, you need to subtract 97 from the integer value of each char so that each char can correspond to an index from 0 - 25.要将每个字母对应到 26 长度数组中的索引,您需要从每个char的 integer 值中减去 97,以便每个 char 可以对应 0 - 25 的索引。

So your struct should look something like this:所以你的结构应该是这样的:

struct Page{
 char data[50];
 int count[26];
};

If you had a char called letter , the way you would find the correct index of the count array and increment it would be:如果您有一个名为letterchar ,那么您将找到count数组的正确索引并递增它的方式是:

int index = (int)letter - 97;
count[index]++;

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

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