【发布时间】:2020-12-30 15:40:50
【问题描述】:
您好,我遇到了 CS50 问题集 2 的问题,特别是可读性任务。问题是当我输入网站上的预设值以尝试测试您的程序时,其中一些不起作用我不确定为什么它不起作用任何人都可以帮助我
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int main(void){
string text = get_string("Text: ");
int letter_count = 0;
int word_count = 0;
int sentence_count = 0;
for( int i=0;i < strlen(text);i++){
if (isalpha(text[i])){
letter_count ++;
}
else if(text[i] == ' '){
word_count ++;
}
else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentence_count ++;
}
}
float L = ((float)letter_count / (float)word_count) * 100;
float S = ((float)sentence_count / (float)word_count) * 100;
float index = 0.0588 * L - 0.296 * S - 15.8;
if (index <= 16 && index >= 0)
{
printf("Grade %i\n", (int) round(index));
}
else if (index >= 16)
{
printf("Grade 16+\n");
}
else{
printf("Before Grade 1\n");
}
printf("%i Letter(s)\n", letter_count);
printf("%i Word(s)\n", word_count+1);
printf("%i Sentence(s)\n", sentence_count);
}
【问题讨论】:
-
什么不起作用?预期的输入/输出是什么?
-
输入只是一个文本,我们应该逐个字符地阅读,并通过公式检查它属于哪个可读性等级,但问题是输出应该输出正确的等级它不让我分享的文本和例子 天地之间的事情比你的哲学梦想的要多。 (9 年级)
-
你计算单词的方法不准确,因为你计算的是间隙,而不是单词。 (如果您在示例之前添加一个空格,您将获得正确的成绩。)此外,将所有空格视为间隙也不是很好。有些人喜欢在句子后写两个空格。有些人无意中用多余的空格结束了他们的行。 “␣So␣what?␣”和“So␣␣what␣?”应该产生相同的单词和句子计数。
-
一个更好的方法是计算单词的开头,当你有一个字母字符前面有一个非字母字符时。 (并假设在输入之前有一个非字母字符。您可能需要一些类似的逻辑来组合多字符标点符号,如“WTF?!?我快疯了......”。)
-
"Modify readability.c so that instead of outputting the number of letters, words, and sentences, it instead outputs the grade level as given by the Coleman-Liau index ..."你忘了去掉字母词和句数。
标签: c cs50 readability