Pwestm
Pwestm

Reputation: 13

Displaying an equation in C++

I’m writing a formula to solve for the roots of a quadratic in C++. My output should begin with the equation such as

3x^2 +2x -3

Everything in my program is correct except for this part. My output has all plus signs followed by the coefficient. How can I get it to display minus (-) signs instead of + when a coefficient is negative? Any help is appreciated thanks!

Example code:

std::cout << a << “x^2” << “+” << b<< “x”

If b is negative it prints ax^2 + -3x but I need it to display ax^2 - 3x

Upvotes: 0

Views: 1642

Answers (2)

charliepu123
charliepu123

Reputation: 59

std::cout << a << "x^2" << b >= 0? "+" : "" << b<< "x";

Use ternary operator to make sure "+" is only there when b is not negative.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882028

Your problem with something like:

std::cout << a << "x^2" << "+" << b << "x";

is that you are always outputting a + followed by the b value, be it positive or negative (or zero for that matter). Consider a = 7, b = -42:

7x^2+-42x

As you've seen, these won't look nice for negative numbers. You could get rid of the + but then it would look bad for positive numbers. Consider a = 7, b = 42:

7x^242x

The following code shows a slightly different approach, where the coefficients are checked for sign and then the output is adjusted properly. It also handles arbitrary powers, though only down to zero, allows you to decide whether to print zero coefficients or not, and spaces the terms nicely:

#include <iostream>
#include <vector>

void outputEquation(const std::vector<int> &vec, bool showZeros = false) {
    // Power counts down, first is to change
    // formatting of first term.

    auto power = vec.size() - 1;
    auto first = true;

    // Handle each coefficient.

    for (const auto coeff: vec) {
        // Select whether zero coefficients are shown.

        if (coeff != 0 || showZeros) {
            // Leading space on all but first term.

            if (! first) std::cout << " ";

            // Intelligently handle negative/non-negative.

            if (coeff >= 0) {
                // No leading '+' on first term,
                // only print coefficient if not 1.

                if (! first) std::cout << "+ ";
                if (coeff != 1) std::cout << coeff;
            } else {
                // Output sign for negative, with space after
                // if not first term. Then output |coeff|
                // unless 1.

                std::cout << "-";
                if (! first) std::cout << " ";
                if (coeff != -1) std::cout << -coeff;
            }

            // Output power, taking into account "x^1"
            // and "x^0", which become "x" and "".

            if (power > 1) {
                std::cout << "x^" << power;
            } else if (power == 1) {
                std::cout << "x";
            }

            // First term done, adjust future behaviour.

            first = false;
        }

        // Decrease power for next element.

        --power;
    }

    // If no terms were output, just give 0.

    if (first) {
        std::cout << '0';
    }
    std::cout << '\n';
}

void outputEquation(int a, int b, int c, bool showZeros = false) {
    // Just make a vector and call function above.

    outputEquation({a, b, c}, showZeros);
}

// Test harness, adapt as necessary.

int main() {
    outputEquation({ -4, 3, -2, 1, -1, 0, 4 });
    outputEquation({ });
    outputEquation({ 4 });
    outputEquation({ 0 });
    outputEquation({ 0, 1, 2 });
    outputEquation({ 0, 0, 0, 0, 99, 0, 0, 0, 0 });
    outputEquation({ 0, 0, 0, 0, 99, 0, 0, 0, 0 }, true);
    outputEquation(1, 2, 3);
}

The first overloaded function is a general purpose one which you can augment with the second overload, meant specifically for quadratics. This outputs:

-4x^6 + 3x^5 - 2x^4 + x^3 - x^2 + 4
0
4
0
x + 2
9x^4
0x^8 + 0x^7 + 0x^6 + 0x^5 + 99x^4 + 0x^3 + 0x^2 + 0x + 0
x^2 + 2x + 3

but you can adjust the parameters for other possibilities.

Upvotes: 0

Related Questions