mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 06:13:14 -07:00
- Remove while(true)
conditional and simplify evaluation of precedence stack.
This commit is contained in:
parent
d2e7d475bb
commit
091286e8d6
2 changed files with 49 additions and 40 deletions
|
@ -463,45 +463,12 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
|
||||||
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
|
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
// Evaluate the precedence stack.
|
||||||
|
ResolveHighestPrecedenceOperation();
|
||||||
if (m_nOpCode) /* Is there a valid operation around? */
|
while (m_fPrecedence && m_precedenceOpCount > 0)
|
||||||
{
|
{
|
||||||
/* If this is the first EQU in a string, set m_holdVal=m_currentVal */
|
m_precedenceOpCount--;
|
||||||
/* Otherwise let m_currentVal=m_holdVal. This keeps m_currentVal constant */
|
m_nOpCode = m_nPrecOp[m_precedenceOpCount];
|
||||||
/* through all EQUs in a row. */
|
|
||||||
if (m_bNoPrevEqu)
|
|
||||||
{
|
|
||||||
m_holdVal = m_currentVal;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do the current or last operation.
|
|
||||||
m_currentVal = DoOperation(m_nOpCode, m_currentVal, m_lastVal);
|
|
||||||
m_nPrevOpCode = m_nOpCode;
|
|
||||||
m_lastVal = m_currentVal;
|
|
||||||
|
|
||||||
/* Check for errors. If this wasn't done, DisplayNum */
|
|
||||||
/* would immediately overwrite any error message. */
|
|
||||||
if (!m_bError)
|
|
||||||
DisplayNum();
|
|
||||||
|
|
||||||
/* No longer the first EQU. */
|
|
||||||
m_bNoPrevEqu = false;
|
|
||||||
}
|
|
||||||
else if (!m_bError)
|
|
||||||
DisplayNum();
|
|
||||||
|
|
||||||
if (m_precedenceOpCount == 0 || !m_fPrecedence)
|
|
||||||
break;
|
|
||||||
|
|
||||||
m_nOpCode = m_nPrecOp[--m_precedenceOpCount];
|
|
||||||
m_lastVal = m_precedenceVals[m_precedenceOpCount];
|
m_lastVal = m_precedenceVals[m_precedenceOpCount];
|
||||||
|
|
||||||
// Precedence Inversion check
|
// Precedence Inversion check
|
||||||
|
@ -514,7 +481,9 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
|
||||||
m_HistoryCollector.PopLastOpndStart();
|
m_HistoryCollector.PopLastOpndStart();
|
||||||
|
|
||||||
m_bNoPrevEqu = true;
|
m_bNoPrevEqu = true;
|
||||||
} while (true);
|
|
||||||
|
ResolveHighestPrecedenceOperation();
|
||||||
|
}
|
||||||
|
|
||||||
if (!m_bError)
|
if (!m_bError)
|
||||||
{
|
{
|
||||||
|
@ -789,6 +758,45 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to resolve one item on the precedence stack.
|
||||||
|
void CCalcEngine::ResolveHighestPrecedenceOperation()
|
||||||
|
{
|
||||||
|
if (m_nOpCode) /* Is there a valid operation around? */
|
||||||
|
{
|
||||||
|
/* If this is the first EQU in a string, set m_holdVal=m_currentVal */
|
||||||
|
/* Otherwise let m_currentVal=m_holdVal. This keeps m_currentVal constant */
|
||||||
|
/* through all EQUs in a row. */
|
||||||
|
if (m_bNoPrevEqu)
|
||||||
|
{
|
||||||
|
m_holdVal = m_currentVal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do the current or last operation.
|
||||||
|
m_currentVal = DoOperation(m_nOpCode, m_currentVal, m_lastVal);
|
||||||
|
m_nPrevOpCode = m_nOpCode;
|
||||||
|
m_lastVal = m_currentVal;
|
||||||
|
|
||||||
|
/* Check for errors. If this wasn't done, DisplayNum */
|
||||||
|
/* would immediately overwrite any error message. */
|
||||||
|
if (!m_bError)
|
||||||
|
DisplayNum();
|
||||||
|
|
||||||
|
/* No longer the first EQU. */
|
||||||
|
m_bNoPrevEqu = false;
|
||||||
|
}
|
||||||
|
else if (!m_bError)
|
||||||
|
{
|
||||||
|
DisplayNum();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CheckAndAddLastBinOpToHistory
|
// CheckAndAddLastBinOpToHistory
|
||||||
//
|
//
|
||||||
// This is a very confusing helper routine to add the last entered binary operator to the history. This is expected to
|
// This is a very confusing helper routine to add the last entered binary operator to the history. This is expected to
|
||||||
|
|
|
@ -129,6 +129,7 @@ private:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ProcessCommandWorker(OpCode wParam);
|
void ProcessCommandWorker(OpCode wParam);
|
||||||
|
void ResolveHighestPrecedenceOperation();
|
||||||
void HandleErrorCommand(OpCode idc);
|
void HandleErrorCommand(OpCode idc);
|
||||||
void HandleMaxDigitsReached();
|
void HandleMaxDigitsReached();
|
||||||
void DisplayNum(void);
|
void DisplayNum(void);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue