【问题标题】:Issues with a string count, integers to float, Grade formula based on the number of letters, words, and sentences, in C字符串计数问题、浮点整数、基于字母、单词和句子数量的评分公式,在 C 中
【发布时间】:2020-04-16 20:19:53
【问题描述】:

我正在编写一个程序来生成字母、单词和句子的数量,提供输入文本。
有了这些值,我想根据一个公式生成一个数字(评估“可读性”),其中字母、单词和句子中的数字作为变量。

请参阅此代码的最后部分(关于成绩公式)。

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{
    char str[1000];
    int i=0, chr=0, st=1, sn=0;

    printf("Text: ");
    fgets(str, sizeof(str), stdin);

    for( i = 0; str[i] != '\0' ; i++)
    {
        chr = chr + 1;
    }

    printf("%d letters\n", chr);

    for (i = 0; i <= chr - 1; i++)
    {
        if (str[i] == ' ')
        {
            st = st + 1;
        }
    }

    printf("%d words\n", st);

    for (i = 0; i <= chr - 1; i++)
    {
        if (str[i] == '.' || str[i] == '!' || str[i] == '?')
        {
            sn = sn + 1;
        }
    }

    printf("%d sentences\n", sn);

    double L = (100.0 * chr / st);
    double S = (100.0 * sn / st);
    double grade = 0.0588 * L - 0.296 * S - 15.8;

    if (grade <= 1)
    {
        printf("Before Grade 1\n");
    }
    else if (grade < 16)
    {
        printf("Grade %f\n", round(grade));
    }
    else
    {
        printf("Grade 16+\n");
    }
}

我不知道为什么成绩公式会产生奇怪的结果。

例如,

样本输入:

There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy. 

样本输出:

92 letters 
17 words 
1 sentences
Grade 14 

字母、单词和句子并不奇怪——它们是意料之中的。 14级很奇怪,因为我期待9级。我无法识别模式,只是特征奇怪可以描述为Grade的输出总是大于预期值,对于预期高于1级的值。

to continue, 
-Expected Grade 10 shows output Grade 16
-Expected Grade 8 shows output Grade 15
-Expected Grade 7 shows output Grade 14
-Expected Grade 5 shows output Grade 11
-Expected Grade 3 shows output Grade 10
-Expected Grade 2 shows output Grade 9

预期在 1 年级之前的文本会产生预期的输出。

成绩公式:

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

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

总的来说,我不知道为什么成绩公式没有产生预期的输出。

【问题讨论】:

  • 我们应该如何知道哪些结果是奇怪,哪些是正确的?您能否描述一下您通过什么输入获得的结果以及您期望的结果?
  • 请提供示例输入、示例输出以及您认为奇怪的解释。
  • 我会对您的代码提出建议。当您编写数学问题时,鼓励无偿使用括号。它通过将公式化的部分分解为可理解的部分来帮助澄清代码,不花费任何成本,并且有时可以为您尝试做的事情提供有用的洞察力。
  • 如果你关心“字母、单词和句子”,你为什么关心换行符?例如,你为什么使用 fgets?如果您一次只阅读一个字符,您可能会大大简化事情。
  • 我使用 fgets 是因为编译器说使用 puts 很危险

标签: c cs50


【解决方案1】:

我想,你对字母的看法是错误的。在您的input 中有一些空格字符、逗号字符等。因此您需要准确计算这句话中的字母数量(a、b、c、d、e、f 等)。

可以使用函数isalpha计算字母个数:

    for( i = 0; str[i] != '\0' ; i++)
    {
        if(isalpha(str[i]))
            chr = chr + 1;
    }

然后其他for循环计算单词和句子的数量,使用strlen(str)而不是chr - 1

    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] == ' ')
        {
            st = st + 1;
        }
    }

    printf("%d words\n", st);

    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] == '.' || str[i] == '!' || str[i] == '?')
        {
            sn = sn + 1;
        }
    }

当我测试时,我得到了结果:

Text: There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy.
72 letters
16 words
1 sentences
Grade 9.000000

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2020-05-25
    • 2022-10-16
    • 2020-12-03
    • 1970-01-01
    • 2014-07-31
    • 2021-03-06
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多