-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.h
More file actions
266 lines (232 loc) · 6.83 KB
/
controller.h
File metadata and controls
266 lines (232 loc) · 6.83 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#pragma once
#include <string>
#include <sstream>
#include "calculator.h"
#include "mainwindow.h"
#include "rational.h"
template<class T>
std::string ToString(T number) {
std::ostringstream tmp_str;
tmp_str << number;
return tmp_str.str();
}
template<class T>
T FromString(const std::string& number) {
std::istringstream tmp_str{number};
T result{};
tmp_str >> result;
return result;
}
inline std::string ToString(std::uint8_t number) {
std::ostringstream tmp_str;
tmp_str << +number;
return tmp_str.str();
}
template<>
std::uint8_t FromString<std::uint8_t>(const std::string& number) {
std::istringstream tmp_str{number};
unsigned result{};
tmp_str >> result;
return static_cast<std::uint8_t>(result);
}
template<class Number>
class Controller
{
public:
Controller() {}
void BindWithMainWindow(MainWindow* main_window) {
view_ = main_window;
if (!view_) {
return;
}
view_->SetDigitKeyCallback([this](int key){
PressDigitKey(key);
});
view_->SetProcessOperationKeyCallback([this](Operation key){
ProcessOperationKey(key);
});
view_->SetProcessControlKeyCallback([this](ControlKey key){
ProcessControlKey(key);
});
RevealView();
}
private:
void PressDigitKey(int digit) {
AddChar(digit + '0');
}
void ProcessOperationKey(Operation operation) {
switch(operation) {
case Operation::ADDITION:
OnOperation([this](Number x){return calculator_.Add(x);}, " + ");
break;
case Operation::SUBTRACTION:
OnOperation([this](Number x){return calculator_.Sub(x);}, " − ");
break;
case Operation::MULTIPLICATION:
OnOperation([this](Number x){return calculator_.Mul(x);}, " × ");
break;
case Operation::DIVISION:
OnOperation([this](Number x){return calculator_.Div(x);}, " ÷ ");
break;
case Operation::POWER:
OnOperation([this](Number x){return calculator_.Pow(x);}, " ^ ");
break;
}
}
void ProcessControlKey(ControlKey key) {
switch(key) {
case ControlKey::EQUALS: {
if (!operation_) {
return;
}
std::string formula = ToString(calculator_.GetNumber()) + operation_name_ + ToString(active_number_) + " = ";
auto error = operation_(active_number_);
if (error.has_value()) {
SetErrorInView(error.value());
break;
}
SetFormulaInView(formula);
SetInputAsNumber(calculator_.GetNumber());
operation_ = {};
break;
}
case ControlKey::CLEAR:
SetInputAsNumber(Number{});
SetFormulaInView("");
operation_ = {};
break;
case ControlKey::MEM_SAVE:
mem_ = active_number_;
SetMemInView("M");
break;
case ControlKey::MEM_LOAD:
if (!mem_.has_value()) {
return;
}
SetInputAsNumber(mem_.value());
SetFormulaInView("");
break;
case ControlKey::MEM_CLEAR:
mem_ = std::nullopt;
SetMemInView("");
break;
case ControlKey::PLUS_MINUS:
if (input_as_number_) {
active_number_ = -active_number_;
SetInputAsNumber(active_number_);
} else {
if (input_.size() && input_.front() == '-') {
SetInputAsString(input_.substr(1));
} else {
SetInputAsString("-" + input_);
}
}
break;
case ControlKey::BACKSPACE:
if (input_.size() > 0) {
SetInputAsString(input_.substr(0, input_.size() - 1));
}
break;
case ControlKey::EXTRA_KEY:
DoExtraAction();
break;
}
}
std::optional<std::string> GetExtraKey() const {
if constexpr (std::is_integral_v<Number>) {
return std::nullopt;
}
if constexpr (std::is_same_v<Number, Rational>) {
return "/";
}
return ".";
}
void OnOperation(std::function<std::optional<Error> (Number)> action, std::string text) {
if (!operation_) {
calculator_.Set(active_number_);
}
operation_name_ = text;
operation_ = action;
input_ = "";
SetFormulaInView(ToString(calculator_.GetNumber()) + text);
}
void SetInputAsString(const std::string& new_input) {
input_as_number_ = false;
input_ = new_input;
active_number_ = FromString<Number>(input_);
UpdateInputInView(new_input);
}
void SetInputAsNumber(Number number) {
input_as_number_ = true;
input_ = "";
active_number_ = number;
UpdateInputInView(ToString(active_number_));
}
void AddChar(char character) {
SetInputAsString(input_ + character);
}
void DoExtraAction() {
if constexpr (std::is_integral_v<Number>) {
return;
}
if constexpr (std::is_same_v<Number, Rational>) {
if (input_.size() == 0 || input_.find('/') != std::string::npos) {
return;
}
AddChar('/');
} else {
if (input_.find('.') != std::string::npos) {
return;
}
AddChar('.');
}
}
void UpdateInputInView(const std::string& string) {
if (!view_) {
return;
}
view_->SetInputText(string);
text_ = string;
}
void SetErrorInView(const std::string& string) {
if (!view_) {
return;
}
view_->SetErrorText(string);
}
void SetFormulaInView(const std::string& string) {
if (!view_) {
return;
}
view_->SetFormulaText(string);
formula_ = string;
}
void SetMemInView(const std::string& string) {
if (!view_) {
return;
}
view_->SetMemText(string);
mem_text_ = string;
}
void RevealView() {
if (!view_) {
return;
}
view_->SetInputText(text_);
view_->SetFormulaText(formula_);
view_->SetMemText(mem_text_);
view_->SetExtraKey(GetExtraKey());
}
private:
std::function<std::optional<Error>(Number number)> operation_;
std::string operation_name_;
Calculator<Number> calculator_;
Number active_number_ = {};
std::string input_;
MainWindow* view_ = nullptr;
std::optional<Number> mem_ = {};
bool input_as_number_ = true;
std::string text_;
std::string formula_;
std::string mem_text_;
};