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

600612159 Lab2 #574

Open
wants to merge 2 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
8 changes: 7 additions & 1 deletion CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RPNCalculatorEngine.cs" />
<Compile Include="RPNForm.cs">
<SubType>Form</SubType>
</Compile>
<EmbeddedResource Include="ExtendForm.resx">
<DependentUpon>ExtendForm.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -86,7 +90,9 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
142 changes: 137 additions & 5 deletions CPE200Lab1/CPE200Lab1/CalculatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

namespace CPE200Lab1
{
class CalculatorEngine
public class CalculatorEngine
{
private bool isNumber(string str)
protected bool isNumberPart = false;
protected bool isContainDot = false;
protected bool isSpaceAllowed = false;
private string display = "0";
protected bool isNumber(string str)
{
double retNum;
return Double.TryParse(str, out retNum);
}

private bool isOperator(string str)
protected bool isOperator(string str)
{
switch(str) {
case "+":
Expand All @@ -26,7 +30,7 @@ private bool isOperator(string str)
return false;
}

public string Process(string str)
protected virtual string Process(string str)
{
string[] parts = str.Split(' ');
if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2])))
Expand Down Expand Up @@ -122,7 +126,135 @@ public string calculate(string operate, string firstOperand, string secondOperan
//your code here
break;
}
return "E";
return operate;
}

protected bool isOperator(char ch)
{
switch (ch)
{
case '+':
case '-':
case 'X':
case '÷':
return true;
}
return false;
}

public void Num_click(string num)
{
if (display is "0")
{
display = "";
}
if (!isNumberPart)
{
isNumberPart = true;
isContainDot = false;
}
display += num;
isSpaceAllowed = true;
}

public void Binary(string str)
{
isNumberPart = false;
isContainDot = false;
string current = display;
if (current[current.Length - 1] != ' ' || isOperator(current[current.Length - 2]))
{
display += " " + str + " ";
isSpaceAllowed = false;
}
}

public void Back()
{
string current = display;
if (current[current.Length - 1] is ' ' && current.Length > 2 && isOperator(current[current.Length - 2]))
{
display = current.Substring(0, current.Length - 3);
}
else
{
display = current.Substring(0, current.Length - 1);
}
if (display is "")
{
display = "0";
}
}

public void Clear()
{
display = "0";
isContainDot = false;
isNumberPart = false;
isSpaceAllowed = false;
}

public void Equal()
{
string result = Process(display);
if (result is "E")
{
display = "Error";
}
else
{
display = result;
}
}

public void Sign()
{
if (!isNumberPart)
{
return;
}
string current = display;
if (current is "0")
{
display = "-";
}
else if (current[0] is '-')
{
display = current.Substring(1, current.Length-1);
if (display is "")
{
display = "0";
}
}
else
{
display = "-" + current;
}
//isSpaceAllowed = false;
}

public void Dot()
{
if (!isContainDot)
{
isContainDot = true;
display += ".";
isSpaceAllowed = false;
}
}

public void Space()
{
if (isSpaceAllowed)
{
display += " ";
isSpaceAllowed = false;
}
}

public string Display()
{
return display;
}
}
}
110 changes: 22 additions & 88 deletions CPE200Lab1/CPE200Lab1/ExtendForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,18 @@ namespace CPE200Lab1
{
public partial class ExtendForm : Form
{
private bool isNumberPart = false;
private bool isContainDot = false;
private bool isSpaceAllowed = false;
private CalculatorEngine engine;
protected CalculatorEngine engine;

public ExtendForm()
{
InitializeComponent();
engine = new CalculatorEngine();
Display();
}

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

private void btnNumber_Click(object sender, EventArgs e)
Expand All @@ -41,17 +32,8 @@ private void btnNumber_Click(object sender, EventArgs e)
{
return;
}
if (lblDisplay.Text is "0")
{
lblDisplay.Text = "";
}
if (!isNumberPart)
{
isNumberPart = true;
isContainDot = false;
}
lblDisplay.Text += ((Button)sender).Text;
isSpaceAllowed = true;
engine.Num_click(((Button)sender).Text);
Display();
}

private void btnBinaryOperator_Click(object sender, EventArgs e)
Expand All @@ -60,14 +42,8 @@ private void btnBinaryOperator_Click(object sender, EventArgs e)
{
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;
}
engine.Binary(((Button)sender).Text);
Display();
}

private void btnBack_Click(object sender, EventArgs e)
Expand All @@ -77,38 +53,21 @@ private void btnBack_Click(object sender, EventArgs e)
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";
}
engine.Back();
Display();
}

private void btnClear_Click(object sender, EventArgs e)
{
lblDisplay.Text = "0";
isContainDot = false;
isNumberPart = false;
isSpaceAllowed = false;
engine.Clear();
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;
}
//if()
engine.Equal();
Display();
}

private void btnSign_Click(object sender, EventArgs e)
Expand All @@ -117,26 +76,8 @@ private void btnSign_Click(object sender, EventArgs e)
{
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;
engine.Sign();
Display();
}

private void btnDot_Click(object sender, EventArgs e)
Expand All @@ -145,12 +86,8 @@ private void btnDot_Click(object sender, EventArgs e)
{
return;
}
if(!isContainDot)
{
isContainDot = true;
lblDisplay.Text += ".";
isSpaceAllowed = false;
}
engine.Dot();
Display();
}

private void btnSpace_Click(object sender, EventArgs e)
Expand All @@ -159,11 +96,8 @@ private void btnSpace_Click(object sender, EventArgs e)
{
return;
}
if(isSpaceAllowed)
{
lblDisplay.Text += " ";
isSpaceAllowed = false;
}
engine.Space();
Display();
}
}
}
2 changes: 1 addition & 1 deletion CPE200Lab1/CPE200Lab1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ExtendForm());
Application.Run(new RPNForm());
}
}
}
Loading