CS50 可读性 pset2 中的错误 [英] Bug in CS50 Readability pset2

查看:100
本文介绍了CS50 可读性 pset2 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码编译正确,我通过了每个测试用例除了一个.错误出现在这句话中——在我更年轻、更脆弱的岁月里,我父亲给了我一些建议,从那时起我就一直在脑海里翻来覆去.",它预计是 7 级,但我的代码输出了 8 级.

My code compiled correctly and I passed every test case except one. The bug occurs in the sentence- "In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.", which expects a Grade 7, but My code outputs grade 8.

这是部署 check50 时的输出:

Here is the output when check50 is deployed:

处理包含多个单词的单个句子

handles single sentence with multiple words

预期的7 年级\n"而不是8年级\n"

expected "Grade 7\n" and not "Grade 8\n"

在打印出字母 (96)、单词 (23)、句子 (1)、索引 (7.548) 和 round(index) 的数量后,在这个特殊情况下为 8,明确地,我看到所有的数字都是正确的.但我不明白 7.548 如何四舍五入为 7.

After printing out the number of letters ( 96) , words (23 ) sentences (1) , index (7.548) and round(index) which is 8 for this particular case, explicitly, I saw that all of the numbers are correct. But I do not understand how 7.548 can be rounded to 7.

请帮助我调试代码,并告诉我我遗漏了什么.谢谢!

Please help me in Debugging the code, and let me know what I'm missing. Thank you!

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

int count_letters(int length , char arr[]);

int count_words(int length , char arr[]);

int count_sentences(int n, char arr[]);

int main (void)
{
    // Getting Input from user
    string text = get_string("Text: ");

    // Counting letters
    int  n = strlen(text);

    int letter = count_letters(n, text);
    int word = count_words(n, text);
    int sentence = count_sentences(n, text);
    
    float L = (letter*100)/word;
    float S = (sentence*100)/word;

    int index = round((0.0588 * L) - (0.296 * S) - 15.8);

    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }

}

int count_letters(int length , char arr[])
{
    int letters = 0;
    for ( int i = 0 ; i < length ;  i++)
    {

        if (tolower(arr[i]) >= 'a' && tolower(arr[i]) <= 'z' )
        {
            letters++ ;
        }

    }

    return letters;
}

int count_words(int length , char arr[])
{
    int words = 1 ;
    for (int i =0 ; i < length ; i++)
    {
        if (arr[i]== ' ')
        {
            words++;
        }
    }
    return words;
}

int count_sentences(int length, char arr[])
{
    int sentence = 0;
    for (int i = 0 ; i <length ; i++)
    {
        if (arr[i] == '.')
        {
           sentence ++;
        }
        else if (arr[i] == '?')
        {
            sentence ++;
        }
        else if ( arr[i] == '!')
        {
            sentence++;
        }
    }
    return sentence;
}`

推荐答案

试试这个(改变它)

float L = (letter / (float) word) * 100;
float S = (sentence / (float) word) * 100;

变量word"需要指定为浮点数.我遇到了同样的问题.

The variable "word" needs to be specified as a float. I had the same issue.

这篇关于CS50 可读性 pset2 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