Guest User

Untitled

a guest
Mar 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. use std::cmp::Ordering;
  2. use std::ops::Mul;
  3.  
  4. #[derive(Debug)]
  5. struct Fraction<T> {
  6. numerator: T,
  7. denominator: T,
  8. }
  9.  
  10. impl<T> PartialOrd for Fraction<T>
  11. where
  12. T: Mul,
  13. T: Mul<Output = T>,
  14. T: Ord,
  15. T: Clone,
  16. {
  17. fn partial_cmp(&self, other: &Fraction<T>) -> Option<Ordering> {
  18. Some(self.cmp(other))
  19. }
  20. }
  21.  
  22. impl<T> Ord for Fraction<T>
  23. where
  24. T: Mul,
  25. T: Mul<Output = T>,
  26. T: Ord,
  27. T: Clone
  28. {
  29. fn cmp(&self, other: &Fraction<T>) -> Ordering {
  30. let x = self.numerator.clone() * other.denominator.clone();
  31. let y = other.numerator.clone() * self.denominator.clone();
  32. x.cmp(&y)
  33. }
  34. }
  35.  
  36. impl<T> PartialEq for Fraction<T>
  37. where
  38. T: Mul,
  39. T: Mul<Output = T>,
  40. T: PartialEq,
  41. T: Clone,
  42. {
  43. fn eq(&self, other: &Fraction<T>) -> bool {
  44. let x = self.numerator.clone() * other.denominator.clone();
  45. let y = other.numerator.clone() * self.denominator.clone();
  46. x.eq(&y)
  47. }
  48. }
  49.  
  50. impl<T> Eq for Fraction<T>
  51. where
  52. T: Mul,
  53. T: Mul<Output = T>,
  54. T: Eq,
  55. T: Clone
  56. {
  57. }
  58.  
  59.  
  60.  
  61. fn fraction<T>(x: T, y: T) -> Fraction<T> {
  62. Fraction {
  63. numerator: x,
  64. denominator: y,
  65. }
  66. }
  67.  
  68. fn main() {
  69. let x = fraction::<i64>(1, 2);
  70. let y = fraction::<i64>(1, 3);
  71. println!("x = {:?}", x);
  72. println!("y = {:?}", y);
  73. println!("x < y = {:?}", x < y);
  74. println!("x > y = {:?}", x > y);
  75. }
Add Comment
Please, Sign In to add comment