CS50第2周Readability作业代码问题:中间等级输出异常
CS50 Readability作业:中间等级输出异常问题
问题描述
我编写的C语言代码用于完成CS50第2周Readability作业,功能为接收用户输入文本,统计其中的字母、单词、句子数量,再通过公式计算可读性指数:
index = 0.0588 * L - 0.296 * S - 15.8
其中L为每百词平均字母数,S为每百词平均句子数。
输出规则:
- 指数小于1时输出“Before Grade 1”
- 指数大于16时输出“Grade 16+”
- 其余情况输出对应等级(如指数为12则输出“Grade level 12”)
目前代码仅在等级小于1和大于16时输出正确,中间等级的输出存在异常。
原代码
#include <cs50.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> int count_letters(string text); int count_sentances(string text); int count_words(string text); int Grade_level(int i, int j, int k); int main(void) { string text = get_string("Text = "); int num_letters = count_letters(text); int num_sentances = count_sentances(text); int num_words = count_words(text); int gradeLevel = Grade_level(num_letters, num_sentances, num_words); if (gradeLevel < 1) { printf("Before Grade 1\n"); } else if (gradeLevel > 16) { printf("Grade 16+\n"); } else { printf("Grade level %d\n", gradeLevel); } } int count_letters(string text) { int letters = 0; for (int i = 0; i < strlen(text); i++) { if (isalpha(text[i])) { letters++; } } return letters; } int count_sentances(string text) { int sentances = 0; for (int i = 0; i < strlen(text); i++) { if (text[i] == '.'|| text[i] == '!' || text[i] == '?') { sentances++; } } return sentances; } int count_words(string text) { int words = 1; for (int i = 0; i < strlen(text); i++) { if (text[i] == ' ') { words++; } } return words; } int Grade_level(int num_letters, int num_words, int num_sentances) { float index, L, S; L = (num_letters/num_words) * 100; S = (num_sentances/num_words) * 100; index = (0.0588 * L) - (0.296 * S) - 15.8; index = round(index); return (int)index; }
问题原因
核心错误在于Grade_level函数中的整数除法:num_letters、num_words、num_sentances都是整数类型,执行num_letters/num_words时会触发整数除法,结果会被截断为整数(例如123个字母对应10个单词,123/10的结果是12而非12.3),导致L和S的计算值严重偏离实际,最终使得可读性指数计算错误。
修复方案
将除法操作中的其中一个操作数强制转换为浮点数,确保执行浮点除法:
在Grade_level函数中修改L和S的计算代码:
L = ((float)num_letters / num_words) * 100; S = ((float)num_sentances / num_words) * 100;
另外,代码中存在拼写错误:count_sentances和num_sentances应为count_sentences和num_sentences,虽然不影响功能,但建议修正以保持代码规范。
修复后的完整代码
#include <cs50.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> int count_letters(string text); int count_sentences(string text); int count_words(string text); int Grade_level(int i, int j, int k); int main(void) { string text = get_string("Text = "); int num_letters = count_letters(text); int num_sentences = count_sentences(text); int num_words = count_words(text); int gradeLevel = Grade_level(num_letters, num_sentences, num_words); if (gradeLevel < 1) { printf("Before Grade 1\n"); } else if (gradeLevel > 16) { printf("Grade 16+\n"); } else { printf("Grade level %d\n", gradeLevel); } } int count_letters(string text) { int letters = 0; for (int i = 0; i < strlen(text); i++) { if (isalpha(text[i])) { letters++; } } return letters; } int count_sentences(string text) { int sentences = 0; for (int i = 0; i < strlen(text); i++) { if (text[i] == '.'|| text[i] == '!' || text[i] == '?') { sentences++; } } return sentences; } int count_words(string text) { int words = 1; for (int i = 0; i < strlen(text); i++) { if (text[i] == ' ') { words++; } } return words; } int Grade_level(int num_letters, int num_words, int num_sentences) { float index, L, S; L = ((float)num_letters / num_words) * 100; S = ((float)num_sentences / num_words) * 100; index = (0.0588 * L) - (0.296 * S) - 15.8; index = round(index); return (int)index; }
内容的提问来源于stack exchange,提问作者Alok Chedambath
相关产品推荐
相关产品推荐

