📄 fraction.h.html
字号:
<html>
<body>
<tt>
<pre> 1 #ifndef FRACTION_H
2 #define FRACTION_H
3
4 #include <iostream>
5
6 using namespace std;
7
8 class Fraction
9 {
10 public:
11 /**
12 Constructs a fraction with numerator 0 and denominator 1.
13 */
14 Fraction();
15
16 /**
17 Constructs a fraction with numerator t and denominator 1.
18 @param t the numerator for the fraction
19 */
20 Fraction(int t);
21
22 /**
23 Constructs a fraction with given numerator and denominator.
24 @param t the initial numerator
25 @param b the initial denominator
26 */
27 Fraction(int t, int b);
28
29 /**
30 Returns the numerator.
31 @return the numerator value
32 */
33 int numerator() const;
34
35 /**
36 Returns the denominator.
37 @return the denominator value
38 */
39 int denominator() const;
40
41 /**
42 Updates a fraction by adding in another fraction value.
43 @param right the fraction to be added
44 @return the updated fraction value
45 */
46 Fraction& operator+=(const Fraction& right);
47
48 /**
49 Increment fraction by 1.
50 */
51 Fraction& operator++(); // Prefix form
52 Fraction operator++(int unused); // Postfix form
53
54 /**
55 Converts a fraction into a floating-point value.
56 @return the converted value
57 */
58 operator double() const;
59
60 /**
61 Compare one fraction value to another.
62 Result is negative if less than right, zero if equal, and
63 positive is greater than right.
64 @param right the fraction to be compared against
65 */
66 int compare(const Fraction& right) const;
67 private:
68 /**
69 Place the fraction in least common denominator form.
70 */
71 void normalize();
72 /**
73 Compute the greatest common denominator of two integer values.
74 @param n the first integer
75 @param m the second integer
76 */
77 int gcd(int n, int m);
78 int top;
79 int bottom;
80 };
81
82 Fraction operator+(const Fraction& left, const Fraction& right);
83 Fraction operator-(const Fraction& left, const Fraction& right);
84 Fraction operator*(const Fraction& left, const Fraction& right);
85 Fraction operator/(const Fraction& left, const Fraction& right);
86 Fraction operator-(const Fraction& value);
87
88 bool operator<(const Fraction& left, const Fraction& right);
89 bool operator<=(const Fraction& left, const Fraction& right);
90 bool operator==(const Fraction& left, const Fraction& right);
91 bool operator!=(const Fraction& left, const Fraction& right);
92 bool operator>=(const Fraction& left, const Fraction& right);
93 bool operator>(const Fraction& left, const Fraction& right);
94
95 ostream& operator<<(ostream& out, const Fraction& value);
96 istream& operator>>(istream& in, Fraction& r);
97
98 #endif</pre>
</tt>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -