Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3026

adding rational number using only class functions

$
0
0
Hi, i'm having trouble finishing this program. What I’m trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I’m not supposed to use accessor functions. The part that I can’t seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can’t seem to know how to implement it to the program. The main program is still empty but all I’m supposed to do there is call the class functions, which my because of my lake of experience I can’t seem to figure out where to start.


Code:

#include <iostream>

using namespace std;

class Rational
{
private:
       
        int numerator, denominator;
        char sign;
       
public:

        Rational(const int denominator = 1, const char sign = '+');  // constructor.  Will set beginning balance to zero
        int setnumerator(const int numerator);
        int setdenominator(const int denominator);
        char setsign(const int numerator, const int denominator, const char sign);
        void PrintFraction();
        void readint();
        Rational addUp(Rational first, Rational second);

};




int main()
{

       



        system ("PAUSE");
        return 0;
}





Rational::Rational(const int denominator, const char sign)  // the constructor
{

}

int Rational::setnumerator(const int numerator)
{

        if (numerator<0)
                {
                        int numer = numerator * -1;
                        return numer;
                }
        else
                {
                        int numer = numerator;
                        return numer;
                }

}

int Rational::setdenominator(const int denominator)
{
       
                if (denominator<0)
                        {
                                int denom = denominator * -1;
                                return denom;
                        }
                else
                        {
                                int denom = denominator;
                                return denom;
                        }

}

char Rational::setsign(const int numerator, const int denominator, const char sign)
{

        if (denominator>0 && numerator>0 || denominator<0 && numerator<0)
                {

                        char fianlsign = '+';
                        return fianlsign;
                }
        char fianlsign = '-';
        return fianlsign;

}

void Rational::PrintFraction()
{
        if (denominator!=1)
                {
                        cout<<sign<<numerator<<"/"<<denominator;
                }
        cout<<sign<<numerator;
}

void Rational::readint()
{
       
        cout<<"enter numerator: ";
        cin>>numerator;
        cout<<"enter denominator: ";
        cin>>denominator;

        return;

}

Rational addUp(Rational first, Rational second)
{

}


Viewing all articles
Browse latest Browse all 3026

Trending Articles