GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jan-2006, 15:58
fevershark fevershark is offline
Junior Member
 
Join Date: Dec 2005
Posts: 32
fevershark is an unknown quantity at this point

fraction class 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 = (a*d + c*b) / (b*d). Then have fract3 print itself to make sure the correct result was stored.

CPP / C++ / C Code:
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);
 }   

could anybody help me out with what you see im missing. i am getting overwhelmed and maybe with a few godd pointers ic an get set right again thanks
  #2  
Old 30-Jan-2006, 20:00
fevershark fevershark is offline
Junior Member
 
Join Date: Dec 2005
Posts: 32
fevershark is an unknown quantity at this point

Re: fraction class program


please can somebody help me with this i am really confused in what to do next can somebody point me in the right direction? thanks
  #3  
Old 30-Jan-2006, 21:30
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: fraction class program


Please read the guidelines.

#2.2 to 2.4 describes what you did NOT do. We don't know what to tell you.
#7 is self explanitory.
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
  #4  
Old 30-Jan-2006, 21:43
fevershark fevershark is offline
Junior Member
 
Join Date: Dec 2005
Posts: 32
fevershark is an unknown quantity at this point

Re: fraction class program


Quote:
Originally Posted by Guidelines Plz
Please read the guidelines.

#2.2 to 2.4 describes what you did NOT do. We don't know what to tell you.
#7 is self explanitory.

it compiles fine i need help trying to figure out what i am lacking from what the instructions say to do. sorry i didnt make that clearer.
  #5  
Old 30-Jan-2006, 22:04
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: fraction class program


Quote:
Originally Posted by fevershark
it compiles fine i need help trying to figure out what i am lacking from what the instructions say to do. sorry i didnt make that clearer.
  • Run the program.
  • Look at the output.
  • Compare it to the instructions.
  • Cross out everything that is in the instructions that is also in the output.
  • What's left in the instructions is what's missing.

Can't really do more than that. If you've read the Guidelines you know we'll help you fix coding errors, help you understand something you don't, but we usually don't analyze code just as a exercise. If you have specific questions, we can definitely help in that regard.
__________________

Age is unimportant -- except in cheese
  #6  
Old 30-Jan-2006, 22:24
fevershark fevershark is offline
Junior Member
 
Join Date: Dec 2005
Posts: 32
fevershark is an unknown quantity at this point

Re: fraction class program


ok well heres the question i am having trouble with right now. i need to add to public functions in the header file named getnumerator and getdenominator, but how would i write it out in the implementation file to work with the whole program is what i am having problems with. thanks
  #7  
Old 31-Jan-2006, 05:03
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: fraction class program


Quote:
Originally Posted by fevershark
ok well heres the question i am having trouble with right now. i need to add to public functions in the header file named getnumerator and getdenominator, but how would i write it out in the implementation file to work with the whole program is what i am having problems with. thanks
The same way as you did for other functions.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 31-Jan-2006, 11:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: fraction class program


Quote:
Originally Posted by fevershark
ok well heres the question i am having trouble with right now. i need to add to public functions in the header file named getnumerator and getdenominator, but how would i write it out in the implementation file to work with the whole program is what i am having problems with. thanks
Are the Fraction() and store() functions similar to what you are trying to add? To they access the values you need to access in you new functions?
If so, study them to see how they did it and use similar coding techniques.
If not, you need to ask a more detailed question.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fraction program SpudNuts C++ Forum 6 25-Sep-2006 03:50
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.