Es 04 Rationals
- added the + and * traits
This commit is contained in:
47
src/es04_rational_traits.rs
Normal file
47
src/es04_rational_traits.rs
Normal file
@@ -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<i32> 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<i32> for Rational {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: i32) -> Self::Output {
|
||||
let mut temp = Rational::from(rhs);
|
||||
temp.addition(&self);
|
||||
temp
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod es01_anagram;
|
||||
pub mod es02_rational;
|
||||
pub mod es03_game;
|
||||
pub mod es04_rational_traits;
|
||||
|
||||
Reference in New Issue
Block a user