微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

“索引= 0.0588 * L-0.296 * S-15.8

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

(“修改可读性.c,以代替输出字母,单词和句子的数量,而是输出由科尔曼-廖(Coleman-Liau)索引给出的等级水平(例如,” 2级“或” 8级“) 。确保将得到的索引号四舍五入到最接近的整数!

如果所得的索引号为16或更高(等于或大于高年级本科生的阅读水平),则您的程序应输出“ 16年级以上”,而不是给出确切的索引号。如果索引号小于1,则您的程序应输出“在1级之前”。”

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

#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个单词的字母数,并使用等式计算成绩等级。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。