aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs59
1 files changed, 4 insertions, 55 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 039258c..db316f0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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)