Need help(Rational no. class)

This is a discussion on Need help(Rational no. class) within the C++ Programming forums, part of the General Programming Boards category; Ok just take a break and take a chill pill too u r in great need of it...

  1. #16
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Ok just take a break and take a chill pill too u r in great need of it

  2. #17
    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;
      }
    rational operator - (rational& right)
     {
       rational result;
       result.num    = (num*right.denom-right.num*denom);
       result.denom = denom*right.denom;
       return result;
    }
    rational operator * (rational& right) {
       rational result;
       result.num    =num*right.num;
       result.denom =denom*right.denom;
       return result;
    }
    rational operator / (rational& right) {
       rational result;
       result.num    = num*right.denom;
       result.denom = denom*right.num;
       return result;
       }
    
    void display()
    {
    cout<<num<<"/"<<denom<<endl;
    
    }
    };
    int main()
    {
    clrscr();
    rational x,e,f,g,h;
       rational a(3);
       rational b(3, 4);
       rational c(2, 4);
    
    x.input();
    x.display();
    e=b + x;
    e.display();
    f=b - x;
    f.display();
    g=b * x;
    g.display();
    h=b/x;
    h.display();
    getch();
    return 0;
    }
    i m almost done with it,, how to simplify the rational numbers??
    the code i used in the first post,, the same way i add it to private??

  3. #18
    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;
        result.normalize();
        return result;
      }
    rational operator - (rational& right)
     {
       rational result;
       result.num    = (num*right.denom-right.num*denom);
       result.denom = denom*right.denom;
       return result;
    }
    rational operator * (rational& right) {
       rational result;
       result.num    =num*right.num;
       result.denom =denom*right.denom;
       return result;
    }
    rational operator / (rational& right) {
       rational result;
       result.num    = num*right.denom;
       result.denom = denom*right.num;
       return result;
       }
    
    void display()
    {
    cout<<num<<"/"<<denom<<endl;
    
    }
    };
    
    int gcd(int n, int m);
    int main()
    {
    clrscr();
    void rational::normalize()
    {
       int d, sign;
       sign = 1;
       if (top < 0) {
         sign = -1;
         top = -top;
       }
       d = gcd(top, bottom);
       top = sign*(top / d);
       bottom = bottom / d;
    }
    
    
    rational x,e,f,g,h;
       rational a(3);
       rational b(3, 4);
       rational c(2, 4);
    
    x.input();
    x.display();
    e=b + x;
    e.display();
    f=b - x;
    f.display();
    g=b * x;
    g.display();
    h=b/x;
    h.display();
    getch();
    return 0;
    }
    
    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;
     }
    Can anybuddy tell me my mistake

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What does the compiler say is wrong with your program?
    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).

  5. #20
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    ohk i have done it,,

    how to print a constructor??

  6. #21
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    ohk i have done it,,

    how to print a constructor??
    What doest this means i can't get u ??

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by RockyMarrone View Post
    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;
    }
    Funny thing is, his code for that actually is correct. Yours is simply unoptimal and can be simplified to what he has! Now just who should be helping who?

    You should not use variable names with double-leading-underscores. I can't quite remember exactly what names are reserved for the compiler, but you're either using them or close.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
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, 08: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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21