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

1)每组相邻的元音(a,e,i,o,u,y)都计为一个音节(例如,“ real”中的“ ea”计为一个音节,而在“ e..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分!

附言:这是我使用的文件中的文本:

“红色现成的裙子是为您量身定制的!它
明天要准备好。什么是
衣服的颜色?哦,是红色的!”

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

最佳答案

您的代码太复杂了。

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

至少,我建议这些状态:

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

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

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

10-12 20:55