forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rational.h.html
59 lines (45 loc) · 4.21 KB
/
Rational.h.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>Rational.h</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// File Rational.h</span></i>
<b><span style="color:#000080">#ifndef</span></b> RATIONAL_H
<b><span style="color:#000080">#define</span></b> RATIONAL_H
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">Rational</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<i><span style="color:#9A1900">// default constructor</span></i>
<b><span style="color:#000000">Rational</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// destructor</span></i>
<span style="color:#990000">~</span><b><span style="color:#000000">Rational</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// create and initialize a new Rational object</span></i>
<b><span style="color:#000000">Rational</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> numerator<span style="color:#990000">,</span> <span style="color:#009900">int</span> denominator<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// print string representation of (this) to cout</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">print</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// return (this * b)</span></i>
<span style="color:#008080">Rational</span> <b><span style="color:#000000">times</span></b><span style="color:#990000">(</span><span style="color:#008080">Rational</span> b<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// return (this + b)</span></i>
<span style="color:#008080">Rational</span> <b><span style="color:#000000">plus</span></b><span style="color:#990000">(</span><span style="color:#008080">Rational</span> b<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// return (1 / this)</span></i>
<span style="color:#008080">Rational</span> <b><span style="color:#000000">reciprocal</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// return (this / b)</span></i>
<span style="color:#008080">Rational</span> <b><span style="color:#000000">divides</span></b><span style="color:#990000">(</span><span style="color:#008080">Rational</span> b<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">private</span></b><span style="color:#990000">:</span>
<span style="color:#009900">int</span> num<span style="color:#990000">;</span> <i><span style="color:#9A1900">// the numerator</span></i>
<span style="color:#009900">int</span> den<span style="color:#990000">;</span> <i><span style="color:#9A1900">// the denominator</span></i>
<i><span style="color:#9A1900">/*************************************************************************</span></i>
<i><span style="color:#9A1900"> * Helper functions</span></i>
<i><span style="color:#9A1900"> *************************************************************************/</span></i>
<i><span style="color:#9A1900">// return gcd(m, n)</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">gcd</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> m<span style="color:#990000">,</span> <span style="color:#009900">int</span> n<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<b><span style="color:#000080">#endif</span></b>
</pre>
</body>
</html>