From 5965a89979d9d10419d7bbd375ccb818adb2e2b3 Mon Sep 17 00:00:00 2001 From: vova-rs Date: Mon, 15 Jun 2026 20:47:39 +0300 Subject: [PATCH] basics arifmetic --- src/fraction/frac.rs | 78 +++++++++++++++++++++++++++++++++++++++----- src/fraction/gcd.rs | 41 ++++++++++++++++++++--- src/main.rs | 11 ++++--- 3 files changed, 113 insertions(+), 17 deletions(-) diff --git a/src/fraction/frac.rs b/src/fraction/frac.rs index 08d6923..ae78cb3 100644 --- a/src/fraction/frac.rs +++ b/src/fraction/frac.rs @@ -1,16 +1,34 @@ 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{ - num: u32, - den: u32, + num: i32, + den: i32, } 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 { - num: n, - den: d, + num: n/common, + den: d/common, + } + } + pub fn zero() -> Self { + Self { + num: 0, + den: 1 } } } @@ -20,9 +38,53 @@ impl Add for Frac { type Output = 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 { - num: self.num * other.den + other.num * self.den, - den: self.den * other.den, + num: -self.num, + den: self.den, } } } diff --git a/src/fraction/gcd.rs b/src/fraction/gcd.rs index ea838b5..ebc68d5 100644 --- a/src/fraction/gcd.rs +++ b/src/fraction/gcd.rs @@ -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 b == 0 { return a } 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(); - while b != 0 { + loop { b >>= b.trailing_zeros(); if a > b { @@ -14,7 +45,9 @@ pub fn gcd(mut a: u64, mut b: u64) -> u64 { } b -= a; - } - a << shift + if b == 0 { + return a << shift; + } + } } diff --git a/src/main.rs b/src/main.rs index 93fd50f..5c30ef1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,11 @@ use fraction::Frac; fn main() { - let a = Frac::new(14, 200); - let b = Frac::new(1, 10); + let a = Frac::new(1, 4); + let b = Frac::new(6, 5); - let c = a + b; - - println!("c = {:?}", c); + println!("{:?} + {:?} = {:?}", a.clone(), b.clone(), a.clone() + b.clone()); + println!("{:?} - {:?} = {:?}", a.clone(), b.clone(), a.clone() - b.clone()); + println!("{:?} / {:?} = {:?}", a.clone(), b.clone(), a.clone() / b.clone()); + println!("{:?} * {:?} = {:?}", a.clone(), b.clone(), a.clone() * b.clone()); }