You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Pset2 Readability代码无法输出正确等级,请求排查修复

Hey there! Let's troubleshoot why your CS50 Readability code is returning "Before grade 1" for every input—your test case should definitely output "Grade 2", so let's jump straight to the root cause: your calculations for the Coleman-Liau formula are backwards and hitting an integer division snag.

What's Going Wrong?

The core issue is in how you're calculating l and s (the two values needed for the Coleman-Liau index):

  1. Reversed Logic: The formula requires:
    • L: Average number of letters per 100 words → (total letters / total words) * 100
    • S: Average number of sentences per 100 words → (total sentences / total words) * 100
      But your code computes 100 / letters * words and 100 / sentances * words, which is the inverse of what you need. This completely skews the values fed into the formula.
  2. Integer Division: In C, dividing two integers (like 100 / letters) truncates any decimal part. For example, if your text has 65 letters, 100 / 65 becomes 1 instead of ~1.538. This makes your l and s values way too small (or even zero), leading to a negative grade that triggers the "Before grade 1" output.

Fixed Code

Here's the corrected version of your code, with key fixes highlighted:

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

int main(void) {
    string text = get_string("Text: ");
    
    int letters = 0;
    int words = 1;
    int sentances = 0;
    int n = strlen(text);
    
    for (int i = 0; i < n; i++) {
        if (isalpha(text[i])) {
            letters++;
        }
        else if (isspace(text[i])) {
            words++;
        }
        else if (text[i] == '.' || text[i] == '!' || text[i] == '?') {
            sentances++;
        }
    }
    
    // Fixed: Calculate L and S correctly, with float conversion to avoid integer division
    float l = ((float)letters / words) * 100;
    float s = ((float)sentances / words) * 100;
    
    float grade = 0.0588 * l - 0.296 * s - 15.8;
    grade = round(grade);
    
    printf("%i letters, %i words, %i sentences\n", letters, words, sentances);
    
    // Cleaned up conditionals for clarity
    if (grade >= 1 && grade <= 16) {
        printf("Grade %i\n", (int)grade);
    }
    else if (grade < 1) {
        printf("Before grade 1\n");
    }
    else {
        printf("Grade 16+\n");
    }
}

Key Fixes Explained

  • We cast letters and sentances to float before dividing by words to ensure we get a decimal result instead of truncated integers.
  • We reverse the calculation for l and s to match the Coleman-Liau formula's requirements.
  • We cleaned up the conditional logic with else if to make it more readable (though this wasn't causing the bug).

If you run this corrected code with your test text, it should now output "Grade 2" as expected!

内容的提问来源于stack exchange,提问作者Zoey Herbst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:27:45