1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::ops::{Add, Div, Mul, Sub};

#[derive(PartialEq, Debug, Clone)]
pub struct Rational {
    num: i32,
    den: u32,
}

impl Rational {
    pub fn new(num: i32, den: i32) -> Self {
        let mut opposite_sign = false;
        if num * den < 0 {
            opposite_sign = true;
        }

        let mut num = num.abs();
        let den: u32 = den.abs() as u32;

        let gcd = gcd(num as u32, den);
        if opposite_sign {
            num *= -1;
        }

        Rational {
            num: num / gcd as i32,
            den: den / gcd,
        }
    }
    pub fn eval(&self) -> f64 {
        self.num as f64 / (self.den as f64)
    }
    pub fn num(&self) -> i32 {
        self.num
    }
    pub fn den(&self) -> i32 {
        self.den as i32
    }
}

impl Add for Rational {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let lcm = lcm(self.den, rhs.den);
        Rational::new(
            lcm as i32 / self.den as i32 * self.num + lcm as i32 / rhs.den as i32 * rhs.num,
            lcm as i32,
        )
    }
}
impl Sub for Rational {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let lcm = lcm(self.den, rhs.den);
        Rational::new(
            lcm as i32 / self.den as i32 * self.num - lcm as i32 / rhs.den as i32 * rhs.num,
            lcm as i32,
        )
    }
}
impl Mul for Rational {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Rational::new(self.num * rhs.num, (self.den * rhs.den) as i32)
    }
}
impl Div for Rational {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        Rational::new(self.num * rhs.den as i32, self.den as i32 * rhs.num)
    }
}

pub fn gcd(mut n: u32, mut m: u32) -> u32 {
    assert!(n != 0 && m != 0);

    while m != 0 {
        if m < n {
            std::mem::swap(&mut m, &mut n);
        }
        m %= n;
    }
    n
}
fn lcm(first: u32, second: u32) -> u32 {
    first * second / gcd(first, second)
}

#[test]
fn test_rat_operators() {
    let a = Rational::new(-1, 2);
    let b = Rational::new(2, 2);
    assert_eq!(a + b, Rational::new(1, 2));

    let a = Rational::new(-1, 2);
    let b = Rational::new(2, 2);
    assert_eq!(a - b, Rational::new(-3, 2));

    let a = Rational::new(-1, 2);
    let b = Rational::new(3, 2);
    assert_eq!(a * b, Rational::new(-3, 4));

    let a = Rational::new(-1, 2);
    let b = Rational::new(2, 3);
    assert_eq!(a / b, Rational::new(-3, 4));
}