jeudi 23 avril 2020

Overloading istream operator to show fractions in multi line project

Hello Stackoverflow this is indeed my first post ever on this forum. I needed some guidance on my c++ program, I am in a situation where I do not know where to start to make my normal and mixed fractions display from a file. Here is what I have so far for the header and implementation file. The Main Focus is The iStream Function in Fraction.cpp to be able to display mixed and normal fractions from a file. Any help will be appreciated!

Here is my Fraction.cpp code

/* USED FOR FUNCTIONS */
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include "fraction.h"
using namespace std;

namespace cs10b_fraction
{


ostream& operator << (std::ostream &out, const Fraction printMe)
{

    if (printMe.denom == 1)
    {
        out << printMe.num << endl;
    }

    else if (printMe.num == 0)
    {
        out << printMe.num << endl;
    }

    else if(printMe.denom == -1)
        out << printMe.num / printMe.denom << endl;

    else if(printMe.num < printMe.denom && printMe.num != 0)
        out << printMe.num << "/" << printMe.denom << endl;

    else if(abs(printMe.num) > abs(printMe.denom))
        out << printMe.num / printMe.denom << "+" << (printMe.num % printMe.denom) << "/" << printMe.denom << endl;



   /* else if (abs(printMe.num) > abs(printMe.denom))
        out << printMe.num / printMe.denom << endl;

    else if (abs(printMe.num) > abs(printMe.denom))
        out << printMe.num / printMe.denom << "+" << (printMe.num % printMe.denom) << "/" << printMe.denom << endl; */

    return out;
}

istream& operator >> (std::istream &in, Fraction readMe)
{
   int temp;
   in >> temp;



    if (in.peek() == '+')
    {

    }

    else if (in.peek() == '/')
    {

    }

    else
    {

    }

    return in;

}

}

Here is my Fraction.h code

/*USED FOR CLASS WITH FUNCTIONS ETC */
#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
using namespace std;

namespace cs10b_fraction
{
class Fraction
{
public:
    Fraction(int n = 0, int d = 1)
    {
        num = n;
        denom = d;
        assert(denom != 0);
        Simplify();
    }

friend ostream& operator << (std::ostream &out, const Fraction printMe);
friend std::istream& operator >> (std::istream &in, Fraction readMe);

private:
    int num;
    int denom;

    void Simplify();
};
}

#endif

Aucun commentaire:

Enregistrer un commentaire