diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-09 10:17:46 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-09 10:17:46 -0330 |
| commit | 676e90b57e3a7fe96aa393c59a897bccff0ab8ec (patch) | |
| tree | 158bbfb588962e0e38414a4699a0adc3a07b76d4 | |
| parent | 9e3c9eeef438ddd03fa541dd11cbdfed3b1a5394 (diff) | |
| download | pfc-676e90b57e3a7fe96aa393c59a897bccff0ab8ec.zip | |
sin function
| -rw-r--r-- | src/input.rs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/input.rs b/src/input.rs index 998d380..4aae623 100644 --- a/src/input.rs +++ b/src/input.rs @@ -39,9 +39,7 @@ impl Calculator { } fn push_to_buffer(&mut self, c: char) { - if c.is_ascii_digit() { - self.input_buffer.push(c); - } else if c == '.' && !self.input_buffer.contains('.') { + if c == '.' && !self.input_buffer.contains('.') { if self.input_buffer.len() == 0 { self.input_buffer.push('0'); } @@ -49,14 +47,25 @@ impl Calculator { } else if let Ok(op) = Operator::parse(c) { self.push_buffer_to_stack(); self.perform_operation(op); + } else { + self.input_buffer.push(c); } } fn push_buffer_to_stack(&mut self) { - if self.input_buffer.len() > 0 { - self.stack.push(self.input_buffer.parse::<f64>().unwrap()); - self.input_buffer = String::new(); + match self.input_buffer.as_str() { + "sin" => { + if let Some(f) = self.stack.last_mut() { + *f = f.sin(); + } + } + _ => { + if self.input_buffer.len() > 0 { + self.stack.push(self.input_buffer.parse::<f64>().unwrap()); + } + } } + self.input_buffer = String::new(); } fn swap(&mut self) { |