我的代码正确编译,并且通过了所有测试用例,除了一个测试用例。 该错误出现在以下句子中:“在我更年轻,更脆弱的岁月中,父亲给了我一些建议,从那时起我就一直在思考。”该句子的期望等级为7级,但是我的代码输出等级8。 / p>
以下是部署check50时的输出:
处理包含多个单词的单个句子
预期为“ 7年级\ n”,而不是“ 8年级\ n”
在打印出字母(96),单词(23)句子(1),索引(7.548)和round(index)的数量后,对于该特定情况,该数量明确为8,我清楚地看到所有数字都是正确。 但是我不明白如何将7.548舍入为7。
请帮助我调试代码,并让我知道我所缺少的内容。 谢谢!
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int count_letters(int length , char arr[]);
int count_words(int length , char arr[]);
int count_sentences(int n, char arr[]);
int main (void)
{
// Getting Input from user
string text = get_string("Text: ");
// Counting letters
int n = strlen(text);
int letter = count_letters(n, text);
int word = count_words(n, text);
int sentence = count_sentences(n, text);
float L = (letter*100)/word;
float S = (sentence*100)/word;
int index = round((0.0588 * L) - (0.296 * S) - 15.8);
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index > 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", index);
}
}
int count_letters(int length , char arr[])
{
int letters = 0;
for ( int i = 0 ; i < length ; i++)
{
if (tolower(arr[i]) >= 'a' && tolower(arr[i]) <= 'z' )
{
letters++ ;
}
}
return letters;
}
int count_words(int length , char arr[])
{
int words = 1 ;
for (int i =0 ; i < length ; i++)
{
if (arr[i]== ' ')
{
words++;
}
}
return words;
}
int count_sentences(int length, char arr[])
{
int sentence = 0;
for (int i = 0 ; i <length ; i++)
{
if (arr[i] == '.')
{
sentence ++;
}
else if (arr[i] == '?')
{
sentence ++;
}
else if ( arr[i] == '!')
{
sentence++;
}
}
return sentence;
}`
答案 0 :(得分:1)
尝试一下(更改)
float L = (letter / (float) word) * 100;
float S = (sentence / (float) word) * 100;
变量“ word”需要指定为浮点数。 我有同样的问题。
答案 1 :(得分:0)
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int count_letters(int length, char arr[]);
int count_words(int length, char arr[]);
int count_sentences(int n, char arr[]);
int main(void)
{
// Getting Input from user
string text = get_string("Text: ");
// Length of text
int n = strlen(text);
//Calculating Coleman-Liau index
float L = ((float)count_letters(n, text) / (float)count_words(n, text)) * 100;
float S = ((float)count_sentences(n, text) / (float)count_words(n, text)) * 100;
int index = round((0.0588 * L) - (0.296 * S) - 15.8);
// Print Grades
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index > 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", index);
}
}
int count_letters(int length, char arr[])
{
int letters = 0; // Letter counter
for (int i = 0 ; i < length ; i++)
{
if (tolower(arr[i]) >= 'a' && tolower(arr[i]) <= 'z')
{
letters++ ;
}
}
return letters;
}
int count_words(int length, char arr[])
{
int words = 1 ; // word counter
for (int i = 0 ; i < length ; i++)
{
if (arr[i] == ' ')
{
words++;
}
}
return words;
}
int count_sentences(int length, char arr[])
{
int sentence = 0; // sentence counter
for (int i = 0 ; i < length ; i++)
{
if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
{
sentence ++;
}
}
return sentence;
}