12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include <iostream>
- #include <algorithm>
- #include "Rational.h"
-
- using namespace std;
-
-
- Rational::Rational() {
- num = 0;
- den = 1;
- }
-
- Rational::Rational(int n, int d) {
- num = n;
- den = d;
- }
-
- void Rational::reduce() {
- int g = __gcd(num,den);
- num /= g;
- den /= g;
- }
-
- Rational Rational::sum(const Rational &b) const {
- return Rational(num * b.den + den * b.num, den * b.den);
- }
-
- Rational Rational::operator+(const Rational &b) const {
- return Rational(num * b.den + den * b.num, den * b.den);
- }
-
- double Rational::operator+(double b) const {
- return real() + b;
- }
-
- Rational Rational::operator*(const Rational &b) const {
- return Rational(num * b.num, den * b.den);
- }
-
-
- bool Rational::gt(const Rational &b) const {
- return num * b.den > den * b.num;
- }
-
- bool Rational::operator>(const Rational &b) const {
- return num * b.den > den * b.num;
- }
-
-
-
- void Rational::display() const {
- cout << num << " / " << den << endl;
- }
-
-
- double Rational::real() const {
- return static_cast<double>(num) / den;
- }
-
-
- double operator+(double a, const Rational &b) {
- return a + b.real();
- }
-
-
-
-
-
-
|