gpt4 book ai didi

无法读取数字字符频率

转载 作者:行者123 更新时间:2023-11-30 19:34:09 25 4
gpt4 key购买 nike

#include <stdio.h>

int main() {
FILE *fb;
char data[255];
int c=0;
int count[75] = {0};
fb = fopen("Input.txt", "r");
fgets(data, 255, fb);

/* Start finding frequency*/
while (data[c] != '\0')
{
if( data[c] >= '0' && data[c] <= 'z')
count[data[c] - 'a']++;

c++;
}
for (c = 0; c < 75; c++)
{
/** Printing only those characters
whose count is at least 1 */

if (count[c] != 0)
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}

return 0;
}

示例输入文件:“Fred Fish 12345678”

我能够处理输入文件中的空格,但程序无法读取大写字母和数字字符的频率。我可以在程序中更改哪些内容来帮助解决问题。阅读频率后,我的计划是保存文件,以便我可以使用 Huffman 进行压缩

最佳答案

#include <stdio.h>

int main() {
FILE *fb;
char data[255];
int c = 0;
int count[75] = { 0 };
fb = fopen("Input.txt", "r");
fgets(data, 255, fb);

/* Start finding frequency*/
while (data[c] != '\0')
{
if (data[c] >= 'a' && data[c] <= 'z') // here you check normal letters
count[data[c] - 'a']++;
else if (data[c] >= 'A' && data[c] <= 'Z')
count[data[c] - 'A' + 26]++; // Capital letters will be stored after lower cases
else if (data[c] >= '0' && data[c] <= '9')
count[data[c] - '0' + 51]++; // Numbers will be stored after capital letters
c++;
}

// count[] is initialized as following :
// From count[0] to count[25] == Occurence of low case characters
// From count[26] to count[51] == Occurence of capital characters
// From count[52] to count[61] == Occurence of numbers from 0 to 9

for (c = 0; c < 61; c++)
{
/** Printing only those characters
whose count is at least 1 */
if (count[c] != 0) {
if (c < 26)
printf("%c occurs %d times in the entered string.\n", c + 'a', count[c]);

// Starting from 'a', iterating until 'z'
else if (c < 52)
printf("%c occurs %d times in the entered string.\n", c + 'A' - 26, count[c]);

// Same as for low case characters
// Substracting 26 because we already have iterated through the first 26 low case characters
// Starting from 'A' until 'Z'

else if (c >= 52)
printf("%c occurs %d times in the entered string.\n", c + '0' - 51, count[c]);

// Same as for characters
// Substracting 51 because we already have iterated through low cases and capital characters
// Starting from '0' until '9'
}
}

return 0;
}

问题是,考虑到 ascii table ,您通过减去等于的 'a' 将大写字母和数字存储在数组 count 的负索引中到 ASCII 格式的 97。这应该可行,但我还无法测试它,所以要小心。

对于打印,我们对小写字符执行相同的操作,然后是大写字符,然后是数字:我们从第一个字符开始:'a',然后 'A',然后 '0',使用 c 迭代直到最后一个并将其全部打印。我们使用的事实是,在 C 语言中,'a' + 1 = 'b''A' + 1 = 'B' 等。

对于输入 Fred Fish 12345678,此代码的输出为:

d occurs 1 times in the entered string.
e occurs 1 times in the entered string.
h occurs 1 times in the entered string.
i occurs 1 times in the entered string.
r occurs 1 times in the entered string.
s occurs 1 times in the entered string.
F occurs 2 times in the entered string.
1 occurs 1 times in the entered string.
2 occurs 1 times in the entered string.
3 occurs 1 times in the entered string.
4 occurs 1 times in the entered string.
5 occurs 1 times in the entered string.
6 occurs 1 times in the entered string.
7 occurs 1 times in the entered string.
8 occurs 1 times in the entered string.

关于无法读取数字字符频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com