From 01c9a72de09207d1775fd22ae8ab728c4b9ac81d Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Apr 2024 19:10:34 +0200 Subject: [PATCH] Es 04 Rationals - added the + and * traits --- src/es04_rational_traits.rs | 47 +++++++++++++++++++++++++++++++ src/lib.rs | 1 + tests/es04_rational_traits.rs | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/es04_rational_traits.rs create mode 100644 tests/es04_rational_traits.rs diff --git a/src/es04_rational_traits.rs b/src/es04_rational_traits.rs new file mode 100644 index 0000000..f7387e0 --- /dev/null +++ b/src/es04_rational_traits.rs @@ -0,0 +1,47 @@ +#![allow(dead_code)] + +use std::ops::Add; +use std::ops::Mul; + +use super::es02_rational::Rational; + +/** Es.4 + * Per la struttura dei numeri razionali implementare i traits Add e Mul sia per fare + * somma e moltiplicazione di numeri razionali che per fare somma e moltiplicazione di un numero razionale e un intero (i32). + * Aggiungere ai test che avete fatto altri test per queste implementazioni. + */ +impl Mul for Rational { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + let mut temp = rhs.clone(); + temp.multiplication(&self); + temp + } +} + +impl Mul for Rational { + type Output = Self; + fn mul(self, rhs: i32) -> Self::Output { + let mut temp = Rational::from(rhs); + temp.multiplication(&self); + temp + } +} + +impl Add for Rational { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + let mut temp = rhs.clone(); + temp.addition(&self); + temp + } +} + +impl Add for Rational { + type Output = Self; + fn add(self, rhs: i32) -> Self::Output { + let mut temp = Rational::from(rhs); + temp.addition(&self); + temp + } +} diff --git a/src/lib.rs b/src/lib.rs index 20f83b2..0278b59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod es01_anagram; pub mod es02_rational; pub mod es03_game; +pub mod es04_rational_traits; diff --git a/tests/es04_rational_traits.rs b/tests/es04_rational_traits.rs new file mode 100644 index 0000000..91b03d1 --- /dev/null +++ b/tests/es04_rational_traits.rs @@ -0,0 +1,52 @@ +use esercizi::es02_rational::Rational; + +#[test] +fn test_rational_traits() { + assert_eq!( + Rational::new(2, 3) * Rational::new(3, 2), + Rational::new(1, 1) + ); + assert_eq!( + Rational::new(4, 3) * Rational::new(5, 7), + Rational::new(20, 21) + ); + assert_eq!( + Rational::new(-3, 2) * Rational::new(7, 4), + Rational::new(-21, 8) + ); + assert_eq!( + Rational::new(-3, 2) * Rational::new(14, 14), + Rational::new(-3, 2) + ); + assert_eq!(Rational::new(2, 3) * 1, Rational::new(2, 3)); + assert_eq!(Rational::new(2, 3) * (-5), Rational::new(-10, 3)); + assert_eq!(Rational::new(2, 3) * 10, Rational::new(20, 3)); + + assert_eq!( + Rational::new(2, 3) + Rational::new(3, 2), + Rational::new(13, 6) + ); + assert_eq!( + Rational::new(5, 3) + Rational::new(5, 2), + Rational::new(25, 6) + ); + assert_eq!( + Rational::new(-3, 16) + Rational::new(5, -4), + Rational::new(-23, 16) + ); + assert_eq!( + Rational::from(100) + Rational::from(47), + Rational::from(147) + ); + assert_eq!( + Rational::new(23, 12) + + Rational::new(24, 15) + + Rational::new(23, 24) + + Rational::new(-437, 120), + Rational::new(5, 6) + ); + assert_eq!(Rational::new(-3, 2) + 0, Rational::new(-3, 2)); + assert_eq!(Rational::new(-3, 2) + 1, Rational::new(-1, 2)); + assert_eq!(Rational::new(-3, 2) + 5, Rational::new(7, 2)); + assert_eq!(Rational::new(-3, 2) + (-1), Rational::new(-5, 2)); +}