Home > Mobile >  Is there any function that avoids needing to print specific coefficient
Is there any function that avoids needing to print specific coefficient

Time:12-16

I'm trying to print a quadratic equation.
But if one of the coefficients is zero, then avoid print it and if one of them is 1, also print it without the number one.
I started to do it with if else but there are too much combinations.

double a, b, c;
printf("The quadratic equation is\n");

if (a == 0 && b != 0 && c != 0)
    printf("%.2lfX   %.2lf = 0\n", b, c);
else if (b == 0 && a != 0 && c != 0)
    printf("%.2lfX^2   %.2lf = 0\n", a, c);
else if (c == 0 && a != 0 && b != 0)
    printf("%.2lfX^2   %.2lfX = 0\n", a, b);
else if (a == 0 && b == 0 && c !=0)
    printf("%.2lf = 0\n", c);
else if (b == 0 && c == 0 && a != 0)
    printf("%.2lfX^2 = 0\n", a);
else if (a == 0 && c == 0 && b != 0)
    printf("%.2lfX = 0\n", b);
else if (a == 0 && b == 0 && c == 0)
    printf("");
else if (a == 1 && b != 1 && c != 1) {
    printf("X^2   %.2lfX   %.2lf = 0\n", b, c);
}
else if (b == 1 && a != 1 && c != 1) {
    printf("%.2lfX^2   X   %.2lf = 0\n", a, c);
}
else if (c == 1 && a != 1 && b != 1) {
    printf("%.2lfX^2   %.2lfX   1 = 0\n", a, b);
}
else if (a == 1 && b == 1 && c != 1) {
    printf("X^2   X   %.2lf = 0\n", c);
}
else if (b == 1 && c == 1 && a != 1) {
    printf("%.2lfX^2   X   1 = 0\n", a);
}
else if (a == 1 && c == 1 && b != 1) {
    printf("X^2   %.2lfX   1 = 0\n", b);
}
else if (a == 1 && b == 1 && c == 1) {
    printf("X^2   X   1 = 0\n");
}
else {
    printf("%.2lfX^2   %.2lfX   %.2lf = 0\n", a, b, c);
}

There must be a better way for doing it.
Now, I have to handle with cases like one of them is 0 and one of them is 1.

Please help me.
I'm trying to do it shorter.

CodePudding user response:

You print each term in multiple parts with multiple printf() calls each. Make this into a function and you can easily extend it for larger equations.

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool printTerm(double coeff, int degree, bool isFirst) {
    // If this coefficient is 0, print nothing
    if (coeff == 0 && degree != 0) {
        return true;
    }

    // Handle minus sign
    if (coeff < 0) {
        printf(" - ");
    }
    // Print " " only if some previous terms were printed
    else if (!isFirst) {
        printf("   ");  
    }
    
    // Print the coefficient
    if (coeff != 1 || degree == 0) {
        // Minus sign was already printed, so fabs() is needed here
        printf("%.2lf", fabs(coeff));
    }
    
    // Print "X" and the degree
    if (degree >= 1) {
        printf("X");
        if (degree > 1) {
            printf("^%d", degree);
        }
    }
    
    // Return false if something was printed
    return false;
}

int main() {
    double a = 4.0, b = 1.0, c = 0.0;

    // Once printTerm returns false (something was printed), isFirst is set false
    bool isFirst = true;
    isFirst &= printTerm(a, 2, isFirst);
    isFirst &= printTerm(b, 1, isFirst);
    isFirst &= printTerm(c, 0, isFirst);
    printf(" = 0\n");
}

CodePudding user response:

You can print the terms separately after each other. Be careful when comparing floating point numbers. As mentioned in the comments, some floating point numbers are represented exactly for example 0.0 and 1.0 but others are not (so the comparison a == 1.3 will not work as one might expect).

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

double a = 1.0, b = 1.0, c = 1.0;

bool print_term(char *t, double coeff, bool needsign) {

    if (coeff != 0.0) {
        if (needsign && coeff > 0.0) {
            printf(" ");
        }
        if (coeff != 1.0 || strcmp("", t) == 0) // also print c if it is 1.0
            printf("%.2lf%s", coeff, t);
        else
            printf("%s", t);
    }
    return coeff != 0.0;
}

int main() {
    bool need_operator = false;

    printf("The quadratic equation is\n");
    need_operator |= print_term("X^2", a, need_operator);
    need_operator |= print_term("X", b, need_operator);
    need_operator |= print_term("", c, need_operator);
    printf("\n");
    return 0;
}

Code tested with onlinegdb.com

  • Related