首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用C语言求取Coleman-Liau指数的可读性水平

用C语言求取Coleman-Liau指数的可读性水平
EN

Stack Overflow用户
提问于 2019-10-23 17:09:20
回答 5查看 12.8K关注 0票数 0

我正在写一个程序来找出字母,单词和句子的数量。使用这些值,我将使用Coleman-Liau指数来查找文章的可读性级别。

我认为我有一个非常可靠的帮助在线程序,但我不知道为什么一些错误发生在下面的链接。https://submit.cs50.io/check50/35e7ae7e4b968d6b06a8152f61a0e83381b376ab

有关上下文,请参见https://cs50.harvard.edu/college/psets/2/readability/

下面是我遇到麻烦的代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>

int letter;
int word;
int sentence;


int main(void)
{

// prompt the user with the question

    string article = get_string("What's the article?: ");

// set the length of article

    int n = strlen(article);

// add +1 if the article starts with alphanumeric letter

    if (isalnum(article[0]))
    {
        word = 1;
    }

// count words

    for (int i = 0; i < n;  i++)
    {
        // count letters

        if (isalnum(article[i]))
        {
            letter++;
        }

        // count words

        if (i < n - 1 && isspace(article[i]) && isalnum(article[i + 1]))
        {
            word++;
        }

        // count sentences

        if (i > 0 && (article[i] == '!' || article[i] == '?' || article[i] == '.') && isalnum(article[i - 1]))
        {
            sentence++;
        }

    }

// calculate Coleman-Liau index

    int grade = 0.0588 * (100 * letter / word) - 0.296 * (100 * sentence / word) - 15.8;

// debugger

    printf("Letters: %i\n Words: %i\n Sentences: %i\n", letter, word, sentence);

// print result
    if (grade <= 1)
    {
        printf("Before Grade 1\n");
    }
    else if (grade < 16)
    {
        printf("Grade %i\n", grade);
    }
    else
    {
        printf("Grade 16+\n");
    }
}

据我所见,我看不出为什么有些结果是扭曲的。

EN

Stack Overflow用户

发布于 2020-07-24 00:31:16

除了圆形()问题之外,这行代码还存在另外两个问题:if (i

首先,你没有考虑下一个单词以“或”开头的情况,后两者不是字母数字。

第二,假设有人不小心在两个单词之间输入了更多空格,这一行代码将无法正确地计算单词数。CS50分配确实需要考虑这种情况。

票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58527862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档