CS50-pset2:可读性.不正确的结果



当我运行我的代码时,结果没有显示基于问题集中给出的文本的确切结果。虽然显示了成绩,但结果是错误的。文本是:"你想要他们在这里还是那里?我不喜欢他们在这里或那里。我在任何地方都不喜欢他们。"(二年级)输入图片描述

假设,文本的结果是"等级2"。然而,它却显示了所有的等级。

enter code here
int main(void)
{
string s = get_string("Text: ");
printf("%sn",s);
int count_letters = 0; //To count letters (uppercase & lowercase)
int count_words = 1; //To count words
int count_sentences = 0; //To count sentences
for (int i = 0; i < strlen(s); i++)

if (isalpha(s[i]))
{
if ((s[i] >= 'a' && s[i] <= 'z' )||( s[i] >= 'A' && s[i] <= 'Z'))
{
count_letters++;
}
if (s[i] == ' ')
{
count_words++;
}
if (s[i] == '.' || s[i] =='!' || s[i] == '?')
{
count_sentences++;
}
//printf("%i count_letter(s)n", count_letters);
//printf("%i count_words(s)n", count_words);
//printf("%i sentence(s)n", count_sentences);
//Coleman-Liau index
float L = (count_letters / (float) count_words) * 100;
float S = (count_sentences / (float) count_words) * 100;
int grade = round (0.0588 * L - 0.296 * S -15.8);

if (grade < 1)
{
printf("Before Grade 1n");
}
else if (grade >= 16)
{
printf("Grade 16+n");
}
else
{
printf("Grade %.dn", grade);
}
}

}

我的代码有问题吗?我如何修复我的代码以获得确切的结果?这个习题我做了差不多两天了。提前感谢

计算循环内的字母、句子和单词的数目,并计算循环外的Coleman-Liau索引。

不要在循环中计算某些东西并试图从循环中获得特定的输出,它永远不会有好结果。总而言之,在循环中计算你的值,然后在循环之外做其他的事情。

int count_letters = 0; //To count letters (uppercase & lowercase)
int count_words = 1; //To count words
int count_sentences = 0; //To count sentences
for (int i = 0; i < strlen(s); i++){
// get the amounts in the loop
if (isalpha(s[i]))
{
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
{
count_letters++;
}
if (s[i] == ' ')
{
count_words++;
}
if (s[i] == '.' || s[i] == '!' || s[i] == '?')
{
count_sentences++;
}
}
}
//Calculate Coleman-Liau outside of it and get the correct grade from your if statements

由于您有三个不同的类别要计数,因此我将为每个类别创建一个函数。

例如,根据您的代码,您可以创建一个函数来计数字符(这里不需要isdigit函数,非数字字符已经被算法本身过滤掉了):
int get_letters_count(char *text_str)
{
int count_letters = 0;
int text_len = strlen(text_str);
for (int i = 0; i < text_len; i++) {
if (   (text_str[i] >= 'a' && text_str[i] <= 'z')
|| (text_str[i] >= 'A' && text_str[i] <= 'Z')
|| (text_str[i] >= '0' && text_str[i] <= '9')) {
count_letters++;
}
}
return count_letters;
}

这种分解程序的方法将使它更容易开发。

这是一个基于Coleman-Liau索引的非常粗糙的实现:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int get_letters_count(char *text_str)
{
int count_letters = 0;
int text_len = strlen(text_str);
for (int i = 0; i < text_len; i++) {
if (   (text_str[i] >= 'a' && text_str[i] <= 'z')
|| (text_str[i] >= 'A' && text_str[i] <= 'Z')
|| (text_str[i] >= '0' && text_str[i] <= '9')) {
count_letters++;
}
}
return count_letters;
}
int get_words_count(char *text_str)
{
int count_words = 0;
int text_len = strlen(text_str);
for (int i = 0; i < text_len; i++) {
if (text_str[i] == ' ') {
count_words++;
}
}
if (count_words)
count_words++;
return count_words;
}
bool word_is_acronym(char *word)
{
bool ret = true;
for (; *word && *word != ' '; word++) {
if (   *word != '.'
&& *word < 'A' || *word > 'Z') {
ret = false;
}
}
return ret;
}
int get_sentences_count(char *text_str)
{
int count_sentences = 0;
int text_len = strlen(text_str);
char *last_word = &text_str[0];
for (int i = 0; i < text_len; i++) {
if (   text_str[i] == ' '
&& i < (text_len - 1)) {
last_word = &text_str[i + 1];
}
bool end_mark =    text_str[i] == '.'
|| text_str[i] == '!'
|| text_str[i] == '?';
if (   end_mark           
&& word_is_acronym(last_word) == false) {
count_sentences++;
}
}
return count_sentences;
}
int main(void)
{
char text_str[] = "Existing computer programs that measure readability are based "
"largely upon subroutines which estimate number of syllables, "
"usually by counting vowels. The shortcoming in estimating syllables "
"is that it necessitates keypunching the prose into the computer. "
"There is no need to estimate syllables since word length in letters "
"is a better predictor of readability than word length in syllables. "
"Therefore, a new readability formula was computed that has for its "
"predictors letters per 100 words and sentences per 100 words. "
"Both predictors can be counted by an optical scanning device, and "
"thus the formula makes it economically feasible for an organization "
"such as the U.S. Office of Education to calibrate the readability of "
"all textbooks for the public school system.";
int count_letters   = get_letters_count(text_str);
int count_words     = get_words_count(text_str);
int count_sentences = get_sentences_count(text_str);;
if (   count_letters   > 0
&& count_words     > 0
&& count_sentences > 0) {
float L = ((count_letters * 100) / count_words);
float S = ((count_sentences * 100) / count_words);
float grade = 0.0588 * L - 0.296 * S - 15.8;
printf("grade = %.01fn", grade);
} else {
printf("bad inputn");
}
}

输出:

$ gcc main.c && ./a.out
grade = 14.5
但是,解析文本可能非常棘手。一旦你得到了一个使用已知输入的第一个版本,试着扩展你的数据集,并不断改进你的程序。

这个程序的计算效率也远远不够。如果这成为瓶颈,您可以优化函数,或者减少将函数分组在单个循环中的循环数量。

当然,大多数时候最好从一个粗略的工作解决方案开始,然后从那里改进,而不是从一开始就尝试一个更复杂/完整的解决方案。

最新更新