CS50可读性指数计算关闭

qco9c6ql  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(115)

我试图找到Colemen-Liau基于数字或字母、单词和句子的阅读索引。当我插入示例文本时,如果字母、单词和句子计数正确,但索引总是返回-2147483648。帮助!

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

int count_letters(string input);
int count_words(string input);
int count_sentences(string input);

int letters =0;
int words = 0;
int sentences = 0;

int main(void)//
{
    // user prompted for input
    string input = get_string("Input: "); //prompt user to input text

    float L = 100.0 * (letters/words);
    float S = 100.0 * (sentences/words);
    int index = round(0.0588 * L - 0.296 * S - 15.8);

    letters = count_letters(input);
    words = count_words(input);
    sentences = count_sentences(input);

    printf("Letters: %i\n", letters);
    printf("Words: %i\n", words);
    printf("Sentences: %i\n", sentences);
    printf("Index: %i\n", index);
}

我希望看到返回的索引为8。

hmae6n7t

hmae6n7t1#

台词

letters = count_letters(input);
words = count_words(input);
sentences = count_sentences(input);

必须在这些行之前执行:

float L = 100.0 * (letters/words);
float S = 100.0 * (sentences/words);
int index = round(0.0588 * L - 0.296 * S - 15.8);

因此,我建议您在程序中交换这两组行的顺序。
如果没有这种交换,线路

float L = 100.0 * (letters/words);
float S = 100.0 * (sentences/words);

没有意义,因为当执行这些行时,变量letterswordssentences的值都是0,除以0将调用undefined behavior

相关问题