Handle IDC_SUB binary in Special cases

This commit is contained in:
Dhikshith 2023-08-06 20:07:46 +05:30
commit 09a655b194
9 changed files with 79 additions and 33 deletions

View file

@ -44,7 +44,6 @@ bool IsGuiSettingOpCode(OpCode opCode)
case IDC_FE: case IDC_FE:
case IDC_MCLEAR: case IDC_MCLEAR:
case IDC_BACK: case IDC_BACK:
case IDC_EXP:
case IDC_STORE: case IDC_STORE:
case IDC_MPLUS: case IDC_MPLUS:
case IDC_MMINUS: case IDC_MMINUS:

View file

@ -87,6 +87,11 @@ namespace CalcEngine
return m_q; return m_q;
} }
bool Rational::IsZero() const
{
return m_p.IsZero();
}
Rational Rational::operator-() const Rational Rational::operator-() const
{ {
return Rational{ Number{ -1 * m_p.Sign(), m_p.Exp(), m_p.Mantissa() }, m_q }; return Rational{ Number{ -1 * m_p.Sign(), m_p.Exp(), m_p.Mantissa() }, m_q };

View file

@ -71,6 +71,7 @@ CCalcEngine::CCalcEngine(
, m_nPrevOpCode(0) , m_nPrevOpCode(0)
, m_bChangeOp(false) , m_bChangeOp(false)
, m_bRecord(false) , m_bRecord(false)
, m_bFlagSign(false)
, m_bSetCalcState(false) , m_bSetCalcState(false)
, m_input(DEFAULT_DEC_SEPARATOR) , m_input(DEFAULT_DEC_SEPARATOR)
, m_nFE(NumberFormat::Float) , m_nFE(NumberFormat::Float)

View file

@ -112,10 +112,11 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
{ {
// Save the last command. Some commands are not saved in this manor, these // Save the last command. Some commands are not saved in this manor, these
// commands are: // commands are:
// Inv, Deg, Rad, Grad, Stat, FE, MClear, Back, and Exp. The excluded // Inv, Deg, Rad, Grad, Stat, FE, MClear, and Back. The excluded
// commands are not // commands are not
// really mathematical operations, rather they are GUI mode settings. // really mathematical operations, rather they are GUI mode settings.
//
// UDATE: We now save the last command for the Exp command as well.
if (!IsGuiSettingOpCode(wParam)) if (!IsGuiSettingOpCode(wParam))
{ {
m_nLastCom = m_nTempCom; m_nLastCom = m_nTempCom;
@ -146,6 +147,11 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
} }
} }
if (wParam == IDC_SUB && m_nLastCom == IDC_EXP)
{
wParam = IDC_SIGN;
}
// Toggle Record/Display mode if appropriate. // Toggle Record/Display mode if appropriate.
if (m_bRecord) if (m_bRecord)
{ {
@ -187,8 +193,24 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
DisplayNum(); DisplayNum();
/*
Addressing issues:
#1541 https://github.com/microsoft/calculator/issues/1541
#1311 https://github.com/microsoft/calculator/issues/1311
Solution:
Consider the previous '-' Binary Op as a '+/-' sign value if
the first opnd is 0.
*/
if (m_bFlagSign)
{
wParam = IDC_SIGN;
m_bFlagSign = false;
}
else
{
return; return;
} }
}
// BINARY OPERATORS: // BINARY OPERATORS:
if (IsBinOpCode(wParam)) if (IsBinOpCode(wParam))
@ -298,9 +320,19 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
} }
DisplayAnnounceBinaryOperator(); DisplayAnnounceBinaryOperator();
// consider this sub as +/- for the upcoming number
if ( wParam == IDC_SUB && m_currentVal.IsZero())
{
m_bFlagSign = true;
}
m_lastVal = m_currentVal; m_lastVal = m_currentVal;
m_nOpCode = (int)wParam; m_nOpCode = (int)wParam;
if (!m_bFlagSign)
{
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, m_fIntegerMode); m_HistoryCollector.AddBinOpToHistory(m_nOpCode, m_fIntegerMode);
}
m_bNoPrevEqu = m_bChangeOp = true; m_bNoPrevEqu = m_bChangeOp = true;
return; return;
} }

View file

