gpt4 book ai didi

c - 计算文件中音节数的程序

转载 作者:行者123 更新时间:2023-11-30 20:03:04 27 4
gpt4 key购买 nike

我有一个函数,应该在给定这些约束的情况下计算文件中的音节数:

1) 每一组相邻的元音(a、e、i、o、u、y)算作一个音节(例如,“real”中的“ea”算作一个音节,但“e..”) “regal”中的“a”算作两个音节

2) 单词末尾的“e”不算音节

3)每个单词至少有一个音节,即使前面的规则给出的计数为零。

鉴于此,我制作了(尽管非常糟糕)函数来计算文件中的音节数

我尝试以多种不同的方式创建这个函数,但这种方式对我来说最有意义,并且也给了我对真实答案的合理(不是真的,而是在它所做的宏伟计划中)估计。

int syllableCount(char **str)
{
int i = 0;
int q = 0;
int syllableCounter = 0;

for (i = 0; i < lineCount; i++)
{
for (q = 0; q <= strlen(str[i]); q++)
{
if (str[i][q] == 'A' || str[i][q] == 'a' ||
str[i][q] == 'E' || str[i][q] == 'e' ||
str[i][q] == 'I' || str[i][q] == 'i' ||
str[i][q] == 'O' || str[i][q] == 'o' ||
str[i][q] == 'U' || str[i][q] == 'u' ||
str[i][q] == 'Y' || str[i][q] == 'y')
{
syllableCounter++;
}
if ((str[i][q] == 'E' && str[i][q + 1] == ' ') ||
(str[i][q] == 'e' && str[i][q + 1] == ' ') ||
(str[i][q] == 'E' && str[i][q + 1] == '\n') ||
(str[i][q] == 'e' && str[i][q + 1] == '\n') ||
(str[i][q] == 'E' && str[i][q + 1] == '.') ||
(str[i][q] == 'e' && str[i][q + 1] == '.') ||
(str[i][q] == 'E' && str[i][q + 1] == ';') ||
(str[i][q] == 'e' && str[i][q + 1] == ';') ||
(str[i][q] == 'E' && str[i][q + 1] == ':') ||
(str[i][q] == 'e' && str[i][q + 1] == ':') ||
(str[i][q] == 'E' && str[i][q + 1] == '!') ||
(str[i][q] == 'e' && str[i][q + 1] == '!') ||
(str[i][q] == 'E' && str[i][q + 1] == '?') ||
(str[i][q] == 'e' && str[i][q + 1] == '?'))
{
syllableCounter--;
}
if ((str[i][q] == 'A' || str[i][q] == 'a' ||
str[i][q] == 'E' || str[i][q] == 'e' ||
str[i][q] == 'I' || str[i][q] == 'i' ||
str[i][q] == 'O' || str[i][q] == 'o' ||
str[i][q] == 'U' || str[i][q] == 'u' ||
str[i][q] == 'Y' || str[i][q] == 'y') &&
(str[i][q + 1] == 'A' || str[i][q + 1] == 'a' ||
str[i][q + 1] == 'E' || str[i][q + 1] == 'e' ||
str[i][q + 1] == 'I' || str[i][q + 1] == 'i' ||
str[i][q + 1] == 'O' || str[i][q + 1] == 'o' ||
str[i][q + 1] == 'U' || str[i][q + 1] == 'u' ||
str[i][q + 1] == 'Y' || str[i][q + 1] == 'y'))
{
syllableCounter--;
}
if ((str[i][q] != 'A' || str[i][q] != 'a' ||
str[i][q] != 'E' || str[i][q] != 'e' ||
str[i][q] != 'I' || str[i][q] != 'i' ||
str[i][q] != 'O' || str[i][q] != 'o' ||
str[i][q] != 'U' || str[i][q] != 'u' ||
str[i][q] != 'Y' || str[i][q] != 'y') &&
(str[i][q + 1] == ' ' || str[i][q + 1] == '\n'))
{
syllableCounter++;
}
}
}
return syllableCounter;
}

在我的测试文件中,我得到了 54 个音节,而真正的答案是 32。是什么导致我落后 22!?

P.S:这是我使用的文件中的文本:

“这件红色成衣就是为你量身定做的!它明天就准备好了。什么是衣服的颜色?哦,它是红色的!”

所有拼写和空格错误都是故意造成的

最佳答案

你的代码太复杂了。

原始问题陈述中的规则非常简单。对于每个单词,您需要计算遇到的不同元音组的数量。评论中使用状态机的建议是绝对正确的。但是,您不需要复杂的机器。您只需要跟踪一些基本状态。

至少,我建议这些状态:

int in_word = 0;         // non-zero if currently processing a word
int in_vowels = 0; // non-zero if currently processing group of vowels
int is_silent_e = 0; // non-zero if the last vowel processed was an 'e'
int vowel_groups = 0; // counts the number of vowel groups encountered in current word

现在,有了上面的合理状态,下面是如何使用它们的概述:

for (char *p = str[i], *end = p + strlen(str[i]) + 1; p != end; ++p)
{
char c = tolower(*p);
if (isalpha(c))
{
// starting a new word?
if (!in_word) {
in_word = 1;
in_vowels = 0;
is_silent_e = 0;
vowel_groups = 0;
}

// do we have a vowel?
if (strchr("aeiouy", c)) {
if (!in_vowels) {
/**** WRITE ME ****/
++vowel_groups;
} else {
/**** WRITE ME ****/
}
} else if (in_vowels) {
// no longer in vowel group
/**** WRITE ME ****/
}
}
else if (in_word)
{
// No longer in a word -- update syllable count and reset
vowel_groups -= is_silent_e;

/**** WRITE ME ****/
}
}

我留下了一些逻辑供您填写。当您正确执行此操作时,您将获得示例输入的答案 32

请注意特殊的循环条件,该条件确保循环也处理字符串的空终止符。这将确保即使字符串中的最后一个字符是单词字符,单词结尾测试也会运行。

关于c - 计算文件中音节数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660474/

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