Guest User

Untitled

a guest
Mar 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. use std::cmp::Ordering;
  2. use std::ops::Mul;
  3. use std::ops::Add;
  4. use std::ops::Sub;
  5. use std::ops::Div;
  6. use std::fmt;
  7.  
  8. #[derive(Debug)]
  9. struct Fraction<T> {
  10. numerator: T,
  11. denominator: T,
  12. }
  13.  
  14. impl<T> Clone for Fraction<T>
  15. where
  16. T: Clone,
  17. {
  18. fn clone(&self) -> Fraction<T> {
  19. Fraction::<T> {
  20. numerator: self.numerator.clone(),
  21. denominator: self.denominator.clone(),
  22. }
  23. }
  24. }
  25.  
  26. impl<T> PartialOrd for Fraction<T>
  27. where
  28. T: Mul,
  29. T: Mul<Output = T>,
  30. T: Ord,
  31. T: Clone,
  32. {
  33. fn partial_cmp(&self, other: &Fraction<T>) -> Option<Ordering> {
  34. Some(self.cmp(other))
  35. }
  36. }
  37.  
  38. impl<T> Ord for Fraction<T>
  39. where
  40. T: Mul,
  41. T: Mul<Output = T>,
  42. T: Ord,
  43. T: Clone,
  44. {
  45. fn cmp(&self, other: &Fraction<T>) -> Ordering {
  46. let x = self.numerator.clone() * other.denominator.clone();
  47. let y = other.numerator.clone() * self.denominator.clone();
  48. x.cmp(&y)
  49. }
  50. }
  51.  
  52. impl<T> PartialEq for Fraction<T>
  53. where
  54. T: Mul,
  55. T: Mul<Output = T>,
  56. T: PartialEq,
  57. T: Clone,
  58. {
  59. fn eq(&self, other: &Fraction<T>) -> bool {
  60. let x = self.numerator.clone() * other.denominator.clone();
  61. let y = other.numerator.clone() * self.denominator.clone();
  62. x.eq(&y)
  63. }
  64. }
  65.  
  66. impl<T> Eq for Fraction<T>
  67. where
  68. T: Mul,
  69. T: Mul<Output = T>,
  70. T: Eq,
  71. T: Clone,
  72. {
  73. }
  74.  
  75. impl<T> Add for Fraction<T>
  76. where
  77. T: Mul,
  78. T: Mul<Output = T>,
  79. T: Add,
  80. T: Add<Output = T>,
  81. T: Clone,
  82. {
  83. type Output = Fraction<T>;
  84.  
  85. fn add(self, other: Fraction<T>) -> Fraction<T> {
  86. let d = self.denominator.clone() * other.denominator.clone();
  87. let x = self.numerator.clone() * other.denominator.clone();
  88. let y = other.numerator.clone() * self.denominator.clone();
  89. fraction::<T>(x + y, d)
  90. }
  91. }
  92.  
  93. impl<T> Sub for Fraction<T>
  94. where
  95. T: Mul,
  96. T: Mul<Output = T>,
  97. T: Sub,
  98. T: Sub<Output = T>,
  99. T: Clone,
  100. {
  101. type Output = Fraction<T>;
  102.  
  103. fn sub(self, other: Fraction<T>) -> Fraction<T> {
  104. let d = self.denominator.clone() * other.denominator.clone();
  105. let x = self.numerator.clone() * other.denominator.clone();
  106. let y = other.numerator.clone() * self.denominator.clone();
  107. fraction::<T>(x - y, d)
  108. }
  109. }
  110.  
  111. impl<T> Mul for Fraction<T>
  112. where
  113. T: Mul,
  114. T: Mul<Output = T>,
  115. T: Clone,
  116. {
  117. type Output = Fraction<T>;
  118. fn mul(self, other: Fraction<T>) -> Fraction<T> {
  119. Fraction {
  120. numerator: self.numerator.clone() * other.numerator.clone(),
  121. denominator: self.denominator.clone() * other.denominator.clone(),
  122. }
  123. }
  124. }
  125.  
  126. impl<T> Div for Fraction<T>
  127. where
  128. T: Mul,
  129. T: Mul<Output = T>,
  130. T: Clone,
  131. {
  132. type Output = Fraction<T>;
  133. fn div(self, other: Fraction<T>) -> Fraction<T> {
  134. Fraction {
  135. numerator: self.numerator.clone() * other.denominator.clone(),
  136. denominator: self.denominator.clone() * other.numerator.clone(),
  137. }
  138. }
  139. }
  140.  
  141. impl<T> fmt::Display for Fraction<T>
  142. where
  143. T: fmt::Display,
  144. {
  145. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  146. write!(f, "{}/{}", self.numerator, self.denominator)
  147. }
  148. }
  149.  
  150. fn fraction<T>(x: T, y: T) -> Fraction<T> {
  151. Fraction {
  152. numerator: x,
  153. denominator: y,
  154. }
  155. }
  156. /*
  157. impl<T> Fraction<T> {
  158. fn simplify(&self) -> Fraction<T>
  159. where
  160. T: Div,
  161. T: Div<Output = T>,
  162. {
  163. let d = gcd(self.numerator, self.denominator);
  164. fraction::<T>(self.numerator / d, self.denominator / d)
  165. }
  166. }
  167.  
  168. fn gcd<T>(m: T, n: T) -> T
  169. where
  170. T: PartialEq,
  171. {
  172. if m == 0 {
  173. n.abs()
  174. } else {
  175. gcd(n % m, m)
  176. }
  177. }*/
  178.  
  179. fn main() {
  180. let x = fraction::<i64>(1, 2);
  181. let y = fraction::<i64>(1, 3);
  182. println!("x = {}", x);
  183. println!("y = {}", y);
  184. println!("x < y = {:?}", x < y);
  185. println!("x > y = {:?}", x > y);
  186. println!("x + y = {}", x.clone() + y.clone());
  187. println!("x - y = {}", x.clone() - y.clone());
  188. println!("x * y = {}", x.clone() * y.clone());
  189. println!("x / y = {}", x.clone() / y.clone());
  190. }
Add Comment
Please, Sign In to add comment