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

finished #577

Open
wants to merge 1 commit into
base: Lab1
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 @@ -45,6 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CalculatorEngine.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
236 changes: 236 additions & 0 deletions CPE200Lab1/CPE200Lab1/CalculatorEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPE200Lab1
{
class CalculatorEngine
{
private bool hasDot;
private bool isAllowBack;
private bool isAfterOperater;
private bool isAfterEqual;
private string firstOperand;
private string operate;

private string display = "0";
public string Display()
{
return display;
}
private void resetAll()
{
lblDisplay.Text = "0";
isAllowBack = true;
hasDot = false;
isAfterOperater = false;
isAfterEqual = false;
}

private 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";
}

public MainForm()
{
InitializeComponent();

resetAll();
}

private void btnNumber_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isAfterEqual)
{
resetAll();
}
if (isAfterOperater)
{
lblDisplay.Text = "0";
}
if (lblDisplay.Text.Length is 8)
{
return;
}
isAllowBack = true;
string digit = ((Button)sender).Text;
if (lblDisplay.Text is "0")
{
lblDisplay.Text = "";
}
lblDisplay.Text += digit;
isAfterOperater = false;
}

private void btnOperator_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isAfterOperater)
{
return;
}
operate = ((Button)sender).Text;
switch (operate)
{
case "+":
case "-":
case "X":
case "÷":
firstOperand = lblDisplay.Text;
isAfterOperater = true;
break;
case "%":
// your code here
break;
}
isAllowBack = false;
}

private void btnEqual_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
string secondOperand = lblDisplay.Text;
string result = calculate(operate, firstOperand, secondOperand);
if (result is "E" || result.Length > 8)
{
lblDisplay.Text = "Error";
}
else
{
lblDisplay.Text = result;
}
isAfterEqual = true;
}

private void btnDot_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isAfterEqual)
{
resetAll();
}
if (lblDisplay.Text.Length is 8)
{
return;
}
if (!hasDot)
{
lblDisplay.Text += ".";
hasDot = true;
}
}

private void btnSign_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isAfterEqual)
{
resetAll();
}
// already contain negative sign
if (lblDisplay.Text.Length is 8)
{
return;
}
if (lblDisplay.Text[0] is '-')
{
lblDisplay.Text = lblDisplay.Text.Substring(1, lblDisplay.Text.Length - 1);
}
else
{
lblDisplay.Text = "-" + lblDisplay.Text;
}
}

private void btnClear_Click(object sender, EventArgs e)
{
resetAll();
}

private void btnBack_Click(object sender, EventArgs e)
{
if (lblDisplay.Text is "Error")
{
return;
}
if (isAfterEqual)
{
return;
}
if (!isAllowBack)
{
return;
}
if (lblDisplay.Text != "0")
{
string current = lblDisplay.Text;
char rightMost = current[current.Length - 1];
if (rightMost is '.')
{
hasDot = false;
}
lblDisplay.Text = current.Substring(0, current.Length - 1);
if (lblDisplay.Text is "" || lblDisplay.Text is "-")
{
lblDisplay.Text = "0";
}
}
}
}
}

}
}
8 changes: 3 additions & 5 deletions CPE200Lab1/CPE200Lab1/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ public partial class MainForm : Form
private bool isAfterEqual;
private string firstOperand;
private string operate;
private CalculatorEngine engine = new CalculatorEngine();

private void resetAll()
{
lblDisplay.Text = "0";
isAllowBack = true;
hasDot = false;
isAfterOperater = false;
isAfterEqual = false;
lblDisplay.Text = engine.Display();
engine.resetAll();
}

private string calculate(string operate, string firstOperand, string secondOperand, int maxOutputSize = 8)
Expand Down