# Thread: Need help(Rational no. class)

1. Ok just take a break and take a chill pill too u r in great need of it

2. 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. 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. What does the compiler say is wrong with your program?

5. ohk i have done it,,

how to print a constructor??

6. Originally Posted by Fatima Rizwan
ohk i have done it,,

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

7. Originally Posted by 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;
}```
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.

Popular pages Recent additions