Bez popisu

Rational.cpp 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Rational Rational::operator+(const Rational &b) const {
  22. return Rational(num * b.den + den * b.num, den * b.den);
  23. }
  24. double Rational::operator+(double b) const {
  25. return real() + b;
  26. }
  27. Rational Rational::operator*(const Rational &b) const {
  28. return Rational(num * b.num, den * b.den);
  29. }
  30. bool Rational::gt(const Rational &b) const {
  31. return num * b.den > den * b.num;
  32. }
  33. bool Rational::operator>(const Rational &b) const {
  34. return num * b.den > den * b.num;
  35. }
  36. void Rational::display() const {
  37. cout << num << " / " << den << endl;
  38. }
  39. double Rational::real() const {
  40. return static_cast<double>(num) / den;
  41. }
  42. double operator+(double a, const Rational &b) {
  43. return a + b.real();
  44. }