diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-11 11:43:39 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-11 11:43:39 -0330 |
| commit | a91472045233160b667ac5112c4a85bfed4f9a95 (patch) | |
| tree | 836dcf4ac5f7a9e5ba5ceab79f7283173a5ebd34 | |
| parent | 0d9bc65be5db6a2a669c9b6b979ed79e0ecfbf9a (diff) | |
| download | pfc-a91472045233160b667ac5112c4a85bfed4f9a95.zip | |
trig functions for radians & degrees
| -rw-r--r-- | src/input.rs | 14 | ||||
| -rw-r--r-- | src/lib.rs | 27 |
2 files changed, 25 insertions, 16 deletions
diff --git a/src/input.rs b/src/input.rs index ab4a1d1..2f9d820 100644 --- a/src/input.rs +++ b/src/input.rs @@ -12,17 +12,17 @@ impl Calculator { _ => {} }, KeyModifiers::SHIFT => match key.code { - KeyCode::Char('D') => self.clear(), - _ => {} - }, - KeyModifiers::NONE => match key.code { - KeyCode::Char('q') => { + KeyCode::Char('Q') => { return Signal::Exit; } - KeyCode::Char('j' | 'k') => self.swap(), - KeyCode::Char('d') => { + KeyCode::Char('J' | 'K') => self.swap(), + KeyCode::Char('D') => { self.input_buffer = String::new(); } + KeyCode::Char('C') => self.clear(), + _ => {} + }, + KeyModifiers::NONE => match key.code { KeyCode::Char(c) => self.push_to_buffer(c), KeyCode::Backspace => { self.input_buffer.pop(); @@ -55,26 +55,35 @@ impl Operator { struct ParseOperatorError(char); enum Function { - Sin, - Cos, - Tan, + DSin, // Sine (degrees) + DCos, // Cosine (degrees) + DTan, // Tangent (degrees) + RSin, // Sine (radians) + RCos, // Cosing (radians) + RTan, // Tangent (radians) } impl Function { fn parse(s: &str) -> Result<Self, ParseFunctionError> { match s { - "sin" => Ok(Self::Sin), - "cos" => Ok(Self::Cos), - "tan" => Ok(Self::Tan), + "dsin" => Ok(Self::DSin), + "dcos" => Ok(Self::DCos), + "dtan" => Ok(Self::DTan), + "rsin" => Ok(Self::RSin), + "rcos" => Ok(Self::RCos), + "rtan" => Ok(Self::RTan), _ => Err(ParseFunctionError(s.to_string())), } } fn call_on(&self, f: f64) -> f64 { match self { - Self::Sin => f.sin(), - Self::Cos => f.cos(), - Self::Tan => f.tan(), + Self::DSin => f.to_radians().sin(), + Self::DCos => f.to_radians().cos(), + Self::DTan => f.to_radians().tan(), + Self::RSin => f.sin(), + Self::RCos => f.cos(), + Self::RTan => f.tan(), } } } |