mercredi 11 mars 2020

How to know check in C language if float decimal places hold values other than 0

In C language I want to check if my float has some decimal value other than 0 then I want to apply if condition on that, I have a formula with 2 ways, I want this program to go with way 1 if float decimals holds some values other than 0, and go way 2 if it has only 0s in decimals. Here is my current code:

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

int letter;
int word;
int sentence;

int main(void) {

    // prompt the user with the question
    string article = get_string("TEXT: ");

    // set the length of article
    int n = strlen(article);

    // add +1 if the article starts with alphanumeric letter
    if (isalnum(article[0])) {
        word = 1;
    }

    // count words
    for (int i = 0; i < n;  i++) {
        // count letters
        if (isalnum(article[i])) {
            letter++;
        }

        // count for words
        if (i < n - 1 && isspace(article[i]) && isalnum(article[i + 1])) {
            word++;
        }

        // count for sentences
        if (i > 0 && (article[i] == '!' || article[i] == '?' || article[i] == '.') && isalnum(article[i - 1])) {
            sentence++;
        }
    }

    // Formula Coleman's calculations:
    float L = letter / word;
    float S = sentence / word;

    //THIS IS WHERE I NEED HELP
    if (#it have some value other then "0") {
        float grade1 = 0.0588 * (100 * L) - 0.296 * (100 * S) - 15.8;
    } else
    if (# it does have only 0s in decimals) {

        float grade1 = 0.0588 * (100 * letter/word) - 0.296 * (100 * sentence/word) - 15.8;
    }

    grade1 = round(grade1);
    grade1 = truncl(grade1);
    int grade = grade1;

    // print result
    if (grade <= 1) {
        printf("Before Grade 1\n");
    } else
    if (grade < 16) {
        printf("Grade %i\n", grade);
    } else {
        printf("Grade 16+\n");
    }
}

Aucun commentaire:

Enregistrer un commentaire