Skip to content

Commit

Permalink
Convert ExpressionCommand and History collector to use Rational inste…
Browse files Browse the repository at this point in the history
…ad of PRAT
  • Loading branch information
joshkoon committed Jan 29, 2019
1 parent c13b8a0 commit 4883fab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/CalcManager/CEngine/History.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
constexpr int ASCII_0 = 48;

using namespace std;
using namespace CalcEngine;

void CHistoryCollector::ReinitHistory()
{
Expand Down Expand Up @@ -51,7 +52,7 @@ CHistoryCollector::~CHistoryCollector()
}
}

void CHistoryCollector::AddOpndToHistory(wstring_view numStr, PRAT hNoNum, bool fRepetition)
void CHistoryCollector::AddOpndToHistory(wstring_view numStr, Rational rat, bool fRepetition)
{
std::shared_ptr<CalculatorVector<int>> commands = std::make_shared<CalculatorVector<int>>();
// Check for negate
Expand Down Expand Up @@ -91,8 +92,7 @@ void CHistoryCollector::AddOpndToHistory(wstring_view numStr, PRAT hNoNum, bool
}
}

auto operandCommand = std::make_shared<COpndCommand>(commands, fNegative, fDecimal, fSciFmt);
operandCommand->Initialize(hNoNum);
auto operandCommand = std::make_shared<COpndCommand>(commands, rat, fNegative, fDecimal, fSciFmt);
int iCommandEnd = AddCommand(operandCommand);
m_lastOpStartIndex = IchAddSzToEquationSz(numStr, iCommandEnd);

Expand Down
29 changes: 7 additions & 22 deletions src/CalcManager/CEngine/scicomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
if (!m_HistoryCollector.FOpndAddedToHistory())
{
// 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, curRat);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}

/* m_bChangeOp is true if there was an operation done and the */
Expand Down Expand Up @@ -310,10 +308,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
{
if (!m_HistoryCollector.FOpndAddedToHistory())
{

PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}

m_HistoryCollector.AddUnaryOpToHistory((INT)wParam, m_bInv, m_angletype);
Expand All @@ -339,9 +334,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
if(wParam == IDC_PERCENT)
{
CheckAndAddLastBinOpToHistory();
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat, true);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal, true);
}

/* reset the m_bInv flag and indicators if it is set
Expand Down Expand Up @@ -464,9 +457,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)

if (!m_HistoryCollector.FOpndAddedToHistory())
{
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}

do {
Expand All @@ -485,9 +476,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
m_currentVal = m_holdVal;
DisplayNum(); // to update the m_numberString
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, false);
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat); // Adding the repeated last op to history
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal); // Adding the repeated last op to history
}

// Do the current or last operation.
Expand Down Expand Up @@ -595,9 +584,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)

if (!m_HistoryCollector.FOpndAddedToHistory())
{
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}

// Get the operation and number and return result.
Expand Down Expand Up @@ -701,9 +688,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)

if (!m_HistoryCollector.FOpndAddedToHistory())
{
PRAT curRat = m_currentVal.ToPRAT();
m_HistoryCollector.AddOpndToHistory(m_numberString, curRat);
destroyrat(curRat);
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}

PRAT curRat = m_currentVal.ToPRAT();
Expand Down
40 changes: 12 additions & 28 deletions src/CalcManager/ExpressionCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ExpressionCommand.h"

using namespace std;
using namespace CalcEngine;

constexpr wchar_t chNegate = L'-';
constexpr wchar_t chExp = L'e';
Expand Down Expand Up @@ -95,25 +96,12 @@ void CBinaryCommand::Accept(_In_ ISerializeCommandVisitor &commandVisitor)
}

COpndCommand::COpndCommand(_In_ shared_ptr<CalculatorVector<int>> const &commands,
_In_ bool fNegative,
_In_ bool fDecimal,
_In_ bool fSciFmt) :
m_commands(commands), m_fNegative(fNegative), m_fDecimal(fDecimal), m_fSciFmt(fSciFmt)
{
m_hnoNum = nullptr;
}


void COpndCommand::Initialize(_In_ PRAT hNoNum)
{
assert(&m_hnoNum != nullptr);
if (m_hnoNum != nullptr)
{
destroyrat(m_hnoNum);
m_hnoNum = nullptr;
}
DUPRAT(m_hnoNum, hNoNum);
}
Rational const& rat,
bool fNegative,
bool fDecimal,
bool fSciFmt) :
m_commands(commands), m_value{ rat }, m_fNegative(fNegative), m_fDecimal(fDecimal), m_fSciFmt(fSciFmt)
{}

const shared_ptr<CalculatorVector<int>> & COpndCommand::GetCommands() const
{
Expand Down Expand Up @@ -294,19 +282,15 @@ const wstring & COpndCommand::GetToken(wchar_t decimalSymbol)

wstring COpndCommand::GetString(uint32_t radix, int32_t precision, wchar_t decimalSymbol)
{
wstring numString{};
if (m_hnoNum != nullptr)
{
numString = NumObjToString(m_hnoNum, radix, eNUMOBJ_FMT::FMT_FLOAT, precision);
}
PRAT valRat = m_value.ToPRAT();
auto 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)
{
Expand Down
13 changes: 7 additions & 6 deletions src/CalcManager/ExpressionCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#pragma once
#include "ExpressionCommandInterface.h"
#include "Header Files\CalcEngine.h"
#include "Header Files/CalcEngine.h"
#include "Header Files/Rational.h"

class CParentheses : public IParenthesisCommand
{
Expand Down Expand Up @@ -49,11 +50,11 @@ class COpndCommand : public IOpndCommand
{
public:
COpndCommand(_In_ std::shared_ptr<CalculatorVector<int>> const &commands,
_In_ bool fNegative,
_In_ bool fDecimal,
_In_ bool fSciFmt);
CalcEngine::Rational const& rat,
bool fNegative,
bool fDecimal,
bool fSciFmt);
~COpndCommand();
void Initialize(_In_ PRAT hNoNum);

const std::shared_ptr<CalculatorVector<int>> & GetCommands() const;
void SetCommands(std::shared_ptr<CalculatorVector<int>> const& commands);
Expand All @@ -74,7 +75,7 @@ class COpndCommand : public IOpndCommand
bool m_fSciFmt;
bool m_fDecimal;
std::wstring m_token;
PRAT m_hnoNum;
CalcEngine::Rational m_value;
void ClearAllAndAppendCommand(CalculationManager::Command command);
};

Expand Down
3 changes: 2 additions & 1 deletion src/CalcManager/Header Files/History.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "ICalcDisplay.h"
#include "IHistoryDisplay.h"
#include "Rational.h"

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

0 comments on commit 4883fab

Please sign in to comment.