Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::Ordering;
- use std::ops::Mul;
- use std::ops::Add;
- use std::ops::Sub;
- use std::ops::Div;
- use std::fmt;
- #[derive(Debug)]
- struct Fraction<T> {
- numerator: T,
- denominator: T,
- }
- impl<T> Clone for Fraction<T>
- where
- T: Clone,
- {
- fn clone(&self) -> Fraction<T> {
- Fraction::<T> {
- numerator: self.numerator.clone(),
- denominator: self.denominator.clone(),
- }
- }
- }
- 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,
- {
- }
- impl<T> Add for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Add,
- T: Add<Output = T>,
- T: Clone,
- {
- type Output = Fraction<T>;
- fn add(self, other: Fraction<T>) -> Fraction<T> {
- let d = self.denominator.clone() * other.denominator.clone();
- let x = self.numerator.clone() * other.denominator.clone();
- let y = other.numerator.clone() * self.denominator.clone();
- fraction::<T>(x + y, d)
- }
- }
- impl<T> Sub for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Sub,
- T: Sub<Output = T>,
- T: Clone,
- {
- type Output = Fraction<T>;
- fn sub(self, other: Fraction<T>) -> Fraction<T> {
- let d = self.denominator.clone() * other.denominator.clone();
- let x = self.numerator.clone() * other.denominator.clone();
- let y = other.numerator.clone() * self.denominator.clone();
- fraction::<T>(x - y, d)
- }
- }
- impl<T> Mul for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Clone,
- {
- type Output = Fraction<T>;
- fn mul(self, other: Fraction<T>) -> Fraction<T> {
- Fraction {
- numerator: self.numerator.clone() * other.numerator.clone(),
- denominator: self.denominator.clone() * other.denominator.clone(),
- }
- }
- }
- impl<T> Div for Fraction<T>
- where
- T: Mul,
- T: Mul<Output = T>,
- T: Clone,
- {
- type Output = Fraction<T>;
- fn div(self, other: Fraction<T>) -> Fraction<T> {
- Fraction {
- numerator: self.numerator.clone() * other.denominator.clone(),
- denominator: self.denominator.clone() * other.numerator.clone(),
- }
- }
- }
- impl<T> fmt::Display for Fraction<T>
- where
- T: fmt::Display,
- {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}/{}", self.numerator, self.denominator)
- }
- }
- fn fraction<T>(x: T, y: T) -> Fraction<T> {
- Fraction {
- numerator: x,
- denominator: y,
- }
- }
- /*
- impl<T> Fraction<T> {
- fn simplify(&self) -> Fraction<T>
- where
- T: Div,
- T: Div<Output = T>,
- {
- let d = gcd(self.numerator, self.denominator);
- fraction::<T>(self.numerator / d, self.denominator / d)
- }
- }
- fn gcd<T>(m: T, n: T) -> T
- where
- T: PartialEq,
- {
- if m == 0 {
- n.abs()
- } else {
- gcd(n % m, m)
- }
- }*/
- 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);
- println!("x + y = {}", x.clone() + y.clone());
- println!("x - y = {}", x.clone() - y.clone());
- println!("x * y = {}", x.clone() * y.clone());
- println!("x / y = {}", x.clone() / y.clone());
- }
Add Comment
Please, Sign In to add comment