我正在写一个程序来找出字母,单词和句子的数量。使用这些值,我将使用Coleman-Liau指数来查找文章的可读性级别。
我认为我有一个非常可靠的帮助在线程序,但我不知道为什么一些错误发生在下面的链接。https://submit.cs50.io/check50/35e7ae7e4b968d6b06a8152f61a0e83381b376ab
有关上下文,请参见https://cs50.harvard.edu/college/psets/2/readability/
下面是我遇到麻烦的代码:
#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");
}
}
据我所见,我看不出为什么有些结果是扭曲的。
发布于 2020-07-24 00:31:16
除了圆形()问题之外,这行代码还存在另外两个问题:if (i
首先,你没有考虑下一个单词以“或”开头的情况,后两者不是字母数字。
第二,假设有人不小心在两个单词之间输入了更多空格,这一行代码将无法正确地计算单词数。CS50分配确实需要考虑这种情况。
https://stackoverflow.com/questions/58527862
复制相似问题