float 和 round 的可读性 cs50 问题
Readability cs50 problem with float and round
我已经尝试了所有方法,但我无法找出代码中的错误。
让用户输入并数字母
int main(void)
{
int letters = 0;
//Getting user input
string text = get_string("Text: ");
//Counting the letters
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
letters++;
}
}
计算单词和句子
int words = 1;
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if ((isspace(text[i])) && (isalpha(text[i+1])) )
{
words++;
}
}
int sentences = 0;
//Checking the symbols and counting the sentences
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
}
然后应用公式
double L = 100.0 * letters / words;
double S = 100.0 * sentences / words;
double index = 0.0588 * L - 0.296 * S - 15.8;
int trueIndex = round(index);
if (trueIndex >= 1 && trueIndex <= 16)
{
printf("Grade %i\n", trueIndex);
}
else
{
if (trueIndex < 1)
{
printf("Before Grade 1\n");
}
if (trueIndex > 16)
{
printf("Grade 16+\n");
}
}
}
它给我这个错误:应为 "Grade 8\n",而不是 "Grade 9\n"。我知道这与我处理花车的方式有关,但我不明白哪里出了问题
尝试从统计单词中删除 (isalpha(text[i+1])
部分。 isalpha()
仅对字母字符 return 为真,即 a - z 、 A - Z。对于引号 return 为假且不计算此类字词。
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?"
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if (isspace(text[i]))
{
words++;
}
}
也许这个块是在制造麻烦:
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if ((isspace(text[i])) && (isalpha(text[i+1])) )
{
words++;
}
}
特别是 (isalpha(text[i+1]))
在您假设的条件下,空格之后将出现字母字符。但是在测试用例中有带引号(")的句子,这不算字母顺序。因此,你不会把它算作一个单词。
测试句,那应该是8级,而你return9级,是:
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?
我试着运行那个程序符合你的条件,那句话确实returns 9。
TL;DR:删除空格后的字母顺序检查,你就成功了。
我已经尝试了所有方法,但我无法找出代码中的错误。
让用户输入并数字母
int main(void)
{
int letters = 0;
//Getting user input
string text = get_string("Text: ");
//Counting the letters
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
letters++;
}
}
计算单词和句子
int words = 1;
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if ((isspace(text[i])) && (isalpha(text[i+1])) )
{
words++;
}
}
int sentences = 0;
//Checking the symbols and counting the sentences
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
}
然后应用公式
double L = 100.0 * letters / words;
double S = 100.0 * sentences / words;
double index = 0.0588 * L - 0.296 * S - 15.8;
int trueIndex = round(index);
if (trueIndex >= 1 && trueIndex <= 16)
{
printf("Grade %i\n", trueIndex);
}
else
{
if (trueIndex < 1)
{
printf("Before Grade 1\n");
}
if (trueIndex > 16)
{
printf("Grade 16+\n");
}
}
}
它给我这个错误:应为 "Grade 8\n",而不是 "Grade 9\n"。我知道这与我处理花车的方式有关,但我不明白哪里出了问题
尝试从统计单词中删除 (isalpha(text[i+1])
部分。 isalpha()
仅对字母字符 return 为真,即 a - z 、 A - Z。对于引号 return 为假且不计算此类字词。
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?"
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if (isspace(text[i]))
{
words++;
}
}
也许这个块是在制造麻烦:
//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++)
{
if ((isspace(text[i])) && (isalpha(text[i+1])) )
{
words++;
}
}
特别是 (isalpha(text[i+1]))
在您假设的条件下,空格之后将出现字母字符。但是在测试用例中有带引号(")的句子,这不算字母顺序。因此,你不会把它算作一个单词。
测试句,那应该是8级,而你return9级,是:
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?
我试着运行那个程序符合你的条件,那句话确实returns 9。 TL;DR:删除空格后的字母顺序检查,你就成功了。