@ -131,7 +131,9 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\Calculator</OutDir>
</PropertyGroup>
<PropertyGroup> <PropertyGroup>
<GenerateManifest>false</GenerateManifest> <GenerateManifest>false</GenerateManifest>
<GenerateProjectSpecificOutputFolder>true</GenerateProjectSpecificOutputFolder> <GenerateProjectSpecificOutputFolder>true</GenerateProjectSpecificOutputFolder>
@ -217,6 +219,7 @@
<WarningLevel>Level4</WarningLevel> <WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>
<ForcedIncludeFiles>pch.h</ForcedIncludeFiles> <ForcedIncludeFiles>pch.h</ForcedIncludeFiles>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

View file

@ -115,8 +115,8 @@
#define IDC_EXP 127 #define IDC_EXP 127
#define IDC_OPENP 128 #define IDC_OPENP 128 // Open parenthesis "("
#define IDC_CLOSEP 129 #define IDC_CLOSEP 129 // Close parenthesis ")"
#define IDC_0 130 // The controls for 0 through F must be consecutive and in order #define IDC_0 130 // The controls for 0 through F must be consecutive and in order
#define IDC_1 131 #define IDC_1 131

View file

@ -133,6 +133,7 @@ private:
bool m_bError; // Error flag. bool m_bError; // Error flag.
bool m_bInv; // Inverse on/off flag. bool m_bInv; // Inverse on/off flag.
bool m_bNoPrevEqu; /* Flag for previous equals. */ bool m_bNoPrevEqu; /* Flag for previous equals. */
bool m_bFlagSign; /* Flag for +/- on next op */
uint32_t m_radix; uint32_t m_radix;
int32_t m_precision; int32_t m_precision;

View file

@ -30,6 +30,8 @@ namespace CalcEngine
Number const& P() const; Number const& P() const;
Number const& Q() const; Number const& Q() const;
bool IsZero() const;
Rational operator-() const; Rational operator-() const;
Rational& operator+=(Rational const& rhs); Rational& operator+=(Rational const& rhs);
Rational& operator-=(Rational const& rhs); Rational& operator-=(Rational const& rhs);

View file

@ -10,6 +10,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{3B773403-B0D6-4F9A-948E-512A7A5FB315}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{3B773403-B0D6-4F9A-948E-512A7A5FB315}"
ProjectSection(ProjectDependencies) = postProject
{311E866D-8B93-4609-A691-265941FEE101} = {311E866D-8B93-4609-A691-265941FEE101}
EndProjectSection
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalcManager", "CalcManager\CalcManager.vcxproj", "{311E866D-8B93-4609-A691-265941FEE101}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalcManager", "CalcManager\CalcManager.vcxproj", "{311E866D-8B93-4609-A691-265941FEE101}"
EndProject EndProject
@ -41,6 +44,30 @@ Global
Release|x86 = Release|x86 Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.ActiveCfg = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Build.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Deploy.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.ActiveCfg = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Build.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Deploy.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.ActiveCfg = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Build.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Deploy.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.ActiveCfg = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Build.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Deploy.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.ActiveCfg = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Build.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Deploy.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.ActiveCfg = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Build.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Deploy.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.ActiveCfg = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Build.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Deploy.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.ActiveCfg = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Build.0 = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Deploy.0 = Release|x86
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.ActiveCfg = Debug|ARM {311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.ActiveCfg = Debug|ARM
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.Build.0 = Debug|ARM {311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.Build.0 = Debug|ARM
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM64.ActiveCfg = Debug|ARM64 {311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -169,30 +196,6 @@ Global
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x64.Build.0 = Release|x64 {FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x64.Build.0 = Release|x64
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.ActiveCfg = Release|Win32 {FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.ActiveCfg = Release|Win32
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.Build.0 = Release|Win32 {FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.Build.0 = Release|Win32
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.ActiveCfg = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Build.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Deploy.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.ActiveCfg = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Build.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Deploy.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.ActiveCfg = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Build.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Deploy.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.ActiveCfg = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Build.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Deploy.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.ActiveCfg = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Build.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Deploy.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.ActiveCfg = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Build.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Deploy.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.ActiveCfg = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Build.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Deploy.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.ActiveCfg = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Build.0 = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Deploy.0 = Release|x86
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.ActiveCfg = Debug|ARM {CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.ActiveCfg = Debug|ARM
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.Build.0 = Debug|ARM {CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.Build.0 = Debug|ARM
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM64.ActiveCfg = Debug|ARM64 {CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM64.ActiveCfg = Debug|ARM64