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();
}