Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::Ordering;
- use std::ops::Mul;
- #[derive(Debug)]
- struct Fraction<T> {
- numerator: T,
- denominator: T,
- }
- impl<T> PartialOrd for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Ord,
- T: Clone,
- {
- fn partial_cmp(&self, other: &Fraction<T>) -> Option<Ordering> {
- Some(self.cmp(other))
- }
- }
- impl<T> Ord for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Ord,
- T: Clone
- {
- fn cmp(&self, other: &Fraction<T>) -> Ordering {
- let x = self.numerator.clone() * other.denominator.clone();
- let y = other.numerator.clone() * self.denominator.clone();
- x.cmp(&y)
- }
- }
- impl<T> PartialEq for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: PartialEq,
- T: Clone,
- {
- fn eq(&self, other: &Fraction<T>) -> bool {
- let x = self.numerator.clone() * other.denominator.clone();
- let y = other.numerator.clone() * self.denominator.clone();
- x.eq(&y)
- }
- }
- impl<T> Eq for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Eq,
- T: Clone
- {
- }
- fn fraction<T>(x: T, y: T) -> Fraction<T> {
- Fraction {
- numerator: x,
- denominator: y,
- }
- }
- fn main() {
- let x = fraction::<i64>(1, 2);
- let y = fraction::<i64>(1, 3);
- println!("x = {:?}", x);
- println!("y = {:?}", y);
- println!("x < y = {:?}", x < y);
- println!("x > y = {:?}", x > y);
- }
Add Comment
Please, Sign In to add comment