1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[cfg(test)]
mod unit_test;
use crate::data_structure::ListStack;
// Simulating a relatively simple calcutor
// implemented with the Djikstra two-stack algorithm
#[derive(Debug)]
pub struct Calculator {
ops: ListStack<String>,
vals: ListStack<usize>,
}
impl Default for Calculator {
fn default() -> Self {
Self::new()
}
}
impl Calculator {
pub fn new() -> Self {
Self {
ops: ListStack::new(),
vals: ListStack::new(),
}
}
pub fn compute(&mut self, expression: String) -> usize {
// operations, parentheses and operands should be separated
// by white spaces, e.g. ( ( 1 * ( 2 + 3 ) ) + ( 4 * ( 5 + 6 ) ) )
for elt in expression.split_whitespace() {
let c = elt.to_string();
// println!("{:?}", self.vals);
// println!("{:?}", self.ops);
if c == "+" || c == "*" || c == ":" || c == "/" {
self.ops.push(c);
} else if c == ")" {
// Apply the last operation to the 2 last values
let op = self.ops.pop().expect("Failed poping last op");
let a = self.vals.pop().unwrap();
let b = self.vals.pop().unwrap();
if op == "+" {
// println!("{}", a + b);
self.vals.push(a + b);
} else if op == "*" {
// println!("{}", a * b);
self.vals.push(a * b);
} else if op == ":" || op == "/" {
self.vals.push(b / a);
}
} else if c == "(" {
} else {
self.vals.push(c.parse::<usize>().unwrap());
}
// println!("\n");
}
let res = self.vals.pop().expect("Failed poping result");
println!("{res}");
res
}
}