Compare commits

..

No commits in common. "cf9ee27149e4690800fcdd7f37d2d67958d95ebe" and "378102901965085f79ee661447cfabf873bcf293" have entirely different histories.

1 changed files with 15 additions and 43 deletions

View File

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