Merge pull request #1 from joshkoon/rational-history

Convert ExpressionCommand and History collector to use Rational
This commit is contained in:
Josh Koon 2019-01-29 14:31:34 -08:00 committed by GitHub
commit 015e0270c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 62 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
#include "pch.h" #include "pch.h"
@ -12,6 +12,7 @@
constexpr int ASCII_0 = 48; constexpr int ASCII_0 = 48;
using namespace std; using namespace std;
using namespace CalcEngine;
void CHistoryCollector::ReinitHistory() void CHistoryCollector::ReinitHistory()
{ {
@ -51,7 +52,7 @@ CHistoryCollector::~CHistoryCollector()
} }
} }
void CHistoryCollector::AddOpndToHistory(wstring_view numStr, PRAT hNoNum, bool fRepetition) void CHistoryCollector::AddOpndToHistory(wstring_view numStr, Rational const& rat, bool fRepetition)
{ {
std::shared_ptr<CalculatorVector<int>> commands = std::make_shared<CalculatorVector<int>>(); std::shared_ptr<CalculatorVector<int>> commands = std::make_shared<CalculatorVector<int>>();
// Check for negate // Check for negate
@ -92,7 +93,7 @@ void CHistoryCollector::AddOpndToHistory(wstring_view numStr, PRAT hNoNum, bool
} }
auto operandCommand = std::make_shared<COpndCommand>(commands, fNegative, fDecimal, fSciFmt); auto operandCommand = std::make_shared<COpndCommand>(commands, fNegative, fDecimal, fSciFmt);
operandCommand->Initialize(hNoNum); operandCommand->Initialize(rat);
int iCommandEnd = AddCommand(operandCommand); int iCommandEnd = AddCommand(operandCommand);
m_lastOpStartIndex = IchAddSzToEquationSz(numStr, iCommandEnd); m_lastOpStartIndex = IchAddSzToEquationSz(numStr, iCommandEnd);

View File

@ -211,9 +211,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
if (!m_HistoryCollector.FOpndAddedToHistory()) if (!m_HistoryCollector.FOpndAddedToHistory())
{ {
// if the prev command was ) or unop then it is already in history as a opnd form (...) // if the prev command was ) or unop then it is already in history as a opnd form (...)
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
} }
/* m_bChangeOp is true if there was an operation done and the */ /* m_bChangeOp is true if there was an operation done and the */
@ -310,10 +308,7 @@ DoPrecedenceCheckAgain:
{ {
if (!m_HistoryCollector.FOpndAddedToHistory()) if (!m_HistoryCollector.FOpndAddedToHistory())
{ {
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
} }
m_HistoryCollector.AddUnaryOpToHistory((INT)wParam, m_bInv, m_angletype); m_HistoryCollector.AddUnaryOpToHistory((INT)wParam, m_bInv, m_angletype);
@ -339,9 +334,7 @@ DoPrecedenceCheckAgain:
if(wParam == IDC_PERCENT) if(wParam == IDC_PERCENT)
{ {
CheckAndAddLastBinOpToHistory(); CheckAndAddLastBinOpToHistory();
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal, true /* Add to primary and secondary display */);
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat, true);
destroyrat(curRat);
} }
/* reset the m_bInv flag and indicators if it is set /* reset the m_bInv flag and indicators if it is set
@ -464,9 +457,7 @@ DoPrecedenceCheckAgain:
if (!m_HistoryCollector.FOpndAddedToHistory()) if (!m_HistoryCollector.FOpndAddedToHistory())
{ {
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
} }
do { do {
@ -485,9 +476,7 @@ DoPrecedenceCheckAgain:
m_currentVal = m_holdVal; m_currentVal = m_holdVal;
DisplayNum(); // to update the m_numberString DisplayNum(); // to update the m_numberString
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, false); m_HistoryCollector.AddBinOpToHistory(m_nOpCode, false);
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal); // Adding the repeated last op to history
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat); // Adding the repeated last op to history
destroyrat(curRat);
} }
// Do the current or last operation. // Do the current or last operation.
@ -595,9 +584,7 @@ DoPrecedenceCheckAgain:
if (!m_HistoryCollector.FOpndAddedToHistory()) if (!m_HistoryCollector.FOpndAddedToHistory())
{ {
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
} }
// Get the operation and number and return result. // Get the operation and number and return result.
@ -701,9 +688,7 @@ DoPrecedenceCheckAgain:
if (!m_HistoryCollector.FOpndAddedToHistory()) if (!m_HistoryCollector.FOpndAddedToHistory())
{ {
PRAT curRat = m_currentVal.ToPRAT(); m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
} }
PRAT curRat = m_currentVal.ToPRAT(); PRAT curRat = m_currentVal.ToPRAT();

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
#include "pch.h" #include "pch.h"
@ -7,6 +7,7 @@
#include "ExpressionCommand.h" #include "ExpressionCommand.h"
using namespace std; using namespace std;
using namespace CalcEngine;
constexpr wchar_t chNegate = L'-'; constexpr wchar_t chNegate = L'-';
constexpr wchar_t chExp = L'e'; constexpr wchar_t chExp = L'e';
@ -94,25 +95,19 @@ void CBinaryCommand::Accept(_In_ ISerializeCommandVisitor &commandVisitor)
commandVisitor.Visit(*this); commandVisitor.Visit(*this);
} }
COpndCommand::COpndCommand(_In_ shared_ptr<CalculatorVector<int>> const &commands, COpndCommand::COpndCommand(shared_ptr<CalculatorVector<int>> const &commands, bool fNegative, bool fDecimal, bool fSciFmt) :
_In_ bool fNegative, m_commands(commands),
_In_ bool fDecimal, m_fNegative(fNegative),
_In_ bool fSciFmt) : m_fDecimal(fDecimal),
m_commands(commands), m_fNegative(fNegative), m_fDecimal(fDecimal), m_fSciFmt(fSciFmt) m_fSciFmt(fSciFmt),
{ m_fInitialized(false),
m_hnoNum = nullptr; m_value{}
} {}
void COpndCommand::Initialize(Rational const& rat)
void COpndCommand::Initialize(_In_ PRAT hNoNum)
{ {
assert(&m_hnoNum != nullptr); m_value = rat;
if (m_hnoNum != nullptr) m_fInitialized = true;
{
destroyrat(m_hnoNum);
m_hnoNum = nullptr;
}
DUPRAT(m_hnoNum, hNoNum);
} }
const shared_ptr<CalculatorVector<int>> & COpndCommand::GetCommands() const const shared_ptr<CalculatorVector<int>> & COpndCommand::GetCommands() const
@ -294,18 +289,16 @@ const wstring & COpndCommand::GetToken(wchar_t decimalSymbol)
wstring COpndCommand::GetString(uint32_t radix, int32_t precision, wchar_t decimalSymbol) wstring COpndCommand::GetString(uint32_t radix, int32_t precision, wchar_t decimalSymbol)
{ {
wstring numString{}; wstring result{};
if (m_hnoNum != nullptr)
if (m_fInitialized)
{ {
numString = NumObjToString(m_hnoNum, radix, eNUMOBJ_FMT::FMT_FLOAT, precision); PRAT valRat = m_value.ToPRAT();
result = NumObjToString(valRat, radix, eNUMOBJ_FMT::FMT_FLOAT, precision);
destroyrat(valRat);
} }
return numString; return result;
}
COpndCommand::~COpndCommand()
{
destroyrat(m_hnoNum);
} }
void COpndCommand::Accept(_In_ ISerializeCommandVisitor &commandVisitor) void COpndCommand::Accept(_In_ ISerializeCommandVisitor &commandVisitor)

View File

@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
#pragma once #pragma once
#include "ExpressionCommandInterface.h" #include "ExpressionCommandInterface.h"
#include "Header Files\CalcEngine.h" #include "Header Files/CalcEngine.h"
#include "Header Files/Rational.h"
class CParentheses : public IParenthesisCommand class CParentheses : public IParenthesisCommand
{ {
@ -48,12 +49,12 @@ private:
class COpndCommand : public IOpndCommand class COpndCommand : public IOpndCommand
{ {
public: public:
COpndCommand(_In_ std::shared_ptr<CalculatorVector<int>> const &commands, COpndCommand(
_In_ bool fNegative, std::shared_ptr<CalculatorVector<int>> const &commands,
_In_ bool fDecimal, bool fNegative,
_In_ bool fSciFmt); bool fDecimal,
~COpndCommand(); bool fSciFmt);
void Initialize(_In_ PRAT hNoNum); void Initialize(CalcEngine::Rational const& rat);
const std::shared_ptr<CalculatorVector<int>> & GetCommands() const; const std::shared_ptr<CalculatorVector<int>> & GetCommands() const;
void SetCommands(std::shared_ptr<CalculatorVector<int>> const& commands); void SetCommands(std::shared_ptr<CalculatorVector<int>> const& commands);
@ -73,8 +74,9 @@ private:
bool m_fNegative; bool m_fNegative;
bool m_fSciFmt; bool m_fSciFmt;
bool m_fDecimal; bool m_fDecimal;
bool m_fInitialized;
std::wstring m_token; std::wstring m_token;
PRAT m_hnoNum; CalcEngine::Rational m_value;
void ClearAllAndAppendCommand(CalculationManager::Command command); void ClearAllAndAppendCommand(CalculationManager::Command command);
}; };

View File

@ -5,6 +5,7 @@
#include "ICalcDisplay.h" #include "ICalcDisplay.h"
#include "IHistoryDisplay.h" #include "IHistoryDisplay.h"
#include "Rational.h"
// maximum depth you can get by precedence. It is just an array's size limit. // maximum depth you can get by precedence. It is just an array's size limit.
static constexpr size_t MAXPRECDEPTH = 25; static constexpr size_t MAXPRECDEPTH = 25;
@ -16,7 +17,7 @@ class CHistoryCollector {
public: public:
CHistoryCollector(ICalcDisplay *pCalcDisplay, std::shared_ptr<IHistoryDisplay> pHistoryDisplay, wchar_t decimalSymbol); // Can throw errors CHistoryCollector(ICalcDisplay *pCalcDisplay, std::shared_ptr<IHistoryDisplay> pHistoryDisplay, wchar_t decimalSymbol); // Can throw errors
~CHistoryCollector(); ~CHistoryCollector();
void AddOpndToHistory(std::wstring_view numStr, PRAT hNoNum, bool fRepetition = false); void AddOpndToHistory(std::wstring_view numStr, CalcEngine::Rational const& rat, bool fRepetition = false);
void RemoveLastOpndFromHistory(); void RemoveLastOpndFromHistory();
void AddBinOpToHistory(int nOpCode, bool fNoRepetition = true); void AddBinOpToHistory(int nOpCode, bool fNoRepetition = true);
void ChangeLastBinOp(int nOpCode, bool fPrecInvToHigher); void ChangeLastBinOp(int nOpCode, bool fPrecInvToHigher);