From ae163c8067114f6af013f4d4dbf44e31364ebab3 Mon Sep 17 00:00:00 2001 From: Rudy Huyn Date: Tue, 21 May 2019 03:33:03 -0700 Subject: [PATCH] Fix:The application displays faulty results with unary operations when a binary action was previously used --- src/CalcManager/CEngine/scicomm.cpp | 51 +++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/CalcManager/CEngine/scicomm.cpp b/src/CalcManager/CEngine/scicomm.cpp index eabb7f94..b6f35423 100644 --- a/src/CalcManager/CEngine/scicomm.cpp +++ b/src/CalcManager/CEngine/scicomm.cpp @@ -170,6 +170,8 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) // BINARY OPERATORS: if (IsBinOpCode(wParam)) { + m_bNoPrevEqu = true; + // Change the operation if last input was operation. if (IsBinOpCode(m_nLastCom)) { @@ -274,13 +276,18 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) m_lastVal = m_currentVal; m_nOpCode = (int)wParam; m_HistoryCollector.AddBinOpToHistory(m_nOpCode); - m_bNoPrevEqu = m_bChangeOp = true; + m_bChangeOp = true; return; } // UNARY OPERATORS: if (IsUnaryOpCode(wParam) || (wParam == IDC_DEGREES)) { + m_bNoPrevEqu = true; + m_bChangeOp = false; + m_nPrevOpCode = m_nOpCode; + m_nOpCode = (INT)wParam; + /* Functions are unary operations. */ /* If the last thing done was an operator, m_currentVal was cleared. */ /* In that case we better use the number before the operator */ @@ -642,7 +649,11 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) } break; } - + else + { + m_nOpCode = IDC_SIGN; + m_bNoPrevEqu = true; + } // Doing +/- while in Record mode is not a unary operation if (IsBinOpCode(m_nLastCom)) { @@ -655,7 +666,6 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) } m_currentVal = -(m_currentVal); - DisplayNum(); m_HistoryCollector.AddUnaryOpToHistory(IDC_SIGN, m_bInv, m_angletype); } @@ -701,6 +711,13 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) break; case IDC_PI: + + m_bNoPrevEqu = true; + if (!m_bChangeOp) + { + m_nOpCode = 0; + } + if (!m_fIntegerMode) { CheckAndAddLastBinOpToHistory(); // pi is like entering the number @@ -760,12 +777,34 @@ void CCalcEngine::ResolveHighestPrecedenceOperation() { m_currentVal = m_holdVal; DisplayNum(); // to update the m_numberString - m_HistoryCollector.AddBinOpToHistory(m_nOpCode, false); - m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal); // Adding the repeated last op to history + if (IsBinOpCode(m_nOpCode)) + { + m_HistoryCollector.AddBinOpToHistory(m_nOpCode, false); + m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal); // Adding the repeated last op to history + } + else if (m_nOpCode == IDC_SIGN || IsUnaryOpCode(m_nOpCode)) + { + m_HistoryCollector.AddUnaryOpToHistory(m_nOpCode, m_bInv, m_angletype); + } } // Do the current or last operation. - m_currentVal = DoOperation(m_nOpCode, m_currentVal, m_lastVal); + if (!m_bNoPrevEqu && (m_nOpCode == IDC_SIGN || IsUnaryOpCode(m_nOpCode))) + { + if (m_nOpCode == IDC_SIGN) + { + m_currentVal = -m_currentVal; + } + else + { + m_currentVal = SciCalcFunctions(m_currentVal, (DWORD)m_nOpCode); + } + m_holdVal = m_currentVal; + } + else + { + m_currentVal = DoOperation(m_nOpCode, m_currentVal, m_lastVal); + } m_nPrevOpCode = m_nOpCode; m_lastVal = m_currentVal;