Thread: Need help(Rational no. class)

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Need help(Rational no. class)

    I have to create a class of rational numbers with the functionality that it can multiply,add,subtract and divide two rational numbers and give a simplified answer!
    i am getting too many errors.
    Here is the code:

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class rational
    {
    
    int num;
    int denom;
    int normal()
    {
       int d, sign;
       sign = 1;
       if (top < 0) {
         sign = -1;
         top = -top;
    
       d = gcd(top, bottom);
       top = sign*(top / d);
       bottom = bottom / d;
    }
    
    unsigned int gcd(unsigned int n, unsigned int m) {
       if (n == 0) return m;
       if (m == 0) return n;
       while (m != n)
        {
          if (n > m) n = n - m;
          else m = m - n;
       }
       return n;
    } 
    
    
    public:
    rational()
    {
    num=0;
    denom=1;
    }
    rational (int)
    {
    cin>>num;
    denom=1;
    }
    rational (int, int)
    {
    cin>>num;
    cin>>denom;
    
    }
    
    void setNum(int)
    {
    cin>>num;
    num.normal();
    }
    
    void setDenom(int)
    {
    cin>>denom;
    denom.normal();
    }
    
    void add()
    {rational add;
    add.a=num*a.denom+a.num*denom;
    add.b=denom*a.denom;
    add.normal();
    return add;
    }
    
    void sub()
    {
    rational sub;
    sub.a=num*a.denom+a.num*denom;
    sub.b=denom*a.denom;
    sub.normal();
    return sub;
    }
    
    void mult()
    {
    mult.a=num*a.num;
    mult.b=denom*a.denom;
    mult.normal();
    return mult;
    }
    
    void div()
    {
    div.a=num*a.denom;
    div.b=a.num*denom;
    div.normal();
    return div;
    }
    };
    int main()
    {
    clrscr();
       rational x;
       rational a(3);
       rational b(3, 4);
       rational c(2, 4);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Fatima don't know what u want to do sorry to say but see my advice for u is that
    before writing a piece of code on machine u shoud write its logic with pen and paper .........
    and then try to write its sudo code there itself and then code it will be better and u can become a good programmer

    don't try to start coding while just getting the problem

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    think as many times as u can before writing the piece of code i will give u cool books of C++ if u want just gimmi a mail at [email protected]

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Hi Fatima

    I think the below code can solve ur problem

    Code:
    #include <iostream>
    
    class RationalNumber {
    private:
      int  numerator;
      int  denominator;
    
    public:
      RationalNumber() : numerator(0), denominator(0) {}
    
      RationalNumber(const int __numerator,
                     const int __denominator) : numerator(__numerator),
                                                denominator(__denominator) {}
    
      RationalNumber Add(RationalNumber __first, RationalNumber __second);
      RationalNumber Sub(RationalNumber __first, RationalNumber __second);
      RationalNumber Mul(RationalNumber __first, RationalNumber __second);
      RationalNumber Div(RationalNumber __first, RationalNumber __second);
    
      void           Print();
    };
    
    RationalNumber RationalNumber::Add(RationalNumber __first,
                                       RationalNumber __second) {
      RationalNumber result;
      result.denominator = __first.denominator * __second.denominator;
      result.numerator   = ((result.denominator / __first.denominator)  *   \
                            __first.numerator)                          +   \
                           ((result.denominator / __second.denominator) *   \
                            __second.numerator);
        return result;
    }
    
    RationalNumber RationalNumber::Sub(RationalNumber __first,
                                       RationalNumber __second) {
      RationalNumber result;
      result.denominator = __first.denominator  * __second.denominator;
      result.numerator   = ((result.denominator / __first.denominator)  *   \
                            __first.numerator)                          -   \
                           ((result.denominator / __second.denominator) *   \
                            __second.numerator);
        return result;
    }
    
    RationalNumber RationalNumber::Mul(RationalNumber __first,
                                       RationalNumber __second) {
      return RationalNumber( __first.numerator   * __second.numerator,     \
                             __first.denominator * __second.denominator);
    }
    
    RationalNumber RationalNumber::Div(RationalNumber __first,
                                       RationalNumber __second) {
      return RationalNumber(__first.numerator   * __second.denominator,    \
                            __first.denominator * __first.numerator);
    }
    
    void RationalNumber::Print() {
      std::cout << numerator << " / " << denominator << std::endl;
    }
    
    int main() {
      RationalNumber number_A(3, 5);
      RationalNumber number_B(4, 7);
    
      RationalNumber result;
    
      result = result.Add(number_A, number_B);
      result.Print();
      result = result.Sub(number_A, number_B);
      result.Print();
      result = result.Mul(number_A, number_B);
      result.Print();
      result = result.Div(number_A, number_B);
      result.Print();
    
      return 0;
    }
    Let me knw did u get that or not ??

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    To construct a class to represent rational no’s of the form p/q for e.g 5/6 ,2/3 apart from the necessary data members the class should have the methods for following functionalities:

    1. Taking the input from user and displaying the fractions.

    2. Addition of two fractions of the form p/q=a/b+c/d i.e the result should also be a rational no (for all

    operations mentioned)

    3. Subtracting two rational nos.

    4. Multiplying two rational nos.

    5. Dividing two rational nos.

    6. Simplification of rational no resulting in another simplified rational no.


    Avoid printing results of operations in functions respectively; prefer using the print function for that.

    ! Support your class with a driver program to check its functionalities.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    I am working on the above instructions,,, thank yew so much for the help but i didnt really get ur code,,=(((

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Hey Fatima tell me one thing first to me....
    did u compiled this code on ur machine if you compiled it you are not getting the result what you are acppecting
    and if you are not getting the result what you want just tell me then

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Or if you didn't understand the code what i can i do i can just simply make u understandable on chat,

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This means that you'll need to write a rational class with the following public interface:

    Code:
    class Rational
    {
    public:
    //1. Taking the input from user and displaying the fractions.
        void input();
        void output() const; //or overload operators << and >>
    
    //2. Addition of two fractions of the form p/q=a/b+c/d i.e the result should also be a rational no 
        Rational operator+(const Rational& rhv) const;
    
    //3. Subtracting two rational nos.
        Rational operator-(const Rational& rhv) const;
    
    //4. Multiplying two rational nos.
        Rational operator*(const Rational& rhv) const;
    
    //5. Dividing two rational nos.
        Rational operator/(const Rational& rhv) const;
    
    //6. Simplification of rational no resulting in another simplified rational no.
        Rational simplify() const;
    };
    Add whatever you need to implement those.

    Also your constructors are fine, but you really shouldn't be doing any input in those.

    Code:
    rational()
    {
        num=0;
        denom=1;
    }
    rational (int full_number)
    {
        num = full_number;
        denom=1;
    }
    rational (int numerator, int denominator)
    {
        num = numerator;
        denom = denominator;
    }
    I would recommend throwing away your code and starting from scratch.

    Implement one function, write a main to test it, compile and test it. Don't move on before it compiles without errors and gives correct results for output.

    I would suggest you first implement input and output, so you can give data to your fractions and view their contents.

    Then implement gcd. Hint: it doesn't have to be a member function of Rational, since it operates only with the two arguments.

    Then implement the simplifying method.

    Then implement the mathematical operators, one by one.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class rational
    {
    private:
    int num;
    int denom;
    int normalize();
    
    public:
    rational()
    {
    num=0;
    denom=1;
    }
    rational( int n )
    {
    num=n;
    denom=1;
    }
    rational (int numerator, int denominator)
    {
    num=numerator;
    denom=denominator;
    }
    
    void input()
    {
      cin>>num>>denom;
    }
    rational add(rational&)
    {
    rational result;
    rational right;
       result.num    = (num* right.denom +
    		   right.num* denom);
       result.denom = denom*right.denom;
       return result;
    }
    
    void display()
    {
    cout<<num<<"/"<<denom;
    
    }
    };
    int main()
    {
    clrscr();
    rational x;
       rational a(3);
       rational b(3, 4);
       rational c(2, 4); 
    x.input();
    x.display();
    getch();
    return 0;
    }
    i guess it has some logical error it is nt displaying the sum,, Please Help!!=((

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    I think you are coding very hard these dayz thatz very good

    See it is having problem in

    Code:
    rational add(rational&)
    {
    rational result;
    rational right;
       result.num    = (num* right.denom +
    		   right.num* denom);
       result.denom = denom*right.denom;
       return result;
    }
    you can write like this

    Code:
      rational add(rational& right)
      {
        rational result;
        result.num    = (num* right.denom +
                         right.num* denom);
        result.denom = denom*right.denom;
        return result;
      }
    i don't know i can write that which you want or not hope it will help you out

    Or if you can't understand then ask in this forum

    Enjoy C++ Programming

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    I think algo of rational addition is not correct in

    Code:
      rational add(rational& right)
      {
        rational result;
        result.num    = (num* right.denom +
                         right.num* denom);
        result.denom = denom*right.denom;
        return result;
      }
    please refer the above code which i posted earlier

    Code:
    RationalNumber RationalNumber::Add(RationalNumber __first,
                                       RationalNumber __second) {
      RationalNumber result;
      result.denominator = __first.denominator * __second.denominator;
      result.numerator   = ((result.denominator / __first.denominator)  *   \
                            __first.numerator)                          +   \
                           ((result.denominator / __second.denominator) *   \
                            __second.numerator);
        return result;
    }

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class rational
    {
    private:
    int num;
    int denom;
    int normalize();
    
    public:
    rational()
    {
    num=0;
    denom=1;
    }
    rational( int n )
    {
    num=n;
    denom=1;
    }
    rational (int numerator, int denominator)
    {
    num=numerator;
    denom=denominator;
    }
    
    void input()
    {
      cin>>num>>denom;
    }
    rational operator + (rational& right)
    
      {
        rational result;
        result.num    = (num* right.denom +
                         right.num* denom);
        result.denom = denom*right.denom;
        return result;
      }
    
    
    void display()
    {
    cout<<num<<"/"<<denom;
    
    }
    };
    int main()
    {
    clrscr();
    rational x,e;
       rational a(3);
       rational b(3, 4);
       rational c(2, 4);
    
    x.input();
    x.display();
    e=b + x;
    cout<<e;
    getch();
    return 0;
    }
    Now whats the error???

  14. #14
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    #include<iostream>
    
    
    class rational
    {
    private:
      int num;
      int denom;
      int normalize();
    
    public:
      rational()
      {
        num=0;
        denom=1;
      }
      rational( int n )
      {
        num=n;
        denom=1;
      }
      rational (int numerator, int denominator)
      {
        num=numerator;
        denom=denominator;
      }
    
      void input()
      {
        std::cin>>num>>denom;
      }
      rational operator + (rational& right)
    
      {
        rational result;
        result.num    = (num* right.denom +
                         right.num* denom);
        result.denom = denom*right.denom;
        return result;
      }
    
    
      void display()
      {
        std::cout<<num<<"/"<<denom << std::endl;
    
      }
    };
    int main()
    {
      rational x,e;
      rational a(3);
      rational b(3, 4);
      rational c(2, 4);
    
      x.input();
      x.display();
      e=b + x;
      e.display();
      return 0;
    }

    Your code which is compiling

    see you can't just print a class object like rational e you were printing directly with << operator with cout ok.....
    If you want to do that you have to overload instream ( << ) operator for that

    i think what i did modification to your code can achieve your goal

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Ohhh thanks,, I know that was a silly mistake,, i think i need a break but i have to submit this assignment today,,!! =((

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM