Need help(Rational no. class)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-10-2009
Fatima Rizwan
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(); }```
• 10-10-2009
RockyMarrone
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
• 10-10-2009
RockyMarrone
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 rocky.marrone@gmail.com
• 10-10-2009
RockyMarrone
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 ??
• 10-10-2009
Fatima Rizwan
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.
• 10-10-2009
Fatima Rizwan
I am working on the above instructions,,, thank yew so much for the help but i didnt really get ur code,,=(((
• 10-10-2009
RockyMarrone
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
• 10-10-2009
RockyMarrone
Or if you didn't understand the code what i can i do i can just simply make u understandable on chat,
• 10-10-2009
anon
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.
• 10-10-2009
Fatima Rizwan
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; }```
• 10-10-2009
RockyMarrone
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 :)
• 10-10-2009
RockyMarrone
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; }```
• 10-10-2009
Fatima Rizwan
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???
• 10-10-2009
RockyMarrone
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; }```

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
• 10-10-2009
Fatima Rizwan
Ohhh thanks,, I know that was a silly mistake,, i think i need a break but i have to submit this assignment today,,!! =((
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last