Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class Rational
- {
- private:
- void reduce()
- {
- int temp = num;
- num = num/gcd(num,denom);
- denom = denom/gcd(temp,denom);
- }
- //numerator variable
- int num;
- //denominator variable
- int denom;
- int gcd(int n, int d)
- {
- if ( n % d == 0 ) // base case
- return d;
- else
- return gcd(d, n % d);
- }
- public:
- Rational(int n, int d)
- {
- num = n;
- denom = d;
- reduce();
- }
- friend ostream & operator<<(ostream &out, const Rational &obj)
- {
- out << "[" << obj.num << "/" << obj.denom << "]";
- return out;
- }
- };
- int main()
- {
- int num = 0;
- int denom = 1;
- int gcd;
- cout << "Enter a numerator. \n";
- cin >> num;
- cout << "\nEnter a denominator. \n";
- cin >> denom;
- Rational fraction(num, denom);
- cout << fraction << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment