diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-01 19:50:32 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-01 19:50:32 -0330 |
| commit | 6e6e7581b4c8575dd379f20a786108ce4edcd9c8 (patch) | |
| tree | 5b1ecf5b07d62e25648a23dd1a2d43260b2ff365 /src/lib.rs | |
| parent | dfbaad1f881be6ca3e93e09da360652ba5023b7d (diff) | |
| download | pfc-6e6e7581b4c8575dd379f20a786108ce4edcd9c8.zip | |
refactor
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -1,5 +1,40 @@ +mod input; pub mod ui; +#[derive(Default)] +pub struct Calculator { + stack: Vec<f64>, + input_buffer: String, +} + +impl Calculator { + fn push_buffer_to_stack(&mut self) { + self.stack.push(self.input_buffer.parse::<f64>().unwrap()); + self.input_buffer = String::new(); + } + + fn perform_operation(&mut self, op: Operator) { + let rhs = match self.stack.pop() { + Some(f) => f, + None => { + return; + } + }; + let lhs = match self.stack.pop() { + Some(f) => f, + None => { + return; + } + }; + match op { + Operator::Add => self.stack.push(lhs + rhs), + Operator::Sub => self.stack.push(lhs - rhs), + Operator::Mul => self.stack.push(lhs * rhs), + Operator::Div => self.stack.push(lhs / rhs), + } + } +} + pub enum Operator { Add, Sub, @@ -20,3 +55,8 @@ impl Operator { } pub struct ParseOperatorError(char); + +pub enum Signal { + None, + Exit, +} |