计算错误

Wrong calculations

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

int letters = 0;
int words = 0;
int sentences = 0;

int main(void)
{
    string text = get_string("Text: ");
    printf("\n");
    
    for(int j = 0; j < strlen(text); j++)
    {
        if((text[j] >= 'a' &&  text[j] <= 'z') || (text[j] >= 'A' && text[j] <= 'Z'))
        {
            letters++;
        }
        
        if(text[j] == ' ')
        {
            words++;
            
        }
        
        if(text[j] == '.' || text[j] == '?' || text[j] == '!')
        {
            sentences++;
        }
    }
    printf("Letters: %i\n", letters);
    printf("Words: %i\n", words + 1);
    printf("Sentences: %i\n", sentences);
    
    float L = ((float)letters / (float)words) * 100;
    float S = ((float)sentences / (float)words) * 100;
    float result = round(0.0588 * L - 0.296 * S - 15.8);
    printf("%f\n", L);
    printf("%f\n", S);
       printf("%f\n", result);
    
    
    if(result < 1)
    {
        printf("Before Grade 1");
    }
    if(result == 1)
    {
        printf("Grade 1");
    }
    if(result == 2)
    {
        printf("Grade 2");
    }
    if(result == 3)
    {
        printf("Grade 3");
    }
    if(result == 4)
    {
        printf("Grade 4");
    }
    if(result == 5)
    {
        printf("Grade 5");
    }
    if(result == 6)
    {
        printf("Grade 6");
    }
    if(result == 7)
    {
        printf("Grade 7");
    }
    if(result == 8)
    {
        printf("Grade 8");
    }
    if(result == 9)
    {
        printf("Grade 9");
    }
    if(result == 10)
    {
        printf("Grade 10");
    }
    if(result == 11)
    {
        printf("Grade 11");
    }
    if(result == 12)
    {
        printf("Grade 12");
    }
    if(result == 13)
    {
        printf("Grade 13");
    }
    if(result == 14)
    {
        printf("Grade 14");
    }
    if(result == 15)
    {
        printf("Grade 15");
    }
    if(result >= 16)
    {
        printf("Grade 16+");
    }
    printf("\n");
}

正文:

 Congratulations! Today is your day. You're off to Great Places! You're off and away!

结果:

Letters: 65
Words: 14
Sentences: 4
500.000000
30.769232
4.000000
Grade 4

我需要在我的代码中计算“0.0588 * L - 0.296 * S - 15.8”公式,但出于某种原因,它对某些文本给出了错误的答案,对某些文本给出了正确的答案。在这个特定的示例中,它应该给我 'Grade 3' 但它却给我 'Grade 4',如果我输入 10 年级的文本,它会给我 'Grade 10'。 'L' 和 'S' 的计算是错误的,在这个例子中,它们应该是 L = 464.29 和 S = 28.57。我真的不知道我哪里错了,有人可以帮我吗?

问题是你是通过计算空格来计算单词的,所以你没有计算最后一个单词。您可以通过将 words 初始化为 1.

来解决问题

我还看到你在写 printf("Words: %i\n", words + 1); 后就注意到了这个问题,那你为什么在计算中不加 1?你打印 14 作为字数,但是你用 13 来计算字的平均长度。

无论如何,你绝对应该用那个改变最终的印刷品

if(result < 16) {
    printf("Grade %d\n", result);
}
else {
    puts("Grade 16+");
}

甚至只是

result < 16 ? printf("Grade %d\n", result) : puts("Grade 16+");

看来你数词数不够。您计算了单词之间的空格数,得出的数字比单词数少一个。在 printf("Words: %i\n", words + 1); 中打印数字时以及在 printf("Words: %i\n", words + 1); 中计算 S 时会进行调整,但在 float L = ((float)letters / (float)words) * 100; 中计算 L 时不会进行调整。 =23=]

将最后一行更改为 float L = ((float)letters / (float)(words+1)) * 100; 会得到您期望的答案“Grade 3”。

但是,更好的解决方案可能是使用 int words = 1; 而不是 int words = 0; 来初始化 words,并将 printfS 的计算更改为使用 words 而不是 words + 1.

(此外,更复杂的程序会考虑如果空格出现在任何单词之前、最后一个单词之后或单词之间多次出现时会发生什么情况。)