these are the instructions for the program.

Prepare the Fraction class as we discussed in the classroom. It should include three constructors -- one with no parameters and which creates the fraction 0/1, one with one parameter numer and which creates the fraction numer/1, and one with two parameters and which creates the fraction numer/denom, but which assures that the fraction will be in normalized form (that is, positive denominator and with the greatest common divisor removed from the numerator and denominator). Make sure that the store method also stores fractions in normalized form. Add two public methods (functions) to the class, called getNumerator and getDenominator, that return the values of the numerator and denominator of the fraction. The header file and implementation files should be separate files!!

Write a main program to test the fraction class. This main program should first create three fractions to test the three types of constructors. Have the fractions print themselves, but precede each with an explanatory remark so that a fraction doesn't appear by itself. Then prompt the user to enter a numerator and a denominator. Store that fraction in the fraction that you first created with no parameters. Have it print itself. Ask the user if he/she wants to enter another fraction. If N or n is answered, then quit this part of the program and go on to the final step (below). If Y or y is entered, then ask for another fraction, store it and print it. Repeat until the user answers N or n, and then move to the final step. (If the user enters a zero denominator, the constructor and the store method should detect this and take an exit from the program.) As a final step, create the three fractions fract1, fract2 and fract3, where fract1 contains 5/6, fract2 contains 7/2 and fract3 is just the default fraction. Use the getNumerator and getDenominator functions to obtain the numerators and denominators of fract1 and fract2, and store in fract3 the sum of fract1 and fract2. Note: a/b + c/d = (ad + cb) / (b*d). Then have fract3 print itself to make sure the correct result was stored.

[C++]
Main Program


#include <iostream>
#include "Fraction.hpp"
using namespace std;


int main()
{


Fraction fract4(5,-10);
cout<<"The Fraction Is ";
fract4.print();
Fraction fract6(8,4);
cout<<"The Fraction Is ";
fract6.print();
system("pause");
return 0;
}


Header File


#include <iostream>
using namespace std;


class Fraction {
private:
int numerator;
int denominator;
int GreatestComDiv(int n1, int n2);
public:
Fraction();
Fraction(int numer);
Fraction(int numer, int denom);
void store(int numer, int denom);
void print();
};//Fraction


implementation file


#include "Fraction.hpp"
#include <iostream>
#include <cstdlib>


Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}//Fraction::Fraction()


Fraction::Fraction(int numer)
{
numerator = numer;
denominator = 1;
}//Fraction::Fraction(int numer)


Fraction::Fraction(int numer, int denom)
{
if(denom == 0)
{
cout << "Can not Divide By 0!" << endl;
exit(100);
}
if(denom < 0)
{
denom = -denom;
numer = -numer;
}
int gcd = GreatestComDiv (abs(numer), abs(denom));
numer = numer/gcd;
denom = denom/gcd;


store(numer, denom);


}//Fraction::Fraction(int numer, int denom)


void Fraction::store(int numer, int denom)
{
numerator = numer;
denominator = denom;
return;
}//Fraction::store


void Fraction::print()
{
cout<< numerator << "/" << denominator << "." << endl;
return;
}//Fraction::print


int Fraction::GreatestComDiv(int numer, int denom)
{
if (numer < denom)
{
int temp = numer;
numer = denom;
denom = temp;
}
if  (denom == 0)
return numer;
else
return GreatestComDiv(denom, numer % denom);
}
[/C++]

could anybody help me out with what you see im missing. i am getting overwhelmed and maybe with a few good pointers i can get set right again thanks

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Next time u ask a question don't just post ur kode and say help me with the missing parts? Pin point an exact problem. Then we mite help u?

Thanks for trying to use code tags. Replace C++ within the brackets with the word code and it should work better next time.

From the point of view of the C++ language, things look pretty good. You don't need a return statement if a method/function has return type void.

From a practical point of view the store() method as written doesn't normalize the fraction. Per the instructions store() is a changeNumeratorAndDenominator() function and acts as the counterpart to the separate getNumerator() and getDenominator() functions. There is no reason to call it in the two parameter constructor.

From a mathematical point of view, the protocol you used for finding the greatest common denominator seems a little off, but it's been a while since I've written a GCD function, so if it works out for you, fine.

In main() I don't see where you use the default constructor or the store() function and I don't see a getNumerator(), getDenominator() function or your attempt to use them to add two fractions as instructed, yet, but maybe they are works in progress.

Use kode tags :eek: then we mite help u. Next time u ask a question don't just post ur kode and say help me with the missing parts? Pin point an exact problem. Then we mite help u?

If you're going to harass newbies about the way they form their posts, you could at least have the sense of hypocrisy to write yours decently.

Member Avatar for iamthwee

If you're going to harass newbies about the way they form their posts, you could at least have the sense of hypocrisy to write yours decently.

Wasn't harassing. Sorrie :o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.