Skip to content
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
8 changes: 8 additions & 0 deletions CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CalculatorControler.cs" />
<Compile Include="CalModel.cs" />
<Compile Include="Controler.cs" />
<Compile Include="Model.cs" />
<Compile Include="NewCalculatorEngine.cs" />
<Compile Include="SimpleCalculatorEngine .cs" />
<Compile Include="CalculatorEngine.cs" />
<Compile Include="ExtendForm.cs">
<SubType>Form</SubType>
Expand All @@ -61,6 +67,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RPNCalculatorEngine.cs" />
<Compile Include="View.cs" />
<EmbeddedResource Include="ExtendForm.resx">
<DependentUpon>ExtendForm.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -76,6 +83,7 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="ClassDiagram1.cd" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
56 changes: 56 additions & 0 deletions CPE200Lab1/CPE200Lab1/CalModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPE200Lab1
{
class CalculatorModel : Model
{
protected CalculatorEngine engine;
protected RPNCalculatorEngine RPNengine;
private string display;
public CalculatorModel()
{
engine = new CalculatorEngine();
RPNengine = new RPNCalculatorEngine();
}
public string Display()
{
return display;
}
public void calculator(string str)
{
string result = engine.calculate(str);
if (result is "E")
{
result = RPNengine.Calculate(str);
if (result is "E")
{
display = "Error";
}
else
{
display = result;
}
}
else
{
display = result;
}
NotifyAll();
}
public bool isNumber(string num)
{
return engine.isNumber(num);
}
public string calculate(string operate, string operand)
{
return engine.calculate(operate, operand);
}
public string calculate(string operate, string firstOperand, string secondOperand)
{
return calculate(operate, firstOperand, secondOperand);
}
}
}
21 changes: 21 additions & 0 deletions CPE200Lab1/CPE200Lab1/CalculatorControler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPE200Lab1
{
class CalculatorController : Controller
{
public CalculatorController()
{
}
public override void Calculate(string str)
{
foreach (CalculatorModel m in mList)
{
m.calculator(str);
}
}
}
}
41 changes: 26 additions & 15 deletions CPE200Lab1/CPE200Lab1/CalculatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace CPE200Lab1
{
public class CalculatorEngine
public class CalculatorEngine : NewCalculatorEngine
{
protected bool isNumber(string str)
{
Expand All @@ -26,7 +26,7 @@ protected bool isOperator(string str)
return false;
}

public string Process(string str)
public string calculate(string str)
{
//Split input string to multiple parts by space
List<string> parts = str.Split(' ').ToList<string>();
Expand All @@ -35,10 +35,10 @@ public string Process(string str)
while(parts.Count > 1)
{
//Check if the first three is ready for calcuation
if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2])))
{
if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2])))
{
return "E";
} else
} else
{
//Calculate the first three
result = calculate(parts[1], parts[0], parts[2], 4);
Expand All @@ -50,7 +50,7 @@ public string Process(string str)
}
return parts[0];
}
public string unaryCalculate(string operate, string operand, int maxOutputSize = 8)
public string calculate(string operate, string operand, int maxOutputSize = 8)
{
switch (operate)
{
Expand All @@ -64,17 +64,18 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize =
// 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)
if (parts[0].Length > maxOutputSize)
{
return "E";
}
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
return result.ToString("N" + remainLength);
}
case "1/x":
if(operand != "0")
//if(operand != "0")
try
{
double result;
string[] parts;
Expand All @@ -84,7 +85,7 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize =
// 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)
if (parts[0].Length > maxOutputSize)
{
return "E";
}
Expand All @@ -93,6 +94,10 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize =
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
}
catch (Exception E)
{
return "E";
}
break;
}
return "E";
Expand All @@ -110,8 +115,8 @@ public string calculate(string operate, string firstOperand, string secondOperan
return (Convert.ToDouble(firstOperand) * Convert.ToDouble(secondOperand)).ToString();
case "÷":
// Not allow devide be zero
if (secondOperand != "0")
{
// if (secondOperand != "0")
try{
double result;
string[] parts;
int remainLength;
Expand All @@ -120,15 +125,21 @@ public string calculate(string operate, string firstOperand, string secondOperan
// 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)
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);
//return result.ToString("N" + remainLength);
return Convert.ToDecimal(result).ToString("0.####");
}
catch (OverflowException E)
{
return "E";
}

break;
case "%":
//your code here
Expand Down
58 changes: 58 additions & 0 deletions CPE200Lab1/CPE200Lab1/ClassDiagram1.cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="CPE200Lab1.CalculatorEngine" Collapsed="true">
<Position X="1.75" Y="0.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAEIAAAgAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>CalculatorEngine.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.ExtendForm">
<Position X="4.25" Y="0.5" Width="1.5" />
<TypeIdentifier>
<HashCode>EABEABAARGIAAAAAAgyEAIAKIAACIABEQAABABww8A8=</HashCode>
<FileName>ExtendForm.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.MainForm">
<Position X="8.75" Y="1.25" Width="1.5" />
<TypeIdentifier>
<HashCode>AABEAQEgRmIBAAAABAyAAIAaIgACICBEAAIpEA4w8I8=</HashCode>
<FileName>MainForm.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.Program" Collapsed="true">
<Position X="8.75" Y="0.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAA=</HashCode>
<FileName>Program.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.RPNCalculatorEngine">
<Position X="0.5" Y="1.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>RPNCalculatorEngine.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.SimpleCalculatorEngine">
<Position X="2.75" Y="1.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>SimpleCalculatorEngine .cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.Properties.Resources" Collapsed="true">
<Position X="5.25" Y="1.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAABEAAAAQAAAAAAAAAAAAAAAIA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="CPE200Lab1.Properties.Settings" Collapsed="true">
<Position X="7" Y="1.5" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAIAAAAAABAAAAAAAAAAAAAAA=</HashCode>
</TypeIdentifier>
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>
33 changes: 33 additions & 0 deletions CPE200Lab1/CPE200Lab1/Controler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPE200Lab1
{
class Controller
{
protected ArrayList mList;
public Controller()
{
mList = new ArrayList();
}
public void AddModel(Model m)
{
mList.Add(m);
}
public virtual void Calculate(string str)
{
throw new NotImplementedException();
}
public virtual void isNumber(string str)
{
throw new NotImplementedException();
}
public virtual void isOperator(string str)
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions CPE200Lab1/CPE200Lab1/ExtendForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@

namespace CPE200Lab1
{
public partial class ExtendForm : Form
public partial class ExtendForm : Form, View
{

private bool isNumberPart = false;
private bool isContainDot = false;
private bool isSpaceAllowed = false;
private RPNCalculatorEngine engine;

Model model;
Controller controller;
public ExtendForm()
{
InitializeComponent();
engine = new RPNCalculatorEngine();
model = new CalculatorModel();
controller = new CalculatorController();
model.AttachObserver(this);
controller.AddModel(model);
}
public void Notify(Model m)
{
lblDisplay.Text = ((CalculatorModel)m).Display();
}

private bool isOperator(char ch)
Expand All @@ -30,6 +40,9 @@ private bool isOperator(char ch)
case '-':
case 'X':
case '÷':
case '%':
case '√':
case 'x':
return true;
}
return false;
Expand Down Expand Up @@ -101,7 +114,7 @@ private void btnClear_Click(object sender, EventArgs e)

private void btnEqual_Click(object sender, EventArgs e)
{
string result = engine.Process(lblDisplay.Text);
string result = engine.Calculate(lblDisplay.Text);
if (result is "E")
{
lblDisplay.Text = "Error";
Expand Down
Loading