pull/2/head
Mark 2023-04-08 20:40:35 -07:00
parent fb9cc03bb9
commit e9d4ec0b12
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 23 additions and 14 deletions

View File

@ -98,13 +98,13 @@ impl PreToken {
// Units
"m" => {
let mut u = Quantity::new_rational_from_string("1").unwrap();
let mut u = Quantity::new_rational(1f64).unwrap();
u.add_unit(BaseUnit::Meter, 1f64);
Token::Number(u)
},
"s" => {
let mut u = Quantity::new_rational_from_string("1").unwrap();
let mut u = Quantity::new_rational(1f64).unwrap();
u.add_unit(BaseUnit::Second, 1f64);
Token::Number(u)
}

View File

@ -25,9 +25,9 @@ pub struct Quantity {
impl ToString for Quantity {
fn to_string(&self) -> String {
let n = self.v.to_string();
//n.push(' ');
//n.push_str(&u.to_string());
let mut n = self.v.to_string();
n.push(' ');
n.push_str(&self.u.to_string());
n
}
}
@ -130,10 +130,9 @@ impl Quantity {
}
pub fn pow(&self, pwr: Quantity) -> Quantity {
if !self.unitless() { panic!() }
Quantity {
v: self.v.pow(pwr.v),
u: self.u.clone()
u: self.u.pow(2f64)
}
}
}

View File

@ -58,10 +58,12 @@ impl Unit {
};
}
pub fn pow(&mut self, pwr: f64) {
for (_, p) in &mut self.val {
pub fn pow(&self, pwr: f64) -> Unit {
let mut u = self.clone();
for (_, p) in &mut u.val {
*p *= pwr;
}
};
return u;
}
}

View File

@ -328,19 +328,27 @@ impl Operator{
if let Token::Number(v) = args {
if v.is_zero() { return Err(()); }
return Ok(Token::Number(Quantity::new_rational(1f64).unwrap()/v));
return Ok(Token::Number(
Quantity::new_rational(1f64).unwrap()/v
));
} else { panic!(); }
},
Operator::Add => {
let mut sum = Quantity::new_rational(0f64).unwrap();
for i in args.iter() {
let j = i.as_number();
let mut sum;
if let Token::Number(s) = args[0].as_number() {
sum = s;
} else {panic!()};
let mut i: usize = 1;
while i < args.len() {
let j = args[i].as_number();
if let Token::Number(v) = j {
sum += v;
} else {
panic!();
}
i += 1;
}
return Ok(Token::Number(sum));
},