Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Under Fraction.cpp Where you have printFraction, I want you to write a function called getFraction that will return a string. String is going to be

Under Fraction.cpp

Where you have printFraction, I want you to write a function called getFraction that will return a string. String is going to be your return.

Under Main.cpp

Create two functions, which are read fraction and write fraction. Write to the file and randomly generate a numerator and denominator.

My code

Fraction.cpp

#include

#include

#include "Fraction.h"

using namespace std;

Fraction::Fraction()

{

this->setFraction(1, 1);

}

Fraction::Fraction(int num, int den)

{

this->setFraction(num, den);

}

void Fraction::setFraction(int n, int d)

{

this->num = n;

this->den = d;

}

Fraction Fraction::add(const Fraction &f)

{

Fraction tmp;

tmp.num = (this->num*f.den) + (f.num*this->den);

tmp.den = f.den*den;

return tmp;

}

Fraction Fraction::sub(const Fraction &f)

{

Fraction tmp;

tmp.num = (this->num*f.den) - (f.num*f.den);

tmp.den = f.den*this->den;

return tmp;

}

Fraction Fraction::mul(const Fraction &f)

{

Fraction tmp;

tmp.num = this->num*f.num;

tmp.den = this->den*f.den;

return tmp;

}

Fraction Fraction::div(const Fraction &f)

{

Fraction tmp;

tmp.num = this->num*f.den;

tmp.den = this->den*f.num;

if (tmp.den < 0)

{

tmp.num *= -1;

tmp.den *= -1;

}

return tmp;

}

void Fraction::printFraction()

{

cout << this->num << "/" << this->den << endl;

}

int Fraction::getDen() const

{

return this->den;

}

int Fraction::getNum() const

{

return this->num;

}

Fraction Fraction::operator+(const Fraction &f)

{

return this->add(f);

}

Fraction Fraction::operator- (const Fraction &f)

{

return this->sub(f);

}

Fraction Fraction::operator*(const Fraction &f)

{

return this->mul(f);

}

Fraction Fraction::operator/(const Fraction&f)

{

return this->div(f);

}

Fraction &Fraction::operator=(const Fraction &f)

{

this->setFraction(f.getNum(), f.getDen());

return*this;

}

ostream & operator<<(ostream & output, const Fraction & f)

{

output << "num: " << endl;

output << "den: "<< endl;

return output;

}

istream &operator>> (istream &input, Fraction &f)

{

cout << "Enter a Numerator" << endl;

input >> f.num;

cout << "Enter a Denominator" << endl;

input >> f.den;

return input;

}

main.cpp

#include

#include

#include "Fraction.h"

using namespace std;

void writeNumbers(int amount);

void readNumbers();

int main()

{

//writeNumbers(10);

readNumbers();

return 0;

}

void writeNumbers(int amount)

{

ofstream outfile("Numbers.txt");

if (!outfile)

{

cout << "unable to open the file for writing" << endl;

exit(1);

}

for (int i = 0; i < amount; i++)

{

outfile << rand() % 100 << endl;

}

outfile.close();

}

void readNumbers()

{

ifstream infile("Numbers.txt");

if (!infile)

{

cout << "Unable to open the file for reading"

return;

}

char input[100];

while (infile >> input)

cout << input << endl;

infile.close();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

Analyse the various techniques of training and learning.

Answered: 1 week ago