# Thread: Rational number excercise need guidance

1. ## Rational number excercise need guidance

Been working on this rational number program.

Here is what the program is suppose to do. To me this is one serious program using classes. My problem is when i get to the I , M , R, G part how do i build something that will take input of that and output what is in this chart. Once again the book gives mickey mouse examples and then the excercises are not even close to them. I am hoping someone can help me out with what to do.

Second question is how could i make this program better by having it read all of these inputs and perfrom the right operations. As you can see i have been manually testing the classes by changing the operator and entering in the numbers directly in the program. When i tried to tie a user input into the program it wouldnt let me. Compiler errors.

Input ---------------------------- Output
3/8 + 1/6--------------------------13/24
3/8 - 1/6---------------------------5/24
3/8 * 1/6--------------------------1/16
3/8 / 1/6---------------------------9/4
3/8 I -------------------------------8/3 ---------------invert a/b
8/3 M ----------------------------- 2 + 2/3-----------Write a/b as a mixed fraction
6/8 R ------------------------------3/4 ---------------reduce a/b to lowest terms
6/8 G ------------------------------2------------------Greatest common divisor of num ..
1/6 L 3/8-------------------------24 ------------- Lowest common denom of a/b and c/d
1/6 < 3/8------------------------true------------a/b < c/d
1/6 < = 3/8 -------------------- true------------a/b <= c/d
1/6 > 3/8 -----------------------false------------a/b > c/d
1/6 >= 3/8----------------------false -----------a/b >= c/d
3/8 = 9/24----------------------true-------------a/b = c/d
2/3 X + 2 = 4/5--------------X = -9/5--solution of linear equation (a/b)X = c/d = e/f

Here is what i have so far:

Code:
```#include<iostream>

using namespace std;

int gcd (int n1, int n2)  //greatest common divisor
{
int rem;
while (n2 != 0)
{
rem = n1 % n2;
n1 = n2;
n2 = rem;
}
return (n1);
}

int lcm(int n1, int n2)  // least common multiple
{
return(n1*n2/gcd(n1,n2));   // because a*b=lcm(a,b)*gcd(a,b)
}

class rational
{
public:
int a, b;   // a - numerator,  b - denominator
public:
rational(){}
rational(int m, int n)
{
if (n!=0)
{
a=m;b=n;
}
else
{
cout << "Error defining rational" << endl;
exit(0);
}
}

void disp(void)
{
int temp=gcd(a,b);
a=a/temp;
b=b/temp;
if (b!=1)
{

cout << a << "/" << b;
}
else
cout << a;
}
};

rational operator +( rational p, rational  q)
{
int temp=lcm(p.b,q.b);
rational temp1(p.a*(temp/p.b)+q.a*(temp/q.b),temp);
return temp1;
}
rational operator -(rational p, rational q)
{
rational temp(-q.a,q.b);
return (p+temp);
}
rational operator *(rational p, rational q)
{
rational temp(p.a*q.a,p.b*q.b);
return temp;
}
rational operator /(rational p, rational q)
{
rational temp(p.a*q.b,p.b*q.a);
return temp;
}

void main()
{
rational a(3,8);
rational b(1,6);
rational temp=a+b;
temp.disp();
cout << "\n\n";

}``` 2. We wont know how to help if you don't post the error the compiler is giving you, and the faulty line of code. 3. No errors , it compiles perfectly right now. I am asking questions in original post, need help in making the I - G parts work in the program..... building classes 4. Is this homework? 5. Maybe...
Code:
```class rational
{
// ...
rational I()
{
int temp = a;
a = b;
b = temp;
}
};```
Code:
```   a.I();
a.disp();
cout << "\n\n";```
...for example.

Or...
Code:
```rational I(const rational &r)
{
return rational(r.b, r.a);
}```
Code:
```   temp = I(a);
temp.disp();
cout << "\n\n";``` Popular pages Recent additions 