From c0b72c71cccf2ade57571af040473de6cfef0338 Mon Sep 17 00:00:00 2001 From: vova-rs Date: Fri, 12 Jun 2026 15:06:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fraction/frac.rs | 28 ++++++++++++++++++++++++++++ src/fraction/gcd.rs | 20 ++++++++++++++++++++ src/fraction/mod.rs | 4 ++++ src/main.rs | 11 ++++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/fraction/frac.rs create mode 100644 src/fraction/gcd.rs create mode 100644 src/fraction/mod.rs diff --git a/src/fraction/frac.rs b/src/fraction/frac.rs new file mode 100644 index 0000000..08d6923 --- /dev/null +++ b/src/fraction/frac.rs @@ -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, + } + } +} diff --git a/src/fraction/gcd.rs b/src/fraction/gcd.rs new file mode 100644 index 0000000..ea838b5 --- /dev/null +++ b/src/fraction/gcd.rs @@ -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 +} diff --git a/src/fraction/mod.rs b/src/fraction/mod.rs new file mode 100644 index 0000000..5e2d5dd --- /dev/null +++ b/src/fraction/mod.rs @@ -0,0 +1,4 @@ +mod frac; +pub use frac::*; +mod gcd; +pub use gcd::*; diff --git a/src/main.rs b/src/main.rs index e7a11a9..93fd50f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); }