文章目录
- 一、说明
- 二、源码
- 三、运行和调试结果
一、说明
长度的国际单位是“米”(符号“m”),常用单位有毫米(mm)、厘米(cm)、分米(dm)、千米(km)、米(m)、微米(μm)、纳米(nm)等等。英寸等英制单位在国内有少量使用,我们继续在程序中体现它们的存在。
工程设计中,最多使用的单位是毫米(mm)。我选择米(m)作为长度类的基准单位。理由有2点:1、米(m)是国际标准单位;2、我使用AutoCAD多年,制图时对长度单位的选择并不敏感,默认长度无量纲(无单位),等标注时通过调整标注比例确定实际长度。
备注:长度类的主要应用场景在尺寸标注和单位换算。
二、源码
/*
* 长度米的类m
* 编制人: $ource
* 修改版次及创建时间:0版完成版(2021年8月24日)
* 修改内容及时间:2024年9月3日
* 待完善问题:无
*/
use std::ops::{Add, Sub, Mul, Div};
//长度单位枚举
pub enum LengthUnit {
m, //米 Meter
dm, //分米 Decimeter: 0.1m */
cm, //厘米 Centimeter: 0.01m
mm, //毫米 Millimeter=0.001m
Micron, //微米 Micron: 10^-6m μm
nm, //纳米 Nanometer: 10^-9m
km, //千米 Kilometer: 1000m
Inch, //英寸 Inch '
Foot, //英尺 Foot= 12 Inches "
Other(f64), //其它 No unit
}
#[derive(Debug,PartialEq,PartialOrd,Clone,Copy)]
pub struct Length{
pub m:f64
}
impl Length{
pub fn new(m:f64)->Self{
Length{m}
}
pub fn from(unit:LengthUnit,value:f64)->Self{
match unit{
LengthUnit::m => {Length{m:value}}
LengthUnit::dm => {Length{m:value*0.1f64}}
LengthUnit::cm => {Length{m:value*0.01f64}}
LengthUnit::mm => {Length{m:value*0.001f64}}
LengthUnit::Micron => {Length{m:value*1e-6f64}}
LengthUnit::nm => {Length{m:value*1e-9f64}}
LengthUnit::km => {Length{m:value*1e3f64}}
LengthUnit::Inch => {Length{m:value*0.0254f64}}
LengthUnit::Foot => {Length{m:value*0.3048f64}}
LengthUnit::Other(s) => {Length{m:value*s}}
}
}
pub fn to(&self,unit:LengthUnit)->f64{
match unit{
LengthUnit::m => {self.m}
LengthUnit::dm => {self.m/0.1f64}
LengthUnit::cm => {self.m/0.01f64}
LengthUnit::mm => {self.m/0.001f64}
LengthUnit::Micron => {self.m/1e-6f64}
LengthUnit::nm => {self.m/1e-9f64}
LengthUnit::km => {self.m/1e3f64}
LengthUnit::Inch => {self.m/0.0254f64}
LengthUnit::Foot => {self.m/0.3048f64}
LengthUnit::Other(s) => {self.m/s}
}
}
pub fn set_m(&mut self, m:f64){
self.m=m;
}
pub fn get_m(&self)->f64{
self.m
}
}
//运算符重载 + - * /,实现加减及比例缩放。
impl Add<Length> for Length{//加法
type Output = Self;
fn add(self, other: Self) ->Length{
Self{m:self.m + other.m}
}
}
impl Sub<Length> for Length{//减法
type Output = Self;
fn sub(self, other: Self) ->Length{
Self{m:self.m - other.m}
}
}
impl Mul<f64> for Length{//比例缩放
type Output = Self;
fn mul(self, scale:f64) -> Length {
Length::new(self.m * scale)
}
}
impl Div<f64> for Length{//比例缩放
type Output = Self;
fn div(self, scale:f64) -> Length {
Length::new(self.m / scale)
}
}
pub const LENGTH_MAX:Length=Length{m:1.0E+10f64};
pub const LENGTH_MIN:Length=Length{m:1.0E-10f64};
pub const LENGTH0:Length=Length{m:0f64};
#[cfg(test)] //cfg 属性负责控制条件编译,并仅会在谓词为 true 时编译其所附带的内容。cargo test 时满足运行条件
mod tests {
use super::*;
#[test]
fn length_test() {
assert_eq!(LENGTH0.m,0f64);
assert_eq!(Length::new(0f64),LENGTH0);
assert_eq!(Length::from(LengthUnit::m,1f64).to(LengthUnit::m),1f64);
assert_eq!(Length::from(LengthUnit::cm,1f64).to(LengthUnit::cm),1f64);
assert_eq!(Length::from(LengthUnit::mm,1f64).to(LengthUnit::mm),1f64);
assert_eq!(Length::from(LengthUnit::Micron,1f64).to(LengthUnit::Micron),1f64);
assert_eq!(Length::from(LengthUnit::nm,1f64).to(LengthUnit::nm),1f64);
assert_eq!(Length::from(LengthUnit::km,1f64).to(LengthUnit::km),1f64);
assert_eq!(Length::from(LengthUnit::Inch,1f64).to(LengthUnit::Inch),1f64);
assert_eq!(Length::from(LengthUnit::Foot,1f64).to(LengthUnit::Foot),1f64);
assert_eq!(Length::from(LengthUnit::Other(11f64),2f64).to(LengthUnit::Other(22f64)),1f64);
assert_eq!(Length::new(1.0E+10f64),LENGTH_MAX);
assert_eq!(Length::new(1.0E-10f64),LENGTH_MIN);
assert_eq!(Length::new(1f64)+Length::new(2f64),Length::new(4f64)-Length::new(1f64));
assert_eq!(Length::new(1f64)*2f64,Length::new(4f64)/2f64);
}
}