pset2 readability.c

发布于:2024-12-06 ⋅ 阅读:(80) ⋅ 点赞:(0)
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
float calculate_letter(string text);
float calculate_word(string text);
float calculate_sentance(string text);
void comfirm_grade(float letters, float words, float sentances);
int count = 0;
int main(void)
{
    // prompt user for texts
    string text = get_string("Text: ");
    // count the number of leters,words,and sentences
    float letters = calculate_letter(text);
    float words = calculate_word(text);
    float sentances = calculate_sentance(text);
    // compute the Coleman-Liau index
    comfirm_grade(letters, words, sentances);
    // print the grade level
    return 0;
}
float calculate_letter(string text)
{
    size_t length = strlen(text);
    for (int y = 0; y < length; y++)
    {

        if (isalpha(text[y]))
        {
            count++;
        }
    }
    float letter = (float) count;
    return letter;
}
float calculate_word(string text)
{
    count = 0;
    size_t length = strlen(text);
    for (int i = 0; i < length; i++)
    {
        if (text[i] == ' ')
        {
            count++;
        }
    }
    float word = (float) (count + 1);
    return word;
}
float calculate_sentance(string text)
{
    count = 0;
    size_t length = strlen(text);
    for (int j = 0; j < length; j++)
    {
        if (text[j] == '.' || text[j] == '?' || text[j] == '!')
        {
            count++;
        }
    }
    float sentance = (float) count;
    return sentance;
}
void comfirm_grade(float letters, float words, float sentances)
{
    double L = (letters / words) * 100;
    double S = (sentances / words) * 100;
    double index = 0.0588 * L - 0.296 * S - 15.8;
    int round_index = round(index);
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index >= 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %d\n", round_index);
    }
}

check:

:) readability.c exists
:) readability.c compiles
:) handles single sentence with multiple words
:) handles punctuation within a single sentence
:) handles more complex single sentence
:) handles multiple sentences
:) handles multiple more complex sentences
:) handles longer passages
:) handles questions in passage
:) handles reading level before Grade 1
:) handles reading level at Grade 16+

网站公告

今日签到

点亮在社区的每一天
去签到