CS50 Problem Set 2 Readability程序评分功能异常求助
Fixing Your CS50 Readability Test Case Issues
Hey there! Let's figure out why your Readability program is failing some test cases. I've spotted two key bugs in your code that are throwing off the calculations—let's break them down step by step.
1. The Core Problems
Incorrect Word Count Logic
Your current code counts every single space as a word separator, which leads to two big issues:
- If there are multiple consecutive spaces (like
"Hello world"), you'll overcount the number of words. - Worse, you use the raw space count (
word_count) to calculate the Coleman-Liau index instead of the actual number of words (word_count + 1), which completely skews yourL(letters per 100 words) andS(sentences per 100 words) values.
For example, if your input is "Hello world.":
- Your code counts 1 space, so
word_count = 1 - You calculate
L = (10 / 1) * 100 = 1000(way too high!) - The correct
Lshould be(10 / 2) * 100 = 50
2. Corrected Code
Here's the fixed version with comments pointing out the key changes:
#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <math.h> #include <string.h> int main(void){ string text = get_string("Text: "); int letter_count = 0; int word_count = 0; int sentence_count = 0; int length = strlen(text); for(int i = 0; i < length; i++){ // Count alphabetic characters as letters if (isalpha(text[i])){ letter_count++; } // Count sentences by checking end punctuation else if(text[i] == '.' || text[i] == '!' || text[i] == '?') { sentence_count++; } // Count words: only increment if current space isn't consecutive else if (isspace(text[i])){ if (i == 0 || !isspace(text[i-1])){ word_count++; } } } // Convert separator count to actual word count (add 1) if (length > 0){ word_count++; } // Calculate Coleman-Liau index with correct word count float L = ((float)letter_count / word_count) * 100; float S = ((float)sentence_count / word_count) * 100; float index = 0.0588 * L - 0.296 * S - 15.8; // Output the grade level if (index >= 16) { printf("Grade 16+\n"); } else if (index < 1) { printf("Before Grade 1\n"); } else { printf("Grade %i\n", (int) round(index)); } // Debug prints (remove once you confirm it works) printf("%i Letter(s)\n", letter_count); printf("%i Word(s)\n", word_count); printf("%i Sentence(s)\n", sentence_count); }
Key Fixes Explained
- Consecutive Space Handling: We now only count a space as a word separator if it's not immediately followed by another space, which fixes overcounting from multiple spaces.
- Correct Word Count for Calculations: We use the actual word count (separator count + 1) instead of raw space counts when computing
LandS, which aligns with the Coleman-Liau formula requirements. - Edge Case Safeguard: Added a check to avoid incrementing word count for empty text (though CS50's test cases won't include this, it's a nice robustness touch).
Test with CS50 Samples
Let's verify with one of CS50's sample inputs:
Input: "Congratulations! Today is your day. You're off to Great Places! You're off and away!"
- Correct word count: 14
- Correct letter count: 60
- Correct sentence count: 4
- Calculated index: ~3.1 → Grade 3
Your corrected code should output this correctly now.
内容的提问来源于stack exchange,提问作者Arsham
相关产品推荐
相关产品推荐

