Home > OS >  C Fraction Display Program
C Fraction Display Program

Time:12-10

Quick question here, I am writing a program to display a fraction after a numerator and denominator are entered, and display a mixed fraction if the numerator is greater than the denominator. The program seems to be running fine except for when I put the same integer for the numerator and denominator, it always displays a negative number, even if they are both positive.

At this part of the code,

if (abs(numerator) == abs(denominator))
{
    if (numerator || denominator < 0)
        cout << "-" << abs(numerator);
    else
        cout << abs(numerator);
}

the else statement is not executing even if neither integer is less than 0. It always displays the negative sign. Here is the whole code, I have tried debugging and both numerator and denominator stay positive but it still executes like one of them is less than 0. Any input is greatly appreciated, thanks.

#include <iostream>
#include <cmath>

using namespace std;
int main()
{
short numerator = 0;
short denominator = 0;
short divisor = 0;
short remainder = 0;

cout << "Please enter two integers representing the numerator and denominator of a fraction: \n";

cout << "Please enter the numerator: ";
cin >> numerator;

cout << "\nPlease enter the denominator: ";
cin >> denominator;

cout << endl;

if (abs(numerator) == abs(denominator))
{
    if (numerator || denominator < 0)
        cout << "-" << abs(numerator);
    else
        cout << abs(numerator);
}

else if (denominator == 0)
{
    cout << "\nCannot divide by zero!" << endl;
}

else if (numerator && denominator > 0)
{
    if (numerator < denominator)
        cout << numerator << " / " << denominator << endl;
    else
    {
        divisor = numerator / denominator;
        remainder = numerator % denominator;
        cout << divisor << "   " << remainder << " / " << denominator << endl;
    }

}

else
    if (abs(numerator) < abs(denominator))
        cout << "-" << abs(numerator) << " / " << abs(denominator) << endl;
    else
    {
        divisor = abs(numerator / denominator);
        remainder = abs(numerator % denominator);
        cout << "-" << divisor << "   " << remainder << " / " << abs(denominator) << endl;
    }

return 0;

}

CodePudding user response:

Changed this section of code, realized the answer was just 1 or -1 not the value of the integers entered.

if (numerator || denominator < 0)
    cout << "-" << abs(numerator);
else
    cout << abs(numerator);

To this, checking to see if they are both negative first, then seeing if just either of them is.

if (numerator < 0 && denominator < 0)
   cout << "1";
else if (numerator < 0 || denominator < 0)
   cout << "-1";
else
   cout << "1";

Here is the whole new code, with 0 in the numerator added.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
short numerator = 0;
short denominator = 0;
short divisor = 0;
short remainder = 0;

cout << "Please enter two integers representing the numerator and denominator of a fraction: \n";

cout << "Please enter the numerator: ";
cin >> numerator;

cout << "\nPlease enter the denominator: ";
cin >> denominator;

cout << endl;

if (abs(numerator) == abs(denominator))
{
    if (numerator < 0 && denominator < 0)
        cout << "1";
    else if (numerator < 0 || denominator < 0)
        cout << "-1" ;
    else
        cout << "1";
}

else if (numerator == 0)
{
    cout << "\n0" << endl;
}

else if (denominator == 0)
{
    cout << "\nCannot divide by zero!" << endl;
}

else if (numerator > 0 && denominator > 0)
{
    if (numerator < denominator)
        cout << numerator << " / " << denominator << endl;
    else
    {
        divisor = numerator / denominator;
        remainder = numerator % denominator;
        if (remainder != 0)
            cout << divisor << "   " << remainder << " / " << denominator << endl;
        else
            cout << divisor << endl;
    }

}

else
    if (abs(numerator) < abs(denominator))
        cout << "-" << abs(numerator) << " / " << abs(denominator) << endl;
    else
    {
        divisor = abs(numerator / denominator);
        remainder = abs(numerator % denominator);
        if (remainder != 0)
            cout << "-" << divisor << "   " << remainder << " / " << abs(denominator) << endl;
        else
            cout << "-" << divisor << endl;
    }

return 0;

}

CodePudding user response:

The question has been answered already. Maybe the following code will give you an additional idea:

#include <iostream>
#include <numeric>
#include <vector>
#include <utility>
#include <unordered_set>

struct RFraction {
    // We will store reduced fractions. And show numerator and denominator separately 
    int numerator{};
    int denominator{};

    // Contructor will take a fraction, a nominater and denominater, reduce the fraction and mormalize the sign
    RFraction(const int n, const int d) : numerator(std::abs(n)), denominator(std::abs(d)) {
        if (n == 0) denominator = 1;    // All faction with a 0 numerator will get a 1 as denominator
        reduce();                       // Reduce fraction
        if ((n < 0 and d >= 0) or (n >= 0 and d < 0)) numerator = -numerator; // Handle sign uniformly
    }
    // Simple reduce function using standardapproach with gcd
    void reduce() { int gcd = std::gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; }

    // Hash Functor
    struct Hash { size_t operator()(const RFraction& r) const { return std::hash<int>()(r.numerator) ^ std::hash<int>()(r.denominator); } };

    // Equality, basedon fraction and not on a double
    bool operator == (const RFraction& other) const { return numerator == other.numerator and denominator == other.denominator; }

    // Simple output
    friend std::ostream& operator << (std::ostream& os, const RFraction& f) {
        if (f.denominator == 1) os << f.numerator;
        else os << f.numerator << " / " << f.denominator;
        return os;
    }
};
  • Related