-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpn_calculator.h
170 lines (139 loc) · 5.2 KB
/
rpn_calculator.h
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
#pragma once
#include <string>
#include <deque>
#include <stack>
#include <sstream>
#include <optional>
#include "nlohmann_json.hpp"
namespace RpnCalculator
{
enum class ButtonType
{
Digit, // 0-9
DirectNumber, // Pi, e
Backspace, // <=
BinaryOperator, // +, -, *
UnaryOperator, // sin, cos, tan, log, ln, sqrt, x^2, floor
StackOperator, // Swap, Dup, Drop, Clear
Inv, // Inverse
DegRadGrad, // Degree, Radian, Gradian
Enter, // Enter
ScientificMode, // Scientific mode
};
struct CalculatorButton
{
std::string Label;
ButtonType Type = ButtonType::Digit;
bool IsDoubleWidth = false;
};
struct CalculatorButtonWithInverse
{
CalculatorButton Button;
std::optional<CalculatorButton> InverseButton = std::nullopt;
CalculatorButtonWithInverse(
const std::string& label,
ButtonType type,
const std::string& inverseLabel = "",
std::optional<ButtonType> inverseType = std::nullopt);
const CalculatorButton& GetCurrentButton(bool inverseMode) const;
};
struct CalculatorLayoutDefinition
{
/*
Here is how the calculator looks like:
* each button row has 4 buttons
* Enter is a special button that is double width
* The stack and error indicator is displayed on top
* Some buttons have an inverse mode
==============================
stack N-3
stack N-2
stack N-1
stack N
user input
error indicator
==============================
[Inv] [Deg] [Rad] [Grad]
[Pi] [sin] [cos] [tan]
[1/x] [log] [ln] [e^x]
[sqrt] [x^2] [floor] [y^x]
==============================
[Swap] [Dup] [Drop] [Clear]
[ Enter ] [E] [<=]
[7] [8] [9] [/]
[4] [5] [6] [*]
[1] [2] [3] [-]
[0] [.] [+/-] [+]
==============================
*/
CalculatorLayoutDefinition();
int DisplayedStackSize = 4;
int NbButtonsPerRow = 4;
int NbDecimals = 12;
std::vector<std::vector<CalculatorButtonWithInverse>>& GetButtons(bool scientificMode);
private:
std::vector<std::vector<CalculatorButtonWithInverse>> ButtonsBasicMode;
std::vector<std::vector<CalculatorButtonWithInverse>> ButtonsScientificMode;
};
struct CalculatorStack
{
std::deque<double> Stack;
std::stack<std::deque<double>> _undoStack;
size_t size() const { return Stack.size(); }
bool empty() const { return Stack.empty(); }
double back() const { return Stack.back();}
double operator[](int index) const { return Stack[index]; }
void push_back(double v) { Stack.push_back(v); }
void push_front(double v) { Stack.push_front(v); }
void pop_back() { Stack.pop_back(); }
void clear() { Stack.clear(); }
void undo() { if (!_undoStack.empty()) { Stack = _undoStack.top(); _undoStack.pop(); } }
void store_undo() {_undoStack.push(Stack); }
// Serialization
nlohmann::json to_json() const { nlohmann::json j; j["Stack"] = Stack; return j; }
void from_json(const nlohmann::json& j) { Stack = j["Stack"].get<std::deque<double>>(); }
};
enum class AngleUnitType
{
Deg, Rad, Grad
};
std::string to_string(AngleUnitType t);
class CalculatorState
{
public:
// public members (displayed by the application)
CalculatorLayoutDefinition LayoutDefinition;
bool InverseMode = false;
AngleUnitType AngleUnit = AngleUnitType::Deg;
std::string Input;
std::string ErrorMessage;
double StoredValue = 0.;
CalculatorStack Stack;
bool ScientificMode = false;
// callbacks
// callbacks for the UI
void OnCalculatorButton(const CalculatorButton& button);
// callbacks for the computer keyboard (transmit \b for backspace)
void OnComputerKey(char key);
// serialization
nlohmann::json to_json() const;
void from_json(const nlohmann::json& j);
private:
// private callback helpers
bool _stackInput();
void _onDigit(const std::string& digit);
void _onBinaryOperator(const std::string& cmd); // cmd is +, -, *, /, y^x
void _onComputerKeyMinus(); // if Input is not empty, add/remove a minus sign, else call the "-" operator
void _onBackspace();
void _onEnter();
void _onDirectNumber(const std::string& label);
void _onStackOperator(const std::string& cmd);
void _onUnaryOperator(const std::string& cmd);
void _onDegRadGrad(const std::string& cmd);
void _onInverse();
void _onPlusMinus();
// private angle helpers
double _toRadian(double v) const;
double _toCurrentAngleUnit(double radian) const;
};
}