forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rational.cpp
69 lines (56 loc) · 1.64 KB
/
Rational.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// File Rational.cpp
#include "Rational.h"
#include <iostream>
using namespace std;
// default constructor: initialize to 0/1
Rational::Rational() : num(0), den(1) {
}
// destructor : more on these later...
Rational::~Rational() {
}
// create and initialize a new Rational object
Rational::Rational(int numerator, int denominator) {
if (denominator == 0) {
cout << "Denominator is zero" << endl;
}
int g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g;
}
// print string representation of (this) to cout
void Rational::print() const {
if (den == 1) {
cout << num << "" << endl;
} else {
cout << num << "/" << den << endl;
}
}
// return (this * b)
Rational Rational::times(Rational b) const {
return Rational(num * b.num, den * b.den);
}
// return (this + b)
Rational Rational::plus(Rational b) const {
int numerator = (num * b.den) + (den * b.num);
int denominator = den * b.den;
return Rational(numerator, denominator);
}
// return (1 / this)
Rational Rational::reciprocal() const {
return Rational(den, num);
}
// return (this / b)
Rational Rational::divides(Rational b) const {
return times(b.reciprocal());
}
/*************************************************************************
* Helper functions
*************************************************************************/
// return gcd(m, n)
// not marked as 'static' here because that can only be done in declaration
int Rational::gcd(int m, int n) {
if (0 == n)
return m;
else
return gcd(n, m % n);
}