缺少 CS50x 可读性作业的一个案例输出,预期为 5 级输出,但我得到了 4 级

问题描述 投票:0回答:1

作业链接:https://cs50.harvard.edu/x/2024/psets/2/readability/ 这是我的 CS50x 可读性作业代码

输入“哈利·波特在很多方面都是一个非常不寻常的男孩。一方面,他比一年中的任何其他时间都更讨厌暑假。另一方面,他真的很想做作业,但被迫做秘密地,在夜深人静的时候。” 需要“5级”的输出...

但我一直得到“4级”,因为它向下舍入,但我无法在不破坏所需的另一个案例输出的情况下操纵它......

如有帮助,将不胜感激

#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>

// A number of “readability tests” have been developed over the years that define formulas for
// computing the reading level of a text. One such readability test is the Coleman-Liau index. The
// Coleman-Liau index of a text is designed to output that (U.S.) grade level that is needed to
// understand some text. The formula is

// index = 0.0588 * L - 0.296 * S - 15.8
// where L is the average number of letters per 100 words in the text, and S is the average number
// of sentences per 100 words in the text.

int main(void)
{
    // get text input
    string text = get_string("Text: ");

    char currentcharacter = 'A';

    int number_of_periods = 0;
    int number_of_spaces = 0;
    int number_of_letters = 0;
    int number_of_words = 0;
    int number_of_sentences = 0;
    int number_of_characters = 0;

    double gradelevel = 0;

    char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // cycle thru all text characters
    for (int i = 0; text[i] != '\0'; i++)
    {
        // loop thru all characters in letters
        currentcharacter = toupper(text[i]);
        // check all letters for a match to either letter period(ot exclamations) or spaces
        for (int j = 0; j < 25; j++)
        {
            if (currentcharacter == letters[j])
            {
                number_of_letters++;
                break;
            }
            else if (currentcharacter == ' ')
            {
                number_of_spaces++;
                break;
            }
            else if (currentcharacter == '.' || currentcharacter == '!' || currentcharacter == '?')
            {
                number_of_periods++;
                break;
            }
        }

        // count number of characters in text
        number_of_characters++;
    }

    // check number of periods (number of periods  = sentences)
    number_of_sentences = number_of_periods;
    // check number of spaces (number of spaces + 1 = number of words)
    number_of_words = number_of_spaces + 1;

    double L = ((double) number_of_letters * 100) / (double) number_of_words;
    double S = ((double) number_of_sentences * 100) / (double) number_of_words;

    gradelevel = (0.0588 * L) - (0.296 * S) - 15.8;

    // debug prints
    // printf("number_of_spaces: %d\n", number_of_spaces);
    // printf("number_of_periods: %d\n", number_of_periods);
    // printf("number_of_characters: %d\n", number_of_characters);
    printf("number_of_letters: %d\n", number_of_letters);
    printf("number_of_words: %d\n", number_of_words);
    printf("number_of_sentences: %d\n", number_of_sentences);
    printf("L: %f\n", L);
    printf("S: %f\n", S);
    printf("gradelevel : %f\n", gradelevel);

    if (gradelevel < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (gradelevel >= 16)
    {
        printf("Grade 16+\n");
    }
    else
    {  
        printf("Grade %.f\n", round(gradelevel));
    }

    // index = 0.0588 * L - 0.296 * S - 15.8  ..... means reading level =
    // where L is the average number of letters per 100 words in the text, and S is the average
    // number of sentences per 100 words in the text.
    return 0;
}
c cs50
1个回答
0
投票

我想我会继续接受我的评论并将其转化为答案,因为这是我认为所有程序员都会犯的错误的一个例子。

在测试循环中测试从“A”到“Z”的任何字母,您有一个这样的 for 循环。

for (int j = 0; j < 25; j++)

输入“哈利·波特”示例得出的年级为“4”。

craig@Vera:~/C_Programs/Console/GradeLevel/bin/Release$ ./GradeLevel 
Text: Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
number_of_letters: 213
number_of_words: 56
number_of_sentences: 4
L: 380.357143
S: 7.142857
gradelevel : 4.450714
Grade 4

一旦“j”的值达到“25”就退出循环会跳过对字母“Z”的测试,因此会扭曲测试。

所以这行代码重构如下:

for (int j = 0; j < 26; j++)

这会产生所需的年级水平。

craig@Vera:~/C_Programs/Console/GradeLevel/bin/Release$ ./GradeLevel 
Text: Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
number_of_letters: 214
number_of_words: 56
number_of_sentences: 4
L: 382.142857
S: 7.142857
gradelevel : 4.555714
Grade 5

主要的收获是不仅要了解数组大小和索引值以便测试数组元素,有时还要了解循环的机制以及测试的结果。

© www.soinside.com 2019 - 2024. All rights reserved.