diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-03-13 21:32:05 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-03-13 21:32:05 -0230 |
| commit | 8756ace462ab94e540d53c633b7e76c21b7a6bcd (patch) | |
| tree | 4ae8e0025aa8c25af8f15164ba06dd98f9a6324b /src/lib.rs | |
| parent | c83c2315dd45bb0bc590995ea0899a0ebd1b59dc (diff) | |
| download | pfc-8756ace462ab94e540d53c633b7e76c21b7a6bcd.zip | |
inverse trig functions
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 59 |
1 files changed, 4 insertions, 55 deletions
@@ -3,9 +3,12 @@ use std::{ fmt::{self, Display, Formatter}, }; +mod function; mod input; pub mod ui; +pub use function::Function; + #[derive(Default)] pub struct Calculator { stack: Vec<f64>, @@ -35,41 +38,10 @@ impl Calculator { Operator::Exp => lhs.powf(rhs), }); } - - fn call_function(&mut self, func: Function) { - let mut val = match self.stack.pop() { - Some(v) => v, - None => { - return; - } - }; - self.stack.push(match func { - Function::Sin => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.sin() - } - Function::Cos => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.cos() - } - Function::Tan => { - if self.angle_mode == AngleMode::Degrees { - val = val.to_radians(); - } - val.tan() - } - Function::Deg => val.to_degrees(), - Function::Rad => val.to_radians(), - }); - } } #[derive(Default, Copy, Clone, PartialEq)] -enum AngleMode { +pub enum AngleMode { #[default] Degrees, Radians, @@ -120,29 +92,6 @@ impl Operator { struct ParseOperatorError(char); -enum Function { - Sin, // Sine - Cos, // Cosine - Tan, // Tangent - Deg, // Convert from radians to degrees - Rad, // Convert from degrees to radians -} - -impl Function { - fn parse(s: &str) -> Result<Self, ParseFunctionError> { - match s { - "sin" => Ok(Self::Sin), - "cos" => Ok(Self::Cos), - "tan" => Ok(Self::Tan), - "deg" => Ok(Self::Deg), - "rad" => Ok(Self::Rad), - _ => Err(ParseFunctionError(s.to_string())), - } - } -} - -struct ParseFunctionError(String); - enum Constant { Pi, // Archimedes’ constant (π) E, // Euler's number (e) |