начало положено
This commit is contained in:
1 parent
628fee3956
commit
c0b72c71cc
4 files changed
+62
-1
No files matched your search
@@ -0,0 +1,28 @@
|
||||
use std::ops::Add;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Frac{
|
||||
num: u32,
|
||||
den: u32,
|
||||
}
|
||||
|
||||
impl Frac {
|
||||
pub fn new(n: u32, d: u32) -> Self {
|
||||
Frac {
|
||||
num: n,
|
||||
den: d,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Add for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
num: self.num * other.den + other.num * self.den,
|
||||
den: self.den * other.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
|
||||
if a == 0 { return b }
|
||||
if b == 0 { return a }
|
||||
|
||||
let shift = (a | b).trailing_zeros();
|
||||
|
||||
a >>= a.trailing_zeros();
|
||||
|
||||
while b != 0 {
|
||||
b >>= b.trailing_zeros();
|
||||
|
||||
if a > b {
|
||||
core::mem::swap(&mut a, &mut b);
|
||||
}
|
||||
|
||||
b -= a;
|
||||
}
|
||||
|
||||
a << shift
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
mod frac;
|
||||
pub use frac::*;
|
||||
mod gcd;
|
||||
pub use gcd::*;
|
||||
+10
-1
@@ -1,3 +1,12 @@
|
||||
mod fraction;
|
||||
use fraction::Frac;
|
||||
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let a = Frac::new(14, 200);
|
||||
let b = Frac::new(1, 10);
|
||||
|
||||
let c = a + b;
|
||||
|
||||
println!("c = {:?}", c);
|
||||
}
|
||||
Reference in new issue
Block a user