Readability 可读性
Problem to Solve 待解决的问题
According to Scholastic, E.B. White’s Charlotte’s Web is between a second- and fourth-grade reading level, and Lois Lowry’s The Giver is between an eighth- and twelfth-grade reading level. What does it mean, though, for a book to be at a particular reading level?
根据 Scholastic 的数据,《夏洛特的网》的阅读级别介于二到四年级之间,《赐予者》的阅读级别介于八年级到十二年级之间。但是,一本图书的特定阅读级别究竟意味着什么呢?
Well, in many cases, a human expert might read a book and make a decision on the grade (i.e., year in school) for which they think the book is most appropriate. But an algorithm could likely figure that out too!
在许多情况下,一位人类专家可能会阅读一本书,并决定这本书最适合哪个年级的学生(即,在校年级)。但是,一个算法也可能计算出这个结果!
In a file called readability.c
in a folder called readability
, you’ll implement a program that calculates the approximate grade level needed to comprehend some text. Your program should print as output “Grade X” where “X” is the grade level computed, rounded to the nearest integer. If the grade level is 16 or higher (equivalent to or greater than a senior undergraduate reading level), your program should output “Grade 16+” instead of giving the exact index number. If the grade level is less than 1, your program should output “Before Grade 1”.
在一个名为 readability.c
的文件中,该文件位于名为 readability
的文件夹内,你将实现一个程序来计算理解某些文本所需的近似年级水平。你的程序应将计算出的年级水平(四舍五入到最接近的整数)作为输出打印为“X 年级”,其中“X”为计算出的年级水平。如果年级水平为 16 或更高(相当于或高于大学四年级学生的阅读水平),则你的程序应输出“16 年级以上”,而不是给出确切的索引号。如果年级水平小于 1,则你的程序应输出“一年级以下”。
Demo 演示
Background 背景
So what sorts of traits are characteristic of higher reading levels? Well, longer words probably correlate with higher reading levels. Likewise, longer sentences probably correlate with higher reading levels, too.
那么,哪些特征是更高阅读水平的标志呢?嗯,更长的单词可能与更高的阅读水平相关。同样,更长的句子也可能与更高的阅读水平相关。
A number of “readability tests” have been developed over the years that define formulas for computing the reading level of a text. One such readability test is the Coleman-Liau index. The Coleman-Liau index of a text is designed to output that (U.S.) grade level that is needed to understand some text. The formula is
多年来,已经开发了许多“可读性测试”,这些测试定义了计算文本阅读水平的公式。其中一项可读性测试是科尔曼-利亚乌指数。文本的科尔曼-利亚乌指数旨在输出理解某些文本所需的(美国)年级水平。公式是
index = 0.0588 * L - 0.296 * S - 15.8
where L
is the average number of letters per 100 words in the text, and S
is the average number of sentences per 100 words in the text.
其中 L
是文本中每 100 个单词的平均字母数, S
是文本中每 100 个单词的平均句子数。
Specification 规范
For the purpose of this problem, we’ll consider any sequence of characters separated by a space to be a word (so a hyphenated word like “sister-in-law” should be considered one word, not three). You may assume that a sentence:
在本题中,我们将任何以空格分隔的字符序列视为一个单词(因此,像“sister-in-law”这样的带连字符的单词应被视为一个单词,而不是三个)。您可以假设一个句子:
- will contain at least one word;
将包含至少一个单词; - will not start or end with a space; and
不会以空格开头或结尾;并且 - will not have multiple spaces in a row.
不会出现连续多个空格。
Under those assumptions, you might already be able to find a mathematical relationship between the number of words and the number of spaces.
基于这些假设,你或许已经能够找到单词数量和空格数量之间的数学关系。
You are, of course, welcome to attempt a more sophisticated solution that can tolerate multiple spaces between words! We’ve tuned check50
to accept either approach to spaces, so the choice is yours.
当然,你也可以尝试更复杂的解决方案,使其能够容忍单词之间存在多个空格!我们已经调整了 check50
以接受这两种处理空格的方法,所以选择权在你。
Advice 建议
Click the below toggles to read some advice!
点击下面的开关阅读一些建议!
Write some code that you know will compile
编写一些你确定可以编译的代码
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
}
Notice that you’ve now included a few header files that will give you access to functions which might help you solve this problem.
Write some pseudocode before writing more code
在编写更多代码之前,先编写一些伪代码
If unsure how to solve the problem itself, break it down into smaller problems that you can probably solve first. For instance, this problem is really only a handful of problems:
- Prompt the user for some text
- Count the number of letters, words, and sentences in the text
- Compute the Coleman-Liau index
- Print the grade level
Let’s write some pseudcode as comments to remind you to do just that:
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// Prompt the user for some text
// Count the number of letters, words, and sentences in the text
// Compute the Coleman-Liau index
// Print the grade level
}
Convert the pseudocode to code
将伪代码转换为代码
First, consider how you might prompt the user for some text. Recall that get_string
, a function in the CS50 library, can prompt the user for a string.
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// Prompt the user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
// Compute the Coleman-Liau index
// Print the grade level
}
Now that you’ve collected input from the user, you can begin to analyze that input. First, try to count the number of letters in the text. Consider letters to be uppercase or lowercase alphabetical character, not punctuation, digits, or other symbols.
One way to approach this problem is to create a function called count_letters
that takes a string, text
, as input, and then returns the number of letters in that text as an int
.
int count_letters(string text)
{
// Return the number of letters in text
}
You’ll need to write your own code to count the number of letters in the text. But someone more experienced than you may have already written a function to determine if a character is alphabetical. This is a good opportunity to use the CS50 manual, a collection of explanations of common functions in the C Standard Library.
You can integrate count_letters
into the code you’ve already written, as follows.
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_letters(string text);
int main(void)
{
// Prompt the user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
int letters = count_letters(text);
// Compute the Coleman-Liau index
// Print the grade level
}
int count_letters(string text)
{
// Return the number of letters in text
}
Next, write a function to count words.
int count_words(string text)
{
// Return the number of words in text
}
You can now integrate count_words
into your program as follows:
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_letters(string text);
int count_words(string text);
int main(void)
{
// Prompt the user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
int letters = count_letters(text);
int words = count_words(text);
// Compute the Coleman-Liau index
// Print the grade level
}
int count_letters(string text)
{
// Return the number of letters in text
}
int count_words(string text)
{
// Return the number of words in text
}
Finally, write a function to count sentences. You can consider any sequence of characters that ends with a .
or a !
or a ?
to be a sentence.
int count_sentences(string text)
{
// Return the number of sentences in text
}
You can integrate count_sentences
into your program as follows:
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
// Prompt the user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text);
// Compute the Coleman-Liau index
// Print the grade level
}
int count_letters(string text)
{
// Return the number of letters in text
}
int count_words(string text)
{
// Return the number of words in text
}
int count_sentences(string text)
{
// Return the number of sentences in text
}
Finally, compute the Coleman-Liau index and print the resulting grade level.
- Recall that the Coleman-Liau index is computed using
index = 0.0588 * L - 0.296 * S - 15.8
L
is the average number of letters per 100 words in the text: that is, the number of letters divided by the number of words, all multiplied by 100.S
is the average number of sentences per 100 words in the text: that is, the number of sentences divided by the number of words, all multiplied by 100.- You’ll want to round the result to the nearest whole number, so recall that
round
is declared inmath.h
, per manual.cs50.io. - Recall that, when dividing values of type
int
in C, the result will also be anint
, with any remainder (i.e., digits after the decimal point) discarded. Put another way, the result will be “truncated.” You might want to cast your one or more values tofloat
before performing division when calculatingL
andS
!
If the resulting index number is 16 or higher (equivalent to or greater than a senior undergraduate reading level), your program should output “Grade 16+” instead of outputting an exact index number. If the index number is less than 1, your program should output “Before Grade 1”.
Walkthrough 演练
How to Test 如何测试
Try running your program on the following texts, to ensure you see the specified grade level. Be sure to copy only the text, no extra spaces.
尝试使用以下文本运行你的程序,以确保你看到指定的年级水平。请确保只复制文本,不要复制额外的空格。
One fish. Two fish. Red fish. Blue fish.
(Before Grade 1)One fish. Two fish. Red fish. Blue fish.
(一年级前)Would you like them here or there? I would not like them here or there. I would not like them anywhere.
(Grade 2)Would you like them here or there? I would not like them here or there. I would not like them anywhere.
(二年级)Congratulations! Today is your day. You're off to Great Places! You're off and away!
(Grade 3)Congratulations! Today is your day. You're off to Great Places! You're off and away!
(三年级)Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
(Grade 5)Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
(五年级)In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
(Grade 7)In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
(七年级)Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"
(Grade 8)Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"
(八年级)When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears of never being able to play football were assuaged, he was seldom self-conscious about his injury. His left arm was somewhat shorter than his right; when he stood or walked, the back of his hand was at right angles to his body, his thumb parallel to his thigh.
(Grade 8)When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears of never being able to play football were assuaged, he was seldom self-conscious about his injury. His left arm was somewhat shorter than his right; when he stood or walked, the back of his hand was at right angles to his body, his thumb parallel to his thigh.
(八年级)There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy.
(Grade 9)There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy.
(9 年级)It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.
(Grade 10)It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.
(10 年级)A large class of computational problems involve the determination of properties of graphs, digraphs, integers, arrays of integers, finite families of finite sets, boolean formulas and elements of other countable domains.
(Grade 16+)A large class of computational problems involve the determination of properties of graphs, digraphs, integers, arrays of integers, finite families of finite sets, boolean formulas and elements of other countable domains.
(16 岁及以上)
Correctness 正确性
In your terminal, execute the below to check your work’s correctness.
在你的终端中,执行以下命令来检查你的工作是否正确。
check50 cs50/problems/2025/x/readability
Style 风格
Execute the below to evaluate the style of your code using style50
.
执行以下命令使用 style50
来评估你的代码风格。
style50 readability.c
How to Submit 提交方法
In your terminal, execute the below to submit your work.
在你的终端中,执行以下命令提交你的作业。
submit50 cs50/problems/2025/x/readability