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

600610741 HW 5 #561

Open
wants to merge 6 commits into
base: Lab2
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: 1 addition & 0 deletions CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RPNCalculatorEngine.cs" />
<Compile Include="SimpleCalculatorEngine.cs" />
<EmbeddedResource Include="ExtendForm.resx">
<DependentUpon>ExtendForm.cs</DependentUpon>
</EmbeddedResource>
Expand Down
71 changes: 71 additions & 0 deletions CPE200Lab1/CPE200Lab1/CalculatorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPE200Lab1
{
public class CalculatorController : Controller
{
public void BtnNumberPerform(string num)
{
((CalculatorModel)m).PerformBtnNumber(num);
}

public void OperatorPerform(string oper)
{
((CalculatorModel)m).PerformOperate(oper);
}

public void EqualPerform()
{
((CalculatorModel)m).PerformEqual();
}

public void ClearPerform()
{
((CalculatorModel)m).PerformClear();
}

public void DotPerform()
{
((CalculatorModel)m).PeformDot();
}

public void SignPerform()
{
((CalculatorModel)m).PerFormSign();
}

public void UnaryPerform(string unary)
{
((CalculatorModel)m).PerformUnary(unary);
}

public void PercentPerform()
{
((CalculatorModel)m).PerformPercent();
}

public void BackPerform()
{
((CalculatorModel)m).PerformBack();
}

public void ModifyMemory(string oper)
{
((CalculatorModel)m).PerformModifyMemory(oper);
}

public void RecallMemoryPeform()
{
((CalculatorModel)m).PerformMemoryRecall();
}

public void ClearMemoryPerform()
{
((CalculatorModel)m).PerformMemoryClear();
}
}
}
119 changes: 18 additions & 101 deletions CPE200Lab1/CPE200Lab1/CalculatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,123 +6,40 @@

namespace CPE200Lab1
{
public class CalculatorEngine
public class CalculatorEngine : SimpleCalculatorEngine
{
private bool isNumber(string str)
private double firstOperand;
private double secondOperand;

public void setFirstOperand(string num)
{
double retNum;
return Double.TryParse(str, out retNum);
firstOperand = Convert.ToDouble(num);
}

private bool isOperator(string str)
public void setsSeconOperand(string num)
{
switch(str) {
case "+":
case "-":
case "X":
case "÷":
return true;
}
return false;
secondOperand = Convert.ToDouble(num);
}

public string Process(string str)
public string calculate(string oper)
{
string[] parts = str.Split(' ');
if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2])))
{
return "E";
} else
{
return calculate(parts[1], parts[0], parts[2], 4);
}

return "E";
}
public string unaryCalculate(string operate, string operand, int maxOutputSize = 8)

public bool isUnaryOperator(string str)
{
switch (operate)
switch (str)
{
case "√":
{
double result;
string[] parts;
int remainLength;

result = Math.Sqrt(Convert.ToDouble(operand));
// split between integer part and fractional part
parts = result.ToString().Split('.');
// if integer part length is already break max output, return error
if (parts[0].Length > maxOutputSize)
{
return "E";
}
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
}
case "1/x":
if(operand != "0")
{
double result;
string[] parts;
int remainLength;

result = (1.0 / Convert.ToDouble(operand));
// split between integer part and fractional part
parts = result.ToString().Split('.');
// if integer part length is already break max output, return error
if (parts[0].Length > maxOutputSize)
{
return "E";
}
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
}
break;
return true;
}
return "E";
return false;
}

public string calculate(string operate, string firstOperand, string secondOperand, int maxOutputSize = 8)
{
switch (operate)
{
case "+":
return (Convert.ToDouble(firstOperand) + Convert.ToDouble(secondOperand)).ToString();
case "-":
return (Convert.ToDouble(firstOperand) - Convert.ToDouble(secondOperand)).ToString();
case "X":
return (Convert.ToDouble(firstOperand) * Convert.ToDouble(secondOperand)).ToString();
case "÷":
// Not allow devide be zero
if (secondOperand != "0")
{
double result;
string[] parts;
int remainLength;

result = (Convert.ToDouble(firstOperand) / Convert.ToDouble(secondOperand));
// split between integer part and fractional part
parts = result.ToString().Split('.');
// if integer part length is already break max output, return error
if (parts[0].Length > maxOutputSize)
{
return "E";
}
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
}
break;
case "%":
//your code here
break;
}
return "E";
}



}
}
Loading