Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "Subtract" ( "-" ) Operation as "Toggle Sign" ( "+/-" ) in Special Cases to fix #1541 and #1311 #2035

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/CalcManager/CEngine/CalcUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ bool IsGuiSettingOpCode(OpCode opCode)
case IDC_FE:
case IDC_MCLEAR:
case IDC_BACK:
case IDC_EXP:
case IDC_STORE:
case IDC_MPLUS:
case IDC_MMINUS:
Expand Down
5 changes: 5 additions & 0 deletions src/CalcManager/CEngine/Rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ namespace CalcEngine
return m_q;
}

bool Rational::IsZero() const
{
return m_p.IsZero();
}

Rational Rational::operator-() const
{
return Rational{ Number{ -1 * m_p.Sign(), m_p.Exp(), m_p.Mantissa() }, m_q };
Expand Down
1 change: 1 addition & 0 deletions src/CalcManager/CEngine/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ CCalcEngine::CCalcEngine(
, m_nPrevOpCode(0)
, m_bChangeOp(false)
, m_bRecord(false)
, m_bFlagSign(false)
, m_bSetCalcState(false)
, m_input(DEFAULT_DEC_SEPARATOR)
, m_nFE(NumberFormat::Float)
Expand Down
42 changes: 37 additions & 5 deletions src/CalcManager/CEngine/scicomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
{
// Save the last command. Some commands are not saved in this manor, these
// 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
// 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))
{
m_nLastCom = m_nTempCom;
Expand Down Expand Up @@ -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.
if (m_bRecord)
{
Expand Down Expand Up @@ -187,11 +193,27 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)

DisplayNum();

return;
/*
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;
}
}

// BINARY OPERATORS:
if (IsBinOpCode(wParam))
if (IsBinOpCode(wParam))
{
// Change the operation if last input was operation.
if (IsBinOpCode(m_nLastCom))
Expand Down Expand Up @@ -298,9 +320,19 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam)
}

DisplayAnnounceBinaryOperator();

// consider this sub as +/- for the upcoming number
if ( wParam == IDC_SUB && m_currentVal.IsZero())
{
m_bFlagSign = true;
}

m_lastVal = m_currentVal;
m_nOpCode = (int)wParam;
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, m_fIntegerMode);
if (!m_bFlagSign)
{
m_HistoryCollector.AddBinOpToHistory(m_nOpCode, m_fIntegerMode);
}
m_bNoPrevEqu = m_bChangeOp = true;
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/CalcManager/CalcManager.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\Calculator</OutDir>
</PropertyGroup>
<PropertyGroup>
<GenerateManifest>false</GenerateManifest>
<GenerateProjectSpecificOutputFolder>true</GenerateProjectSpecificOutputFolder>
Expand Down Expand Up @@ -217,6 +219,7 @@
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
<ForcedIncludeFiles>pch.h</ForcedIncludeFiles>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
4 changes: 2 additions & 2 deletions src/CalcManager/Header Files/CCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@

#define IDC_EXP 127

#define IDC_OPENP 128
#define IDC_CLOSEP 129
#define IDC_OPENP 128 // Open parenthesis "("
#define IDC_CLOSEP 129 // Close parenthesis ")"

#define IDC_0 130 // The controls for 0 through F must be consecutive and in order
#define IDC_1 131
Expand Down
1 change: 1 addition & 0 deletions src/CalcManager/Header Files/CalcEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class CCalcEngine
bool m_bError; // Error flag.
bool m_bInv; // Inverse on/off flag.
bool m_bNoPrevEqu; /* Flag for previous equals. */
bool m_bFlagSign; /* Flag for +/- on next op */

uint32_t m_radix;
int32_t m_precision;
Expand Down
2 changes: 2 additions & 0 deletions src/CalcManager/Header Files/Rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace CalcEngine
Number const& P() const;
Number const& Q() const;

bool IsZero() const;

Rational operator-() const;
Rational& operator+=(Rational const& rhs);
Rational& operator-=(Rational const& rhs);
Expand Down
51 changes: 27 additions & 24 deletions src/Calculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProjectSection
EndProject
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
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalcManager", "CalcManager\CalcManager.vcxproj", "{311E866D-8B93-4609-A691-265941FEE101}"
EndProject
Expand Down Expand Up @@ -41,6 +44,30 @@ Global
Release|x86 = Release|x86
EndGlobalSection
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.Build.0 = Debug|ARM
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM64.ActiveCfg = Debug|ARM64
Expand Down Expand Up @@ -169,30 +196,6 @@ Global
{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.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.Build.0 = Debug|ARM
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM64.ActiveCfg = Debug|ARM64
Expand Down