c-我如何在主函数中使用函数原型的值?-组合单独的值



所以我的任务是评估文本提示的阅读水平。在下面的代码中,我已经设法用三种方式分析了文本。字母、单词和句子的数量。然而,为了计算阅读水平,我需要将这些值组合成一个公式:

"指数=0.0588*L-0.296*S-15.8

其中,L是文本中每100个单词的平均字母数,S是文本中每个单词的平均句子数。

("修改可读性.c,使其不输出字母、单词和句子的数量,而是输出Coleman-Liau指数给出的等级(例如"2级"或"8级"(。一定要将得到的索引号四舍五入到最接近的整数!

如果得到的指数是16或更高(相当于或大于一个高年级本科生的阅读水平(;16+级;而不是给出确切的索引号。如果索引号小于1,则程序应输出"0";在一年级之前"(

所以,是的,基本上我有所有需要的值,但我不知道如何将它们用于计算最终值的公式,因为它们都在函数原型中,我无法在主函数中将它们组合在一起。。。

#include <ctype.h>
#include <string.h>
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int count_letters(string letters);
int count_words(string words);
int count_sentences(string sentences);
int main(void)
{
string text = get_string("Text: ");
count_letters(text);
count_words(text);
count_sentences(text);
}
int count_letters(string letters)
{
int count = 0;
for (int i = 0; i < strlen(letters); i++)
{
if (isalpha(letters[i]) != 0)
{
count++;
}
}
printf("%i letter(s)n", count);
return count;
}
int count_words(string words)
{
int count_w = 0;
for (int j = 0; j < strlen(words); j++)
{
if (isspace(words[j]) != 0)
{
count_w++;
}
}
count_w++;
printf("%i word(s)n", count_w);
return count_w;
}
int count_sentences(string sentences)
{
int count_s = 0;
for (int k = 0; k < strlen(sentences); k++)
{
if ((int) sentences[k] == 33)
{
count_s++;
}
if ((int) sentences[k] == 46)
{
count_s++;
}
if ((int) sentences[k] == 63)
{
count_s++;
}
}
printf("%i sentence(s)n", count_s);
return count_s;
}

您需要使用函数返回的值。

int total_letters = count_letters(text);

等等。当你掌握了这三个单词后,你可以计算出每100个单词的字母数,并用这个等式来计算等级。

最新更新