CS50可读性pset2中的错误

CS50可读性pset2中的错误,c,debugging,rounding,cs50,floating-accuracy,C,Debugging,Rounding,Cs50,Floating Accuracy,我的代码编译正确,除一个测试用例外,我通过了所有测试用例。 错误出现在一句话中:“在我年轻和更脆弱的时候,我父亲给了我一些建议,从那时起我就一直在思考这些建议。”这句话要求7级,但我的代码输出8级 以下是部署check50时的输出: 用多个单词处理单个句子 应为“7级”,而不是“8级” 在打印出字母数(96)、单词数(23)、句子数(1)、索引数(7.548)和四舍五入数(索引数)后,我发现所有数字都是正确的。 但我不明白如何将7.548四舍五入为7 请帮助我调试代码,并让我知道我遗漏了什么。

我的代码编译正确,除一个测试用例外,我通过了所有测试用例。 错误出现在一句话中:“在我年轻和更脆弱的时候,我父亲给了我一些建议,从那时起我就一直在思考这些建议。”这句话要求7级,但我的代码输出8级

以下是部署check50时的输出:

用多个单词处理单个句子

应为“7级”,而不是“8级”

在打印出字母数(96)、单词数(23)、句子数(1)、索引数(7.548)和四舍五入数(索引数)后,我发现所有数字都是正确的。 但我不明白如何将7.548四舍五入为7

请帮助我调试代码,并让我知道我遗漏了什么。 谢谢大家!

#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;
}`
#包括
#包括
#包括
#包括
#包括
整数计数字母(整数长度,字符arr[]);
整数计数(整数长度,字符arr[]);
int count_句(int n,char arr[]);
内部主(空)
{
//从用户获取输入
字符串文本=获取字符串(“文本:”);
//数信
int n=strlen(文本);
int字母=计数字母(n,文本);
int-word=计数单词(n,文本);
int句子=计数句子(n,文本);
浮点数L=(字母*100)/字;
浮点数S=(句子*100)/字;
整数指数=圆形((0.0588*L)-(0.296*S)-15.8);
如果(指数<1)
{
printf(“1级之前”);
}
否则,如果(索引>16)
{
printf(“16+级”);
}
其他的
{
printf(“等级%i\n”,索引);
}
}
整数计数字母(整数长度,字符arr[])
{
整数字母=0;
for(int i=0;i='a'&&tolower(arr[i])试试这个(改变它)

变量“word”需要指定为浮点。 我也有同样的问题。

#包括
#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: ");

    // Length of text
    int  n = strlen(text);
    
    //Calculating Coleman-Liau index 
    float L = ((float)count_letters(n, text) / (float)count_words(n, text)) * 100;
    float S = ((float)count_sentences(n, text) / (float)count_words(n, text)) * 100;
    
    int index = round((0.0588 * L) - (0.296 * S) - 15.8);
    
    // Print Grades
    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; // Letter counter 
    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 ; // word counter
    for (int i = 0 ; i < length ; i++)
    {
        if (arr[i] == ' ')
        {
            words++;
        }
    }
    return words;
}

int count_sentences(int length, char arr[])
{
    int sentence = 0; // sentence counter
    for (int i = 0 ; i < length ; i++)
    {
        if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
        {
            sentence ++;
        }
    }
    
    return sentence;
}
#包括 #包括 #包括 #包括 整数计数字母(整数长度,字符arr[]); 整数计数(整数长度,字符arr[]); int count_句(int n,char arr[]); 内部主(空) { //从用户获取输入 字符串文本=获取字符串(“文本:”); //文本长度 int n=strlen(文本); //计算Coleman-Liau指数 浮点L=((浮点)计数字母(n,文本)/(浮点)计数单词(n,文本))*100; 浮点数S=((浮点数)句子数(n,文本)/(浮点数)单词数(n,文本))*100; 整数指数=圆形((0.0588*L)-(0.296*S)-15.8); //打印等级 如果(指数<1) { printf(“1级之前”); } 否则,如果(索引>16) { printf(“16+级”); } 其他的 { printf(“等级%i\n”,索引); } } 整数计数字母(整数长度,字符arr[]) { int letters=0;//字母计数器 for(int i=0;i如果(tolower(arr[i])>='a'&&tolower(arr[i])
float L=(字母*100.0)/word;
与下一行相同。即使将值分配给一个float,也会对所有整数进行运算,因此会截断。不要发布(链接到)文本输出图片-显示问题中的文本输出。-1用于发布文本图片。您认为我们如何在您的第一个屏幕截图中复制并粘贴链接,以获得真正有用的信息。谢谢@RetiredInja我理解这个问题。@JonathanLeffler下次将尝试记住。这是我第一次尝试asking一个问题,因此链接。我想我是在提供信息:'))很抱歉给您带来不便。在C中,当一个int除以一个int时,结果也是一个int,任何余数都将被丢弃。换句话说,结果将被截断。在计算L和S时,您可能希望在执行除法之前将一个或多个值强制转换为浮点值!
#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: ");

    // Length of text
    int  n = strlen(text);
    
    //Calculating Coleman-Liau index 
    float L = ((float)count_letters(n, text) / (float)count_words(n, text)) * 100;
    float S = ((float)count_sentences(n, text) / (float)count_words(n, text)) * 100;
    
    int index = round((0.0588 * L) - (0.296 * S) - 15.8);
    
    // Print Grades
    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; // Letter counter 
    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 ; // word counter
    for (int i = 0 ; i < length ; i++)
    {
        if (arr[i] == ' ')
        {
            words++;
        }
    }
    return words;
}

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