basics arifmetic
This commit is contained in:
1 parent
c0b72c71cc
commit
5965a89979
3 files changed
+113
-17
No files matched your search
+70
-8
@@ -1,16 +1,34 @@
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
use std::ops::Sub;
|
||||||
|
use std::ops::Mul;
|
||||||
|
use std::ops::Div;
|
||||||
|
use std::ops::Neg;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
use super::gcd;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct Frac{
|
pub struct Frac{
|
||||||
num: u32,
|
num: i32,
|
||||||
den: u32,
|
den: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Frac {
|
impl Frac {
|
||||||
pub fn new(n: u32, d: u32) -> Self {
|
pub fn new(n: i32, d: i32) -> Self {
|
||||||
|
let common = gcd(n, d);
|
||||||
|
|
||||||
|
if common == 0 {
|
||||||
|
return Frac::zero();
|
||||||
|
}
|
||||||
|
|
||||||
Frac {
|
Frac {
|
||||||
num: n,
|
num: n/common,
|
||||||
den: d,
|
den: d/common,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn zero() -> Self {
|
||||||
|
Self {
|
||||||
|
num: 0,
|
||||||
|
den: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,9 +38,53 @@ impl Add for Frac {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
|
Frac::new(
|
||||||
|
self.num * other.den + other.num * self.den,
|
||||||
|
self.den * other.den
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for Frac {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn sub(self, other: Self) -> Self::Output {
|
||||||
|
Frac::new(
|
||||||
|
self.num * other.den + -other.num * self.den,
|
||||||
|
self.den * other.den
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul for Frac {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, other: Self) -> Self::Output {
|
||||||
|
Frac::new(
|
||||||
|
self.num * other.num,
|
||||||
|
self.den * other.den
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div for Frac {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn div(self, other: Self) -> Self::Output {
|
||||||
|
Frac::new(
|
||||||
|
self.num * other.den,
|
||||||
|
self.den * other.num
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Neg for Frac {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn neg(self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
num: self.num * other.den + other.num * self.den,
|
num: -self.num,
|
||||||
den: self.den * other.den,
|
den: self.den,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+37
-4
@@ -1,12 +1,43 @@
|
|||||||
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
|
/*pub fn gcd(a: u32, b: u32) -> u32 {
|
||||||
if a == 0 { return b }
|
if a == 0 { return b }
|
||||||
if b == 0 { return a }
|
if b == 0 { return a }
|
||||||
|
|
||||||
let shift = (a | b).trailing_zeros();
|
let shift = (a | b).trailing_zeros();
|
||||||
|
|
||||||
|
let a = a >> a.trailing_zeros();
|
||||||
|
|
||||||
|
gcd_loop(a, b, shift)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gcd_loop(a: u32, mut b: u32, shift: u32) -> u32 {
|
||||||
|
if b == 0 {
|
||||||
|
return a << shift;
|
||||||
|
}
|
||||||
|
|
||||||
|
b >>= b.trailing_zeros();
|
||||||
|
|
||||||
|
if a > b {
|
||||||
|
gcd_loop(b, a - b, shift)
|
||||||
|
} else {
|
||||||
|
gcd_loop(a, b - a, shift)
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub fn gcd(a: i32, b: i32) -> i32 {
|
||||||
|
let mut a = a.abs();
|
||||||
|
let mut b = b.abs();
|
||||||
|
|
||||||
|
if a == 0 { return b; }
|
||||||
|
if b == 0 { return a; }
|
||||||
|
|
||||||
|
let shift = (a | b).trailing_zeros();
|
||||||
|
|
||||||
a >>= a.trailing_zeros();
|
a >>= a.trailing_zeros();
|
||||||
|
|
||||||
while b != 0 {
|
loop {
|
||||||
b >>= b.trailing_zeros();
|
b >>= b.trailing_zeros();
|
||||||
|
|
||||||
if a > b {
|
if a > b {
|
||||||
@@ -14,7 +45,9 @@ pub fn gcd(mut a: u64, mut b: u64) -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
b -= a;
|
b -= a;
|
||||||
}
|
|
||||||
|
|
||||||
a << shift
|
if b == 0 {
|
||||||
|
return a << shift;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+6
-5
@@ -3,10 +3,11 @@ use fraction::Frac;
|
|||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = Frac::new(14, 200);
|
let a = Frac::new(1, 4);
|
||||||
let b = Frac::new(1, 10);
|
let b = Frac::new(6, 5);
|
||||||
|
|
||||||
let c = a + b;
|
println!("{:?} + {:?} = {:?}", a.clone(), b.clone(), a.clone() + b.clone());
|
||||||
|
println!("{:?} - {:?} = {:?}", a.clone(), b.clone(), a.clone() - b.clone());
|
||||||
println!("c = {:?}", c);
|
println!("{:?} / {:?} = {:?}", a.clone(), b.clone(), a.clone() / b.clone());
|
||||||
|
println!("{:?} * {:?} = {:?}", a.clone(), b.clone(), a.clone() * b.clone());
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user