This commit is contained in:
ldlda committed 2024-10-24 21:37:37 +07:00
1 parent ffbfb5ba01
commit d0b0db714a
7 files changed
+195 -1

No files matched your search

Generated
+82
View File
@@ -2,6 +2,88 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "dsars"
version = "0.1.0"
dependencies = [
"num",
]
[[package]]
name = "num"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
num = "0.4.3"
+41
View File
@@ -0,0 +1,41 @@
pub mod complex_struct;
pub fn sphenic(n: i32) -> bool {
let mut num = n;
let mut count = 0;
'd: for i in 2..=n {
if count == 3 {
break 'd;
}
if num % i == 0 {
num /= i;
if num % i == 0 {
return false;
}
count += 1;
}
}
num == 1 && count == 3
}
pub fn int_swap(a: i32) -> Option<i32> {
if a < 0 {
return None;
};
if a == 0 {
return Some(0);
};
let len: i32 = 10_i32.pow(a.ilog10());
let first = a % 10;
let second = a / len;
let third = a % len - first;
Some(third + second + first * len)
}
pub fn find_max<T>(max: &mut T, n: &T)
where
T: num::Num + PartialOrd + Copy,
{
if n > max {
*max = *n;
}
}
+48
View File
@@ -0,0 +1,48 @@
use std::fmt::Display;
use std::ops::{Add, Mul};
use num::Num;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LdaComplex<S>
where
S: Num,
{
real: S,
imag: S,
}
impl<T: Copy + Num> LdaComplex<T> {
pub fn new(re: T, im: T) -> LdaComplex<T> {
LdaComplex { real: re, imag: im }
}
}
impl<T: Copy + Num> Add for LdaComplex<T> {
type Output = LdaComplex<T>;
fn add(self, other: Self) -> Self::Output {
Self {
real: self.real + other.real,
imag: self.imag + other.imag,
}
}
}
impl<T: Copy + Num> Mul for LdaComplex<T> {
type Output = LdaComplex<T>;
fn mul(self, other: Self) -> Self::Output {
Self {
real: self.real * other.real - self.imag * other.imag,
imag: self.real * other.imag + self.imag * other.real,
}
}
}
impl<T: Num + PartialOrd + Display> Display for LdaComplex<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Self { real: a, imag: b } if *a == T::zero() => write!(f, "{b}i"),
Self { real: a, imag: b } if *b == T::zero() => write!(f, "{a}"),
Self { real: a, imag: b } if *b < T::zero() => write!(f, "{a}{b}i"),
Self { real: a, imag: b } => write!(f, "{a}+{b}i")
}
}
}
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
pub mod lab1;
pub mod lab2;
+20 -1
View File
@@ -1,3 +1,22 @@
use dsars::lab1::complex_struct::*;
use dsars::lab1::int_swap;
use dsars::lab1::sphenic;
use dsars::lab1::find_max;
fn main() {
println!("Hello, world!");
let c = LdaComplex::<f32>::new(1., -15.);
let d = LdaComplex::<f32>::new(2., 5.);
println!("{}, {}", c + d, c * d);
println!("int swap to {}", int_swap(146623i32).unwrap());
for i in 1..=200 {
if sphenic(i) {
print!("{} ", i);
}
};
println!();
let mut max = 13;
let this = 14;
println!("{}, {}", max, this);
find_max(&mut max, &this);
println!("{}, {}", max, this);
}