1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
use std::{
f64::consts::{E, PI},
fmt::{self, Display, Formatter},
};
mod input;
pub mod ui;
#[derive(Default)]
pub struct Calculator {
stack: Vec<f64>,
input_buffer: String,
angle_mode: AngleMode,
}
impl Calculator {
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;
}
};
self.stack.push(match op {
Operator::Add => lhs + rhs,
Operator::Sub => lhs - rhs,
Operator::Mul => lhs * rhs,
Operator::Div => lhs / rhs,
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 {
#[default]
Degrees,
Radians,
}
impl AngleMode {
fn toggle(&self) -> Self {
match self {
Self::Degrees => Self::Radians,
Self::Radians => Self::Degrees,
}
}
}
impl Display for AngleMode {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Degrees => "deg",
Self::Radians => "rad",
}
)
}
}
enum Operator {
Add,
Sub,
Mul,
Div,
Exp,
}
impl Operator {
fn parse(c: char) -> Result<Self, ParseOperatorError> {
match c {
'+' => Ok(Self::Add),
'-' => Ok(Self::Sub),
'*' => Ok(Self::Mul),
'/' => Ok(Self::Div),
'^' => Ok(Self::Exp),
_ => Err(ParseOperatorError(c)),
}
}
}
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)
}
impl Constant {
fn parse(s: &str) -> Result<Self, ParseConstantError> {
match s {
"pi" => Ok(Self::Pi),
"e" => Ok(Self::E),
_ => Err(ParseConstantError(s.to_string())),
}
}
fn value(&self) -> f64 {
match self {
Self::Pi => PI,
Self::E => E,
}
}
}
struct ParseConstantError(String);
pub enum Signal {
None,
Exit,
}
|