-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.h
135 lines (97 loc) · 3.94 KB
/
fraction.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma once
#include <iostream>
using std::istream;
using std::ostream;
namespace Jidantou
{
class fraction
{
private:
// numerator, must be positive
uint64_t _numerator;
// denominator, must be positive
uint64_t _denominator;
// 0 for positive, 1 for negative
bool _sign;
/* Special situation
NaN(Not a Number) : _numerator == 0 && _denominator == 0
Infinity : _numerator != 0 && _denominator == 0
_sign == 0 for positive , _sign == 1 for negative
*/
// reduction of a fraction
void reduction();
public:
// default constructor
fraction();
// Not recommended, because it maybe overflow and cause unexpected error
// turn double to fraction
__attribute__((deprecated)) fraction(const double num);
// turn int8_t to fraction
fraction(const int8_t num);
// turn int16_t to fraction
fraction(const int16_t num);
// turn int32_t to fraction
fraction(const int32_t num);
// turn int64_t to fraction
fraction(const int64_t num);
// Highly recommened
// set numerator, denominator, and sign(default 0)
fraction(const uint64_t numerator, const uint64_t denominator, const bool sign = 0);
// copy constructor
fraction(const fraction &fraction1);
// destructor
~fraction();
// format output, "(-)numerator/denominator", such as -5/8
friend ostream &operator<<(ostream &output, const fraction fraction1);
// format input
// please input fraction as "(-)numerator(int64_t)/(-)denominator(int64_t)"
// "-9c" will be recognized as -9/1
friend istream &operator>>(istream &input, fraction &fraction1);
// judge whether this fraction is "Not a Number"
bool isNaN() const;
// judge whether this fraction is infinify
bool isInfinify() const;
// swap numerator and denominator
void reciprocal();
// Implicit conversion : fraction to double
operator double() const
{
return _sign == 0 ? _numerator / _denominator : -_numerator / _denominator;
}
// Implicit conversion : fraction to int64_t
operator int64_t() const
{
return _sign == 0 ? _numerator / _denominator : -_numerator / _denominator;
}
// overload function -fraction1
fraction operator-() const;
// overload function fraction1 + fraction2
fraction operator+(const fraction &fraction2) const;
// overload function fraction1 - fraction2
fraction operator-(const fraction &fraction2) const;
// overload function fraction1 * fraction2
fraction operator*(const fraction &fraction2) const;
// overload function fraction1 / fraction2
fraction operator/(const fraction &fraction2) const;
// overload function fraction1 += fraction2
fraction &operator+=(const fraction &fraction2);
// overload function fraction1 -= fraction2
fraction &operator-=(const fraction &fraction2);
// overload function fraction1 *= fraction2
fraction &operator*=(const fraction &fraction2);
// overload function fraction1 /= fraction2
fraction &operator/=(const fraction &fraction2);
// judge fraction1 and fraction2
bool operator<(const fraction fraction1) const;
// judge fraction1 and fraction2
bool operator>(const fraction fraction1) const;
// judge fraction1 and fraction2
bool operator<=(const fraction fraction1) const;
// judge fraction1 and fraction2
bool operator>=(const fraction fraction1) const;
// judge fraction1 and fraction2
bool operator!=(const fraction fraction1) const;
// judge fraction1 and fraction2
bool operator==(const fraction fraction1) const;
};
}