@@ -22,34 +22,34 @@ use super::math_methods::{gcd, lcm};
22
22
use handler:: ErrorCases :: UndefinedFrac ;
23
23
use handler:: ErrorCases ;
24
24
use public:: Operator :: { Abs , Add , Div , Mul , Sub } ;
25
- use public:: safe_calc;
25
+ use public:: { safe_calc, CheckedType } ;
26
26
27
- fn rfcd ( a : & Frac , b : & Frac ) -> Result < ( i32 , i32 , i32 ) , ErrorCases > {
27
+ fn rfcd < T : CheckedType > ( a : & Frac < T > , b : & Frac < T > ) -> Result < ( T , T , T ) , ErrorCases > {
28
28
// reduction of fractions to a common denominator
29
29
let d = lcm ( a. denominator , b. denominator ) ?;
30
- let mut a_n = safe_calc ( d, a. denominator , & Div ) ?;
31
- let mut b_n = safe_calc ( d, b. denominator , & Div ) ?;
32
- a_n = safe_calc ( a_n, a. numerator , & Mul ) ?;
33
- b_n = safe_calc ( b_n, b. numerator , & Mul ) ?;
30
+ let mut a_n = safe_calc ( & d, & a. denominator , & Div ) ?;
31
+ let mut b_n = safe_calc ( & d, & b. denominator , & Div ) ?;
32
+ a_n = safe_calc ( & a_n, & a. numerator , & Mul ) ?;
33
+ b_n = safe_calc ( & b_n, & b. numerator , & Mul ) ?;
34
34
Ok ( ( a_n, b_n, d) )
35
35
}
36
36
37
37
#[ derive( Clone , Copy ) ]
38
- pub struct Frac {
39
- pub numerator : i32 ,
40
- pub denominator : i32 ,
38
+ pub struct Frac < T : CheckedType > {
39
+ pub numerator : T ,
40
+ pub denominator : T ,
41
41
}
42
42
43
- impl Frac {
44
- pub fn new ( numerator : i32 , denominator : i32 ) -> Self {
43
+ impl < T : CheckedType > Frac < T > {
44
+ pub fn new ( numerator : T , denominator : T ) -> Self {
45
45
Self {
46
46
numerator,
47
47
denominator,
48
48
}
49
49
}
50
50
51
51
pub fn check ( & self ) -> Result < bool , ErrorCases > {
52
- if self . denominator == 0 {
52
+ if self . denominator == T :: zero ( ) {
53
53
Err ( UndefinedFrac )
54
54
} else {
55
55
Ok ( true )
@@ -60,31 +60,31 @@ impl Frac {
60
60
self . check ( ) ?;
61
61
let gcd = gcd ( self . numerator , self . denominator ) ?;
62
62
Ok ( Self {
63
- numerator : safe_calc ( self . numerator , gcd, & Div ) ?,
64
- denominator : safe_calc ( self . denominator , gcd, & Div ) ?,
63
+ numerator : safe_calc ( & self . numerator , & gcd, & Div ) ?,
64
+ denominator : safe_calc ( & self . denominator , & gcd, & Div ) ?,
65
65
} )
66
66
}
67
67
68
68
pub fn abs ( & self ) -> Result < Self , ErrorCases > {
69
69
self . check ( ) ?;
70
70
let mut tmp = Self {
71
- numerator : safe_calc ( self . numerator , 0 , & Abs ) ?,
72
- denominator : safe_calc ( self . denominator , 0 , & Abs ) ?,
71
+ numerator : safe_calc ( & self . numerator , & T :: zero ( ) , & Abs ) ?,
72
+ denominator : safe_calc ( & self . denominator , & T :: zero ( ) , & Abs ) ?,
73
73
} ;
74
74
tmp = tmp. simple ( ) ?;
75
75
tmp. check ( ) ?;
76
76
Ok ( tmp)
77
77
}
78
78
}
79
79
80
- impl ops:: Add for Frac {
80
+ impl < T : CheckedType > ops:: Add for Frac < T > {
81
81
type Output = Result < Self , ErrorCases > ;
82
82
fn add ( self , b : Self ) -> Result < Self , ErrorCases > {
83
83
self . check ( ) ?;
84
84
b. check ( ) ?;
85
85
let ( a_n, b_n, d) = rfcd ( & self , & b) ?;
86
86
let mut tmp = Self {
87
- numerator : safe_calc ( a_n, b_n, & Add ) ?,
87
+ numerator : safe_calc ( & a_n, & b_n, & Add ) ?,
88
88
denominator : d,
89
89
} ;
90
90
tmp = tmp. simple ( ) ?;
@@ -93,14 +93,14 @@ impl ops::Add for Frac {
93
93
}
94
94
}
95
95
96
- impl ops:: Sub for Frac {
96
+ impl < T : CheckedType > ops:: Sub for Frac < T > {
97
97
type Output = Result < Self , ErrorCases > ;
98
98
fn sub ( self , b : Self ) -> Result < Self , ErrorCases > {
99
99
self . check ( ) ?;
100
100
b. check ( ) ?;
101
101
let ( a_n, b_n, d) = rfcd ( & self , & b) ?;
102
102
let mut tmp = Self {
103
- numerator : safe_calc ( a_n, b_n, & Sub ) ?,
103
+ numerator : safe_calc ( & a_n, & b_n, & Sub ) ?,
104
104
denominator : d,
105
105
} ;
106
106
tmp = tmp. simple ( ) ?;
@@ -109,37 +109,37 @@ impl ops::Sub for Frac {
109
109
}
110
110
}
111
111
112
- impl ops:: Mul for Frac {
112
+ impl < T : CheckedType > ops:: Mul for Frac < T > {
113
113
type Output = Result < Self , ErrorCases > ;
114
114
fn mul ( self , b : Self ) -> Result < Self , ErrorCases > {
115
115
self . check ( ) ?;
116
116
b. check ( ) ?;
117
117
let mut tmp = Self {
118
- numerator : safe_calc ( self . numerator , b. numerator , & Mul ) ?,
119
- denominator : safe_calc ( self . denominator , b. denominator , & Mul ) ?,
118
+ numerator : safe_calc ( & self . numerator , & b. numerator , & Mul ) ?,
119
+ denominator : safe_calc ( & self . denominator , & b. denominator , & Mul ) ?,
120
120
} ;
121
121
tmp = tmp. simple ( ) ?;
122
122
tmp. check ( ) ?;
123
123
Ok ( tmp)
124
124
}
125
125
}
126
126
127
- impl ops:: Div for Frac {
127
+ impl < T : CheckedType > ops:: Div for Frac < T > {
128
128
type Output = Result < Self , ErrorCases > ;
129
129
fn div ( self , b : Self ) -> Result < Self , ErrorCases > {
130
130
self . check ( ) ?;
131
131
b. check ( ) ?;
132
132
let mut tmp = Self {
133
- numerator : safe_calc ( self . numerator , b. denominator , & Mul ) ?,
134
- denominator : safe_calc ( self . denominator , b. numerator , & Mul ) ?,
133
+ numerator : safe_calc ( & self . numerator , & b. denominator , & Mul ) ?,
134
+ denominator : safe_calc ( & self . denominator , & b. numerator , & Mul ) ?,
135
135
} ;
136
136
tmp = tmp. simple ( ) ?;
137
137
tmp. check ( ) ?;
138
138
Ok ( tmp)
139
139
}
140
140
}
141
141
142
- impl PartialOrd for Frac {
142
+ impl < T : CheckedType > PartialOrd for Frac < T > {
143
143
fn partial_cmp ( & self , b : & Self ) -> Option < Ordering > {
144
144
if self . check ( ) . is_err ( ) || b. check ( ) . is_err ( ) {
145
145
None
@@ -148,19 +148,19 @@ impl PartialOrd for Frac {
148
148
Ok ( s) => s,
149
149
Err ( e) => panic ! ( "[Ord] `lcm` Err: {:?}" , e) ,
150
150
} ;
151
- let mut a_n = match safe_calc ( d, self . denominator , & Div ) {
151
+ let mut a_n = match safe_calc ( & d, & self . denominator , & Div ) {
152
152
Ok ( s) => s,
153
153
Err ( e) => panic ! ( "[Ord] Err: {:?}" , e) ,
154
154
} ;
155
- let mut b_n = match safe_calc ( d, b. denominator , & Div ) {
155
+ let mut b_n = match safe_calc ( & d, & b. denominator , & Div ) {
156
156
Ok ( s) => s,
157
157
Err ( e) => panic ! ( "[Ord] Err: {:?}" , e) ,
158
158
} ;
159
- a_n = match safe_calc ( a_n, self . numerator , & Mul ) {
159
+ a_n = match safe_calc ( & a_n, & self . numerator , & Mul ) {
160
160
Ok ( s) => s,
161
161
Err ( e) => panic ! ( "[Ord] Err: {:?}" , e) ,
162
162
} ;
163
- b_n = match safe_calc ( b_n, b. numerator , & Mul ) {
163
+ b_n = match safe_calc ( & b_n, & b. numerator , & Mul ) {
164
164
Ok ( s) => s,
165
165
Err ( e) => panic ! ( "[Ord] Err: {:?}" , e) ,
166
166
} ;
@@ -169,7 +169,7 @@ impl PartialOrd for Frac {
169
169
}
170
170
}
171
171
172
- impl PartialEq for Frac {
172
+ impl < T : CheckedType > PartialEq for Frac < T > {
173
173
fn eq ( & self , b : & Self ) -> bool {
174
174
if b. check ( ) . is_err ( ) || self . check ( ) . is_err ( ) {
175
175
panic ! ( "[Eq] UndefinedFrac" )
0 commit comments