Compare commits
3 Commits
3781029019
...
cf9ee27149
Author | SHA1 | Date |
---|---|---|
Alexandre Stein | cf9ee27149 | |
Alexandre Stein | 26601e548e | |
Alexandre Stein | 908726e3c6 |
|
@ -66,7 +66,6 @@ impl TmpNum {
|
|||
}
|
||||
// Returns the float number from the parsed input
|
||||
fn get(&mut self) -> f64 {
|
||||
println!("XXX {}", self.s);
|
||||
let n = self.s.parse::<f64>().ok().expect("number is malformed");
|
||||
self.s = String::new();
|
||||
|
||||
|
@ -140,14 +139,17 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
|||
// Send it to the parse function
|
||||
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
|
||||
// Do the calculation
|
||||
let tmp_result = calculation(&tmp_symbols, &tmp_numbers);
|
||||
let tmp_result = calculation(tmp_symbols, tmp_numbers);
|
||||
// Add the calculated value to the list of numbers
|
||||
numbers.push(tmp_result);
|
||||
// Change the position in the list
|
||||
i = i2;
|
||||
|
||||
// Check the symbol after the closing bracket
|
||||
let c3 = pattern.chars().nth(i2 + 1).expect("some char");
|
||||
let c3 = match pattern.chars().nth(i2 + 1){
|
||||
Some(c) => c,
|
||||
None => break,
|
||||
};
|
||||
if !c3.is_numeric() {
|
||||
let s2 = match c3 {
|
||||
'-' => Some(Symbol::Minus),
|
||||
|
@ -211,16 +213,17 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
|||
}
|
||||
|
||||
// Run the calculation based on the 2 different lists of symbols and numbers
|
||||
fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
|
||||
fn calculation(symbols: Vec<Symbol>, numbers: Vec<f64>) -> f64 {
|
||||
let mut counter = 0;
|
||||
let mut n1 = *numbers.get(0).expect("expect at least the first value");
|
||||
|
||||
// Iterate the list of symbols
|
||||
|
||||
while counter < symbols.len() {
|
||||
let s = symbols
|
||||
.get(counter)
|
||||
.expect("expect at least a symbol value");
|
||||
let n2 = *numbers
|
||||
let mut n2 = *numbers
|
||||
.get(counter + 1)
|
||||
.expect("expect at least a second value");
|
||||
|
||||
|
@ -248,7 +251,7 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
|
|||
.expect("during the priority, we expect at least the first value");
|
||||
while counter_2 < symbols.len() {
|
||||
let nn2 = *numbers
|
||||
.get(counter_2)
|
||||
.get(counter_2 + 1)
|
||||
.expect("during the priority, we expect at least the second value");
|
||||
|
||||
if ss.priority() {
|
||||
|
@ -265,12 +268,14 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
|
|||
// Change the position in the list, save the new value as n1 and stop the priority loop
|
||||
} else {
|
||||
counter = counter_2;
|
||||
n1 = nn1;
|
||||
n2 = nn1;
|
||||
break;
|
||||
}
|
||||
n2 = nn1;
|
||||
|
||||
// Increment the position in the list
|
||||
counter_2 += 1;
|
||||
counter = counter_2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -319,7 +324,7 @@ fn main() {
|
|||
process::exit(2);
|
||||
}
|
||||
|
||||
let res = calculation(&symbols, &numbers);
|
||||
let res = calculation(symbols, numbers);
|
||||
println!("{res}");
|
||||
}
|
||||
|
||||
|
@ -330,23 +335,46 @@ mod tests {
|
|||
#[test]
|
||||
fn basics() {
|
||||
let (s, n) = parse("2+2".to_string());
|
||||
assert_eq!(calculation(&s, &n), 4.0);
|
||||
|
||||
assert_eq!(calculation(s, n), 4.0);
|
||||
let (s, n) = parse("4-9".to_string());
|
||||
assert_eq!(calculation(&s, &n), -5.0);
|
||||
assert_eq!(calculation(s, n), -5.0);
|
||||
let (s, n) = parse("7*3".to_string());
|
||||
assert_eq!(calculation(&s, &n), 21.0);
|
||||
assert_eq!(calculation(s, n), 21.0);
|
||||
let (s, n) = parse("9/3".to_string());
|
||||
assert_eq!(calculation(&s, &n), 3.0);
|
||||
assert_eq!(calculation(s, n), 3.0);
|
||||
|
||||
let (s, n) = parse("15*20".to_string());
|
||||
assert_ne!(calculation(&s, &n), 0.0);
|
||||
assert_ne!(calculation(s, n), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signs_2() {
|
||||
let (s, n) = parse("2*4+4".to_string());
|
||||
assert_eq!(calculation(s, n), 12.0);
|
||||
let (s, n) = parse("2+2*4".to_string());
|
||||
assert_eq!(calculation(&s, &n), 10.0);
|
||||
assert_eq!(calculation(s, n), 10.0);
|
||||
let (s, n) = parse("30/4-9".to_string());
|
||||
assert_eq!(calculation(&s, &n), -1.5);
|
||||
assert_eq!(calculation(s, n), -1.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_number() {
|
||||
let (s, n) = parse("-2*4+4".to_string());
|
||||
assert_eq!(calculation(s, n), -4.0);
|
||||
let (s, n) = parse("-2+2*4".to_string());
|
||||
assert_eq!(calculation(s, n), 6.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_1_bracket_set() {
|
||||
let (s, n) = parse("2*(4+4)*2".to_string());
|
||||
assert_eq!(calculation(s, n), 32.0);
|
||||
let (s, n) = parse("2*(4+4)".to_string());
|
||||
assert_eq!(calculation(s, n), 16.0);
|
||||
let (s, n) = parse("-2*(4+4)".to_string());
|
||||
assert_eq!(calculation(s, n), -16.0);
|
||||
let (s, n) = parse("(4+4)*2".to_string());
|
||||
assert_eq!(calculation(s, n), 16.0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue