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

Lab4 Section 801 #47

Open
wants to merge 15 commits into
base: Lab3
Choose a base branch
from
9 changes: 9 additions & 0 deletions CPE200Lab1/CPE200Lab1.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1", "CPE200Lab1\CP
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1Tests", "CPE200Lab1Tests\CPE200Lab1Tests.csproj", "{C92330F4-DC76-46B0-970C-73126DE55500}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1Test", "CPE200Lab1Test\CPE200Lab1Test.csproj", "{0E964631-C30F-4089-ADD6-D2F374C1E02D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -17,10 +19,17 @@ Global
{C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Release|Any CPU.Build.0 = Release|Any CPU

{C92330F4-DC76-46B0-970C-73126DE55500}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C92330F4-DC76-46B0-970C-73126DE55500}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C92330F4-DC76-46B0-970C-73126DE55500}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C92330F4-DC76-46B0-970C-73126DE55500}.Release|Any CPU.Build.0 = Release|Any CPU

{0E964631-C30F-4089-ADD6-D2F374C1E02D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E964631-C30F-4089-ADD6-D2F374C1E02D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E964631-C30F-4089-ADD6-D2F374C1E02D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E964631-C30F-4089-ADD6-D2F374C1E02D}.Release|Any CPU.Build.0 = Release|Any CPU

EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CalculatorEngine.cs" />
<Compile Include="ExtendFromController.cs" />
<Compile Include="ExtendForm.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -58,6 +59,11 @@
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="partial.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainFormController.cs" />
<Compile Include="SimpleCalculatorEngine.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RPNCalculatorEngine.cs" />
Expand Down
34 changes: 29 additions & 5 deletions CPE200Lab1/CPE200Lab1/CalculatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected bool isOperator(string str)
return false;
}

public string Process(string str)
public virtual string Process(string str)
{
//Split input string to multiple parts by space
List<string> parts = str.Split(' ').ToList<string>();
Expand All @@ -48,7 +48,14 @@ public string Process(string str)
parts.Insert(0, result);
}
}
return parts[0];
if (!(isNumber(parts[0])))
{
return "E";
}
else {
return parts[0];
}

}
public string unaryCalculate(string operate, string operand, int maxOutputSize = 8)
{
Expand All @@ -71,7 +78,14 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize =
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
if (remainLength <= 6)
{
return result.ToString();
}
else
{
return result.ToString("N" + remainLength);
}
}
case "1/x":
if(operand != "0")
Expand All @@ -91,8 +105,17 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize =
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
if (remainLength <= 6)
{
return result.ToString();
}
else
{
return result.ToString("N" + remainLength);
}

}

break;
}
return "E";
Expand Down Expand Up @@ -127,7 +150,8 @@ public string calculate(string operate, string firstOperand, string secondOperan
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);

return result.ToString("0.####");
}
break;
case "%":
Expand Down
138 changes: 21 additions & 117 deletions CPE200Lab1/CPE200Lab1/ExtendForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,161 +12,65 @@ namespace CPE200Lab1
{
public partial class ExtendForm : Form
{
private bool isNumberPart = false;
private bool isContainDot = false;
private bool isSpaceAllowed = false;
private RPNCalculatorEngine engine;
private ExtendFromController controller;

public ExtendForm()
{
InitializeComponent();
engine = new RPNCalculatorEngine();
controller = new ExtendFromController();
}

private bool isOperator(char ch)
public void Display()
{
switch(ch) {
case '+':
case '-':
case 'X':
case '÷':
return true;
}
return false;
lblDisplay.Text = controller.controller();
}

private void btnNumber_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (lblDisplay.Text is "0")
{
lblDisplay.Text = "";
}
if (!isNumberPart)
{
isNumberPart = true;
isContainDot = false;
}
lblDisplay.Text += ((Button)sender).Text;
isSpaceAllowed = true;
controller.controller("Number", ((Button)sender).Text);
Display();
}

private void btnBinaryOperator_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
isNumberPart = false;
isContainDot = false;
string current = lblDisplay.Text;
if (current[current.Length - 1] != ' ' || isOperator(current[current.Length - 2]))
{
lblDisplay.Text += " " + ((Button)sender).Text + " ";
isSpaceAllowed = false;
}
controller.controller("BOperator", ((Button)sender).Text);
Display();
}

private void btnBack_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
// check if the last one is operator
string current = lblDisplay.Text;
if (current[current.Length - 1] is ' ' && current.Length > 2 && isOperator(current[current.Length - 2]))
{
lblDisplay.Text = current.Substring(0, current.Length - 3);
} else
{
lblDisplay.Text = current.Substring(0, current.Length - 1);
}
if (lblDisplay.Text is "")
{
lblDisplay.Text = "0";
}
controller.controller("Back");
Display();
}

private void btnClear_Click(object sender, EventArgs e)
{
lblDisplay.Text = "0";
isContainDot = false;
isNumberPart = false;
isSpaceAllowed = false;
controller.controller("reset");
Display();
}

private void btnEqual_Click(object sender, EventArgs e)
{
string result = engine.Process(lblDisplay.Text);
if (result is "E")
{
lblDisplay.Text = "Error";
} else
{
lblDisplay.Text = result;
isSpaceAllowed = true;
isContainDot = false;
isNumberPart = true;
}
controller.controller("Equal");
Display();
}

private void btnSign_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isNumberPart)
{
return;
}
string current = lblDisplay.Text;
if (current is "0")
{
lblDisplay.Text = "-";
} else if (current[current.Length - 1] is '-')
{
lblDisplay.Text = current.Substring(0, current.Length - 1);
if (lblDisplay.Text is "")
{
lblDisplay.Text = "0";
}
} else
{
lblDisplay.Text = current + "-";
}
isSpaceAllowed = false;
controller.controller("Sign");
Display();
}

private void btnDot_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if(!isContainDot)
{
isContainDot = true;
lblDisplay.Text += ".";
isSpaceAllowed = false;
}
controller.controller("Dot");
Display();
}

private void btnSpace_Click(object sender, EventArgs e)
{
if(lblDisplay.Text is "Error")
{
return;
}
if(isSpaceAllowed)
{
lblDisplay.Text += " ";
isSpaceAllowed = false;
}
controller.controller("Space");
Display();
}
}
}
}
45 changes: 45 additions & 0 deletions CPE200Lab1/CPE200Lab1/ExtendFromController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPE200Lab1
{
class ExtendFromController : RPNCalculatorEngine
{
public string controller(string cont = "", string intput = "")
{
switch (cont)
{
case "":
break;
case "Number":
handleNumber(intput);
break;
case "BOperator":
handleBinaryOperator(intput);
break;
case "Back":
handleBack();
break;
case "reset":
resetAll();
break;
case "Equal":
handleEqual();
break;
case "Sign":
handleSign();
break;
case "Dot":
handleDot();
break;
case "Space":
handleSpace();
break;
}
return Display();
}
}
}
49 changes: 49 additions & 0 deletions CPE200Lab1/CPE200Lab1/MainFormController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPE200Lab1
{
class MainFormController : SimpleCalculatorEngine
{

public string controller(string cont = "", string intput = "")
{
switch (cont)
{
case "":
break;
case "Number":
handleNumber(intput);
break;
case "UOperator":
handleUnaryOperator(intput);
break;
case "Operator":
handleOperator(intput);
break;
case "Equal":
handleEqual();
break;
case "Dot":
handleDot();
break;
case "Sign":
handleSign();
break;
case "reset":
resetAll();
break;
case "Back":
handleBack();
break;
case "M":
handleMP_MC_MM_MR(intput);
break;
}
return Display();
}
}
}
Loading