No Description

Rational.cpp 646B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <iostream>
  2. #include <algorithm>
  3. #include "Rational.h"
  4. using namespace std;
  5. Rational::Rational() {
  6. num = 0;
  7. den = 1;
  8. }
  9. Rational::Rational(int n, int d) {
  10. num = n;
  11. den = d;
  12. }
  13. void Rational::reduce() {
  14. int g = __gcd(num,den);
  15. num /= g;
  16. den /= g;
  17. }
  18. Rational Rational::sum(const Rational &b) const {
  19. return Rational(num * b.den + den * b.num, den * b.den);
  20. }
  21. bool Rational::gt(const Rational &b) const {
  22. /*
  23. if (num * b.den > den * b.num) {
  24. return true;
  25. }
  26. else {
  27. return false;
  28. }
  29. */
  30. return num * b.den > den * b.num;
  31. }
  32. void Rational::display() const {
  33. cout << num << " / " << den << endl;
  34. }