'Using a Fraction Class to read, manipulate, perform calculations and output to console

The program I'm writing is supposed to read the following values from a text file, then using a Fraction Class:

  • Store all elements individually
  • Return each value individually
  • Modify each element
  • Return a Fraction Object in its simplest form
  • Store each fraction in a vector
  • Output each fraction in Initial, Simplest, Mixed, Real forms to console
  • Perform operations on the fractions

FractionList.txt

10 4
20 3
-21 7
-99 22
32 4
-34 10
4 6
5 3

I believe I've done it using the code below, though there seems to be some issues when attempting to simplify the fractions that have had calculations performed on them where the simplified value isn't being returned, and there is still some aspects that are quite tedious and should be able to be replaced with loops.

I'd like to replace the following with the function DisplayInfo, though haven't been able to do so successfully as the simplify function doesn't seem to function when passed through to it.

List[i].DisplayInitial();
        ReduceFraction(List[i]).DisplayReduced();
        ReduceFraction(List[i]).DisplayMixed();
        List[i].DisplayReal();
        cout << endl;
    }

There should also be a more straightforward way using a loop to read the specific values from the vector and write them to individual Fraction objects under the ResultingFraction function.

Would it also be possible to merge the FractionList class into Fraction?

Any help with the code is much appreciated. I've tried many solutions but they don't seem to work as expected. I'm also unsure as to whether this would be the most efficient data structure to use.

main.cpp

#include <vector>
#include <iostream>
#include <fstream>

#include "Fraction.h"

using namespace std;

int main()
{
    FractionList MyFraction;
    Fraction Frac;
    unsigned int NbFraction;

    NbFraction = MyFraction.GetListFromFile("FractionList.txt");
    cout << NbFraction << " Fractions Loaded." << endl; 

    MyFraction.DisplayList();

    system("pause");
    return 0;
}

Fraction.h

#ifndef Fraction_H
#define Fraction_H

#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

class Fraction {
    int num;
    int den;

public:

    Fraction();

    // Accessors

    int GetNum() const { return num; };
    int GetDen() const { return den; };

    // Mutator

    void SetNum(int n) { num = n; };
    void SetDen(int d) { den = d; };
    
    Fraction ReduceFraction(Fraction f);
    
    // Display Member Functions

    void DisplayInitial();
    void DisplayReduced();
    void DisplayMixed();
    void DisplayReal();
    void DisplayInfo();

    // Overloaded Member Operators


    Fraction operator+ (const Fraction& f) const {
        Fraction r;
        r.num = (num * f.den) + (f.num * den);
        r.den = (den * f.den);
        return r;
    }

    Fraction operator- (const Fraction& f) const {
        Fraction r;
        r.num = (num * f.den) - (f.num * den);
        r.den = (den * f.den);
        return r;
    }

    Fraction operator* (const Fraction& f) const {
        Fraction r;
        r.num = num * f.num;
        r.den = den * f.den;
        return r;
    }

    Fraction operator/ (const Fraction& f) const {
        Fraction r;
        if (!f.num) return *this;
        r.num = num * f.den;
        r.den = den * f.num;
        return r;
    }

    Fraction operator- () {
        Fraction r;
        r.num = -num;
        r.den = den;
        return r;
    }

    Fraction& operator+= (const Fraction& f) {
        num = num * f.den + f.num * den;
        den *= f.den;
        return *this;
    }
};

class FractionList {
    vector<Fraction> List;
    
public:
    void DisplayList();

    void SumAll();
    void ResultingFraction();

    unsigned int  GetListFromFile(string FileName);

};

#endif#pragma once

Fraction.cpp

#include "Fraction.h"

using namespace std;

Fraction::Fraction() {
    num = 0;
    den = 1;
}

Fraction ReduceFraction(Fraction f) {
    int num = f.GetNum();
    int den = f.GetDen();

    Fraction Reduced;

    for (int i = den * abs(num); i > 1; i--) {
        if ((num % i == 0) && (den % i == 0)) {
            num /= i;
            den /= i;
        }
    }

    Reduced.SetNum(num);
    Reduced.SetDen(den);

    return Reduced;
}

void Fraction::DisplayInitial() {

    cout << "Initial: " << num << "/" << den << endl;
}

void Fraction::DisplayReduced() {

    if (den == 1) {
        cout << "Reduced: " << num << endl;
    }

    else {
        cout << "Reduced: " << num << "/" << den << endl;
    }
}

void Fraction::DisplayMixed() {
    int rem, remnum;

    rem = num % den;
    remnum = (num - rem) / den;

    if (remnum == 0) {
        cout << "Mixed: " << num << "/" << den << endl;
    }
    else if (rem < 0 || rem > 0) {
        cout << "Mixed: " << remnum << " " << abs(rem) << "/" << den << endl;
    }
    else if (rem == 0) {
        cout << "Mixed: " << remnum << endl;
    }

}

void Fraction::DisplayReal() {

    double realfraction = double(num) / double(den);
    cout << "Real: " << realfraction << endl;

}
void Fraction::DisplayInfo() {
    DisplayInitial();
    DisplayReduced();
    DisplayMixed();
    DisplayReal();  
 }

void FractionList::DisplayList() {
    cout << List.size() << " Fractions: " << endl << endl;
    for (unsigned int i = 0; i < List.size(); i++) {
        List[i].DisplayInitial();
        ReduceFraction(List[i]).DisplayReduced();
        ReduceFraction(List[i]).DisplayMixed();
        List[i].DisplayReal();
        cout << endl;
    }
    SumAll();
    ResultingFraction();
}

void FractionList::SumAll() {
    Fraction SumAll;

    for (unsigned int i = 0; i < List.size(); i++) {
        SumAll += List[i];
    }

    cout << "The Sum of All Fractions is: " << endl;

    SumAll.DisplayInitial();
    ReduceFraction(SumAll).DisplayReduced();
    ReduceFraction(SumAll).DisplayMixed();
    SumAll.DisplayReal();
    cout << endl;
}

void FractionList::ResultingFraction() {
    Fraction F1, F2, F3, F4, F5, F6, Result;

    F1.SetNum(List[0].GetNum());
    F1.SetDen(List[0].GetDen());
    F2.SetNum(List[1].GetNum());
    F2.SetDen(List[1].GetDen());
    F3.SetNum(List[2].GetNum());
    F3.SetDen(List[2].GetDen());
    F4.SetNum(List[3].GetNum());
    F4.SetDen(List[3].GetDen());
    F5.SetNum(List[4].GetNum());
    F5.SetDen(List[4].GetDen());
    F6.SetNum(List[5].GetNum());
    F6.SetDen(List[5].GetDen());

    Result = -F6 - ((F1 + F2) * F3 - F4) / F5;
    
    cout << "The Result using Equation [ResultingFraction= -F6-((F1 + F2)*F3-F4)/F5] is: " << endl;

    Result.DisplayInitial();
    ReduceFraction(Result).DisplayReduced();
    ReduceFraction(Result).DisplayMixed();
    Result.DisplayReal();
    cout << endl;
    
}

unsigned int FractionList::GetListFromFile(string FileName) {
    ifstream FractionFile(FileName);
    if (!FractionFile.fail()) {
        int num, den;
        Fraction Fraction;

        while (FractionFile >> num) {
            FractionFile >> den;
            Fraction.SetNum(num);
            Fraction.SetDen(den);
            List.push_back(Fraction);
        }
    }
    return List.size();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source