forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorManager.cpp
776 lines (680 loc) · 27.4 KB
/
CalculatorManager.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <climits> // for UCHAR_MAX
#include "Header Files/CalcEngine.h"
#include "CalculatorManager.h"
#include "CalculatorResource.h"
using namespace std;
using namespace CalcEngine;
static constexpr size_t MAX_HISTORY_ITEMS = 20;
static constexpr size_t SERIALIZED_NUMBER_MINSIZE = 3;
#ifndef _MSC_VER
#define __pragma(x)
#endif
// Converts Memory Command enum value to unsigned char,
// while ignoring Warning C4309: 'conversion' : truncation of constant value
#define MEMORY_COMMAND_TO_UNSIGNED_CHAR(c) __pragma(warning(push)) __pragma(warning(disable : 4309)) static_cast<unsigned char>(c) __pragma(warning(pop))
namespace CalculationManager
{
CalculatorManager::CalculatorManager(_In_ ICalcDisplay* displayCallback, _In_ IResourceProvider* resourceProvider)
: m_displayCallback(displayCallback)
, m_currentCalculatorEngine(nullptr)
, m_resourceProvider(resourceProvider)
, m_inHistoryItemLoadMode(false)
, m_persistedPrimaryValue()
, m_isExponentialFormat(false)
, m_currentDegreeMode(Command::CommandNULL)
, m_savedDegreeMode(Command::CommandDEG)
, m_pStdHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
, m_pSciHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
{
CCalcEngine::InitialOneTimeOnlySetup(*m_resourceProvider);
}
/// <summary>
/// Call the callback function using passed in IDisplayHelper.
/// Used to set the primary display value on ViewModel
/// </summary>
/// <param name="text">wstring representing text to be displayed</param>
void CalculatorManager::SetPrimaryDisplay(_In_ const wstring& displayString, _In_ bool isError)
{
if (!m_inHistoryItemLoadMode)
{
m_displayCallback->SetPrimaryDisplay(displayString, isError);
}
}
void CalculatorManager::SetIsInError(bool isError)
{
m_displayCallback->SetIsInError(isError);
}
void CalculatorManager::DisplayPasteError()
{
m_currentCalculatorEngine->DisplayError(CALC_E_DOMAIN /*code for "Invalid input" error*/);
}
void CalculatorManager::MaxDigitsReached()
{
m_displayCallback->MaxDigitsReached();
}
void CalculatorManager::BinaryOperatorReceived()
{
m_displayCallback->BinaryOperatorReceived();
}
void CalculatorManager::MemoryItemChanged(unsigned int indexOfMemory)
{
m_displayCallback->MemoryItemChanged(indexOfMemory);
}
void CalculatorManager::InputChanged()
{
m_displayCallback->InputChanged();
}
/// <summary>
/// Call the callback function using passed in IDisplayHelper.
/// Used to set the expression display value on ViewModel
/// </summary>
/// <param name="expressionString">wstring representing expression to be displayed</param>
void CalculatorManager::SetExpressionDisplay(
_Inout_ shared_ptr<vector<pair<wstring, int>>> const& tokens,
_Inout_ shared_ptr<vector<shared_ptr<IExpressionCommand>>> const& commands)
{
if (!m_inHistoryItemLoadMode)
{
m_displayCallback->SetExpressionDisplay(tokens, commands);
}
}
/// <summary>
/// Callback from the CalculatorControl
/// Passed in string representations of memorized numbers get passed to the client
/// </summary>
/// <param name="memorizedNumber">vector containing wstring values of memorized numbers</param>
void CalculatorManager::SetMemorizedNumbers(_In_ const vector<wstring>& memorizedNumbers)
{
m_displayCallback->SetMemorizedNumbers(memorizedNumbers);
}
/// <summary>
/// Callback from the engine
/// </summary>
/// <param name="parenthesisCount">string containing the parenthesis count</param>
void CalculatorManager::SetParenthesisNumber(_In_ unsigned int parenthesisCount)
{
m_displayCallback->SetParenthesisNumber(parenthesisCount);
}
/// <summary>
/// Callback from the engine
/// </summary>
void CalculatorManager::OnNoRightParenAdded()
{
m_displayCallback->OnNoRightParenAdded();
}
/// <summary>
/// Reset CalculatorManager.
/// Set the mode to the standard calculator
/// Set the degree mode as regular degree (as oppose to Rad or Grad)
/// Clear all the entries and memories
/// Clear Memory if clearMemory parameter is true.(Default value is true)
/// </summary>
void CalculatorManager::Reset(bool clearMemory /* = true*/)
{
m_savedCommands.clear();
SetStandardMode();
if (m_scientificCalculatorEngine)
{
m_scientificCalculatorEngine->ProcessCommand(IDC_DEG);
m_scientificCalculatorEngine->ProcessCommand(IDC_CLEAR);
if (m_isExponentialFormat)
{
m_isExponentialFormat = false;
m_scientificCalculatorEngine->ProcessCommand(IDC_FE);
}
}
if (m_programmerCalculatorEngine)
{
m_programmerCalculatorEngine->ProcessCommand(IDC_CLEAR);
}
if (clearMemory)
{
this->MemorizedNumberClearAll();
}
}
/// <summary>
/// Change the current calculator engine to standard calculator engine.
/// </summary>
void CalculatorManager::SetStandardMode()
{
if (!m_standardCalculatorEngine)
{
m_standardCalculatorEngine =
make_unique<CCalcEngine>(false /* Respect Order of Operations */, false /* Set to Integer Mode */, m_resourceProvider, this, m_pStdHistory);
}
m_currentCalculatorEngine = m_standardCalculatorEngine.get();
m_currentCalculatorEngine->ProcessCommand(IDC_DEC);
m_currentCalculatorEngine->ProcessCommand(IDC_CLEAR);
m_currentCalculatorEngine->ChangePrecision(static_cast<int>(CalculatorPrecision::StandardModePrecision));
UpdateMaxIntDigits();
m_pHistory = m_pStdHistory.get();
}
/// <summary>
/// Change the current calculator engine to scientific calculator engine.
/// </summary>
void CalculatorManager::SetScientificMode()
{
if (!m_scientificCalculatorEngine)
{
m_scientificCalculatorEngine =
make_unique<CCalcEngine>(true /* Respect Order of Operations */, false /* Set to Integer Mode */, m_resourceProvider, this, m_pSciHistory);
}
m_currentCalculatorEngine = m_scientificCalculatorEngine.get();
m_currentCalculatorEngine->ProcessCommand(IDC_DEC);
m_currentCalculatorEngine->ProcessCommand(IDC_CLEAR);
m_currentCalculatorEngine->ChangePrecision(static_cast<int>(CalculatorPrecision::ScientificModePrecision));
m_pHistory = m_pSciHistory.get();
}
/// <summary>
/// Change the current calculator engine to scientific calculator engine.
/// </summary>
void CalculatorManager::SetProgrammerMode()
{
if (!m_programmerCalculatorEngine)
{
m_programmerCalculatorEngine =
make_unique<CCalcEngine>(true /* Respect Order of Operations */, true /* Set to Integer Mode */, m_resourceProvider, this, nullptr);
}
m_currentCalculatorEngine = m_programmerCalculatorEngine.get();
m_currentCalculatorEngine->ProcessCommand(IDC_DEC);
m_currentCalculatorEngine->ProcessCommand(IDC_CLEAR);
m_currentCalculatorEngine->ChangePrecision(static_cast<int>(CalculatorPrecision::ProgrammerModePrecision));
}
/// <summary>
/// Send command to the Calc Engine
/// Cast Command Enum to OpCode.
/// Handle special commands such as mode change and combination of two commands.
/// </summary>
/// <param name="command">Enum Command</command>
void CalculatorManager::SendCommand(_In_ Command command)
{
// When the expression line is cleared, we save the current state, which includes,
// primary display, memory, and degree mode
if (command == Command::CommandCLEAR || command == Command::CommandEQU || command == Command::ModeBasic || command == Command::ModeScientific
|| command == Command::ModeProgrammer)
{
switch (command)
{
case Command::ModeBasic:
this->SetStandardMode();
break;
case Command::ModeScientific:
this->SetScientificMode();
break;
case Command::ModeProgrammer:
this->SetProgrammerMode();
break;
default:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(command));
}
m_savedCommands.clear(); // Clear the previous command history
if (command != Command::CommandEQU && command != Command::CommandCLEAR)
{
m_savedCommands.push_back(MapCommandForSerialize(command));
}
m_savedDegreeMode = m_currentDegreeMode;
InputChanged();
return;
}
if (command == Command::CommandDEG || command == Command::CommandRAD || command == Command::CommandGRAD)
{
m_currentDegreeMode = command;
}
if (command != Command::CommandFE)
{
m_savedCommands.push_back(MapCommandForSerialize(command)); // Save the commands in the m_savedCommands
}
switch (command)
{
case Command::CommandASIN:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandSIN));
break;
case Command::CommandACOS:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCOS));
break;
case Command::CommandATAN:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandTAN));
break;
case Command::CommandPOWE:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandLN));
break;
case Command::CommandASINH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandSINH));
break;
case Command::CommandACOSH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCOSH));
break;
case Command::CommandATANH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandTANH));
break;
case Command::CommandASEC:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandSEC));
break;
case Command::CommandACSC:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCSC));
break;
case Command::CommandACOT:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCOT));
break;
case Command::CommandASECH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandSECH));
break;
case Command::CommandACSCH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCSCH));
break;
case Command::CommandACOTH:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandINV));
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(Command::CommandCOTH));
break;
case Command::CommandFE:
m_isExponentialFormat = !m_isExponentialFormat;
[[fallthrough]];
default:
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(command));
break;
}
InputChanged();
}
/// <summary>
/// Convert Command to unsigned char.
/// Since some Commands are higher than 255, they are saved after subtracting 255
/// The smallest Command is CommandSIGN = 80, thus, subtracted value does not overlap with other values.
/// </summary>
/// <param name="command">Enum Command</command>
unsigned char CalculatorManager::MapCommandForSerialize(Command command)
{
unsigned int commandToSave = static_cast<unsigned int>(command);
if (commandToSave > UCHAR_MAX)
{
commandToSave -= UCHAR_MAX;
}
return static_cast<unsigned char>(commandToSave);
}
/// <summary>
/// Convert Command to unsigned int
/// The command that is smaller than 80, CommandSIGN, can be converted back to original value by adding 255.
/// </summary>
/// <param name="command">unsigned char value represent the saved command</command>
unsigned int CalculatorManager::MapCommandForDeSerialize(unsigned char command)
{
unsigned int commandToLoad = command;
if (command < static_cast<unsigned int>(Command::CommandSIGN))
{
commandToLoad += UCHAR_MAX;
}
return commandToLoad;
}
/// <summary>
/// Load the persisted value that is saved in memory of CalcEngine
/// </summary>
void CalculatorManager::LoadPersistedPrimaryValue()
{
m_currentCalculatorEngine->PersistedMemObject(m_persistedPrimaryValue);
m_currentCalculatorEngine->ProcessCommand(IDC_RECALL);
InputChanged();
}
/// <summary>
/// Memorize the current displayed value
/// Notify the client with new the new memorize value vector
/// </summary>
void CalculatorManager::MemorizeNumber()
{
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizeNumber));
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
m_currentCalculatorEngine->ProcessCommand(IDC_STORE);
auto memoryObjectPtr = m_currentCalculatorEngine->PersistedMemObject();
if (memoryObjectPtr != nullptr)
{
m_memorizedNumbers.insert(m_memorizedNumbers.begin(), *memoryObjectPtr);
}
if (m_memorizedNumbers.size() > m_maximumMemorySize)
{
m_memorizedNumbers.resize(m_maximumMemorySize);
}
this->SetMemorizedNumbersString();
}
/// <summary>
/// Recall the memorized number.
/// The memorized number gets loaded to the primary display
/// </summary>
/// <param name="indexOfMemory">Index of the target memory</param>
void CalculatorManager::MemorizedNumberLoad(_In_ unsigned int indexOfMemory)
{
SaveMemoryCommand(MemoryCommand::MemorizedNumberLoad, indexOfMemory);
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
this->MemorizedNumberSelect(indexOfMemory);
m_currentCalculatorEngine->ProcessCommand(IDC_RECALL);
InputChanged();
}
/// <summary>
/// Do the addition to the selected memory
/// It adds primary display value to the selected memory
/// Notify the client with new the new memorize value vector
/// </summary>
/// <param name="indexOfMemory">Index of the target memory</param>
void CalculatorManager::MemorizedNumberAdd(_In_ unsigned int indexOfMemory)
{
SaveMemoryCommand(MemoryCommand::MemorizedNumberAdd, indexOfMemory);
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
if (m_memorizedNumbers.empty())
{
this->MemorizeNumber();
}
else
{
this->MemorizedNumberSelect(indexOfMemory);
m_currentCalculatorEngine->ProcessCommand(IDC_MPLUS);
this->MemorizedNumberChanged(indexOfMemory);
this->SetMemorizedNumbersString();
}
m_displayCallback->MemoryItemChanged(indexOfMemory);
}
void CalculatorManager::MemorizedNumberClear(_In_ unsigned int indexOfMemory)
{
if (indexOfMemory < m_memorizedNumbers.size())
{
SaveMemoryCommand(MemoryCommand::MemorizedNumberClear, indexOfMemory);
m_memorizedNumbers.erase(m_memorizedNumbers.begin() + indexOfMemory);
}
}
/// <summary>
/// Do the subtraction to the selected memory
/// It adds primary display value to the selected memory
/// Notify the client with new the new memorize value vector
/// </summary>
/// <param name="indexOfMemory">Index of the target memory</param>
void CalculatorManager::MemorizedNumberSubtract(_In_ unsigned int indexOfMemory)
{
SaveMemoryCommand(MemoryCommand::MemorizedNumberSubtract, indexOfMemory);
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
// To add negative of the number on display to the memory -x = x - 2x
if (m_memorizedNumbers.empty())
{
this->MemorizeNumber();
this->MemorizedNumberSubtract(0);
this->MemorizedNumberSubtract(0);
}
else
{
this->MemorizedNumberSelect(indexOfMemory);
m_currentCalculatorEngine->ProcessCommand(IDC_MMINUS);
this->MemorizedNumberChanged(indexOfMemory);
this->SetMemorizedNumbersString();
}
m_displayCallback->MemoryItemChanged(indexOfMemory);
}
/// <summary>
/// Clear all the memorized values
/// Notify the client with new the new memorize value vector
/// </summary>
void CalculatorManager::MemorizedNumberClearAll()
{
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizedNumberClearAll));
m_memorizedNumbers.clear();
m_currentCalculatorEngine->ProcessCommand(IDC_MCLEAR);
this->SetMemorizedNumbersString();
}
/// <summary>
/// Helper function that selects a memory from the vector and set it to CCalcEngine
/// Saved RAT number needs to be copied and passed in, as CCalcEngine destroyed the passed in RAT
/// </summary>
/// <param name="indexOfMemory">Index of the target memory</param>
void CalculatorManager::MemorizedNumberSelect(_In_ unsigned int indexOfMemory)
{
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
auto memoryObject = m_memorizedNumbers.at(indexOfMemory);
m_currentCalculatorEngine->PersistedMemObject(memoryObject);
}
/// <summary>
/// Helper function that needs to be executed when memory is modified
/// When memory is modified, destroy the old RAT and put the new RAT in vector
/// </summary>
/// <param name="indexOfMemory">Index of the target memory</param>
void CalculatorManager::MemorizedNumberChanged(_In_ unsigned int indexOfMemory)
{
if (m_currentCalculatorEngine->FInErrorState())
{
return;
}
auto memoryObject = m_currentCalculatorEngine->PersistedMemObject();
if (memoryObject != nullptr)
{
m_memorizedNumbers.at(indexOfMemory) = *memoryObject;
}
}
void CalculatorManager::SaveMemoryCommand(_In_ MemoryCommand command, _In_ unsigned int indexOfMemory)
{
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(command));
if (indexOfMemory > UCHAR_MAX)
{
throw invalid_argument("Unexpected value. IndexOfMemory is bigger than the biggest unsigned char");
}
m_savedCommands.push_back(static_cast<unsigned char>(indexOfMemory));
}
vector<shared_ptr<HISTORYITEM>> const& CalculatorManager::GetHistoryItems()
{
return m_pHistory->GetHistory();
}
vector<shared_ptr<HISTORYITEM>> const& CalculatorManager::GetHistoryItems(_In_ CALCULATOR_MODE mode)
{
return (mode == CM_STD) ? m_pStdHistory->GetHistory() : m_pSciHistory->GetHistory();
}
shared_ptr<HISTORYITEM> const& CalculatorManager::GetHistoryItem(_In_ unsigned int uIdx)
{
return m_pHistory->GetHistoryItem(uIdx);
}
void CalculatorManager::OnHistoryItemAdded(_In_ unsigned int addedItemIndex)
{
m_displayCallback->OnHistoryItemAdded(addedItemIndex);
}
bool CalculatorManager::RemoveHistoryItem(_In_ unsigned int uIdx)
{
return m_pHistory->RemoveItem(uIdx);
}
void CalculatorManager::ClearHistory()
{
m_pHistory->ClearHistory();
}
void CalculatorManager::SetRadix(RADIX_TYPE iRadixType)
{
switch (iRadixType)
{
case RADIX_TYPE::HEX_RADIX:
m_currentCalculatorEngine->ProcessCommand(IDC_HEX);
break;
case RADIX_TYPE::DEC_RADIX:
m_currentCalculatorEngine->ProcessCommand(IDC_DEC);
break;
case RADIX_TYPE::OCT_RADIX:
m_currentCalculatorEngine->ProcessCommand(IDC_OCT);
break;
case RADIX_TYPE::BIN_RADIX:
m_currentCalculatorEngine->ProcessCommand(IDC_BIN);
break;
default:
break;
}
SetMemorizedNumbersString();
}
void CalculatorManager::SetMemorizedNumbersString()
{
vector<wstring> resultVector;
for (auto const& memoryItem : m_memorizedNumbers)
{
int radix = m_currentCalculatorEngine->GetCurrentRadix();
wstring stringValue = m_currentCalculatorEngine->GetStringForDisplay(memoryItem, radix);
if (!stringValue.empty())
{
resultVector.push_back(m_currentCalculatorEngine->GroupDigitsPerRadix(stringValue, radix));
}
}
m_displayCallback->SetMemorizedNumbers(resultVector);
}
CalculationManager::Command CalculatorManager::GetCurrentDegreeMode()
{
if (m_currentDegreeMode == Command::CommandNULL)
{
m_currentDegreeMode = Command::CommandDEG;
}
return m_currentDegreeMode;
}
void CalculatorManager::SetHistory(_In_ CALCULATOR_MODE eMode, _In_ vector<shared_ptr<HISTORYITEM>> const& history)
{
CalculatorHistory* pHistory = nullptr;
switch (eMode)
{
case CM_STD:
pHistory = m_pStdHistory.get();
break;
case CM_SCI:
pHistory = m_pSciHistory.get();
break;
}
if (pHistory)
{
pHistory->ClearHistory();
for (unsigned int i = 0; i < history.size(); ++i)
{
pHistory->AddItem(history[i]);
}
}
}
wstring CalculatorManager::GetResultForRadix(uint32_t radix, int32_t precision, bool groupDigitsPerRadix)
{
return m_currentCalculatorEngine ? m_currentCalculatorEngine->GetCurrentResultForRadix(radix, precision, groupDigitsPerRadix) : L"";
}
void CalculatorManager::SetPrecision(int32_t precision)
{
m_currentCalculatorEngine->ChangePrecision(precision);
}
void CalculatorManager::UpdateMaxIntDigits()
{
m_currentCalculatorEngine->UpdateMaxIntDigits();
}
wchar_t CalculatorManager::DecimalSeparator()
{
return m_currentCalculatorEngine ? m_currentCalculatorEngine->DecimalSeparator() : m_resourceProvider->GetCEngineString(L"sDecimal")[0];
}
bool CalculatorManager::IsEngineRecording()
{
return m_currentCalculatorEngine->FInRecordingState() ? true : false;
}
bool CalculatorManager::IsInputEmpty()
{
return m_currentCalculatorEngine->IsInputEmpty();
}
void CalculatorManager::SetInHistoryItemLoadMode(_In_ bool isHistoryItemLoadMode)
{
m_inHistoryItemLoadMode = isHistoryItemLoadMode;
}
/// <summary>
/// Serialize Rational to vector of long
/// How Rational is serialized :
/// Serialized Rational.P(Number) + Serialized Rational.Q(Number)
/// How Number is saved :
/// [0] = Rational.P.Sign
/// [1] = Rational.P.Mantissa.size
/// [2] = Rational.P.Exp
/// [3] = Rational.P.Mantissa[0]
/// [4] = Rational.P.Mantissa[1]
/// ...
/// [2 + Rational.P.Mantissa.size] = Rational.P.Mantissa[size - 1]
/// </summary>
/// <param name = "rat">Rational number to be serialized</param>
vector<long> CalculatorManager::SerializeRational(Rational const& rat)
{
vector<long> serializedRational{};
auto serialP = SerializeNumber(rat.P());
serializedRational.insert(serializedRational.end(), serialP.begin(), serialP.end());
auto serialQ = SerializeNumber(rat.Q());
serializedRational.insert(serializedRational.end(), serialQ.begin(), serialQ.end());
return serializedRational;
}
/// <summary>
/// DeserializeRational vector and construct a Rational
/// How Rational is serialized :
/// Serialized Rational.P(Number) + Serialized Rational.Q(Number)
/// </summary>
Rational CalculatorManager::DeSerializeRational(vector<long>::const_iterator itr)
{
auto p = DeSerializeNumber(itr);
auto q = DeSerializeNumber(itr + SERIALIZED_NUMBER_MINSIZE + p.Mantissa().size());
return Rational(p, q);
}
/// <summary>
/// Serialize Number to vector of long
/// How Number is saved :
/// [0] = Number.Sign
/// [1] = Number.Mantissa.size
/// [2] = Number.Exp
/// [3] = Number.Mantissa[0]
/// [4] = Number.Mantissa[1]
/// ...
/// [2 + Number.Mantissa.size] = Number.Mantissa[size - 1]
/// </summary>
/// <param name = "num">Number to be serialized</param>
vector<long> CalculatorManager::SerializeNumber(Number const& num)
{
vector<long> serializedNumber{};
serializedNumber.push_back(num.Sign());
serializedNumber.push_back(static_cast<long>(num.Mantissa().size()));
serializedNumber.push_back(num.Exp());
for (auto const& digit : num.Mantissa())
{
serializedNumber.push_back(digit);
}
return serializedNumber;
}
/// <summary>
/// DeserializeNumber vector and construct a Number
/// How Number is saved :
/// [0] = Number.Sign
/// [1] = Number.Mantissa.size
/// [2] = Number.Exp
/// [3] = Number.Mantissa[0]
/// [4] = Number.Mantissa[1]
/// ...
/// [2 + Number.Mantissa.size] = Number.Mantissa[size - 1]
/// </summary>
Number CalculatorManager::DeSerializeNumber(vector<long>::const_iterator itr)
{
int32_t sign = *itr;
uint32_t size = *(itr + 1);
int32_t exp = *(itr + 2);
vector<uint32_t> mant{};
for (size_t i = 0; i < size; ++i)
{
mant.emplace_back(*(itr + 3 + i));
}
return Number{ sign, exp, mant };
}
}