466 lines
12 KiB
C#
466 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics.Eventing.Reader;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ElementaryMathematics
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
|
|
private bool isAdd = false;
|
|
private bool isSubtract = false;
|
|
private bool isMultiply = false;
|
|
private bool isDivide = false;
|
|
|
|
private Int32 numRunningCorrect = 0;
|
|
private Int32 numRunningIncorrect = 0;
|
|
|
|
private bool hasTried = false;
|
|
private bool isError = false;
|
|
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
|
|
pnlSummary.Visible = false;
|
|
lblNumCorrect.Text = "0";
|
|
lblNumIncorrect.Text = "0";
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void groupBox1_Enter(object sender, EventArgs e)
|
|
{
|
|
// throw new System.NotImplementedException();
|
|
}
|
|
|
|
private void radioButton1_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
// throw new System.NotImplementedException();
|
|
}
|
|
|
|
private void chkSummary_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
if (chkShowSummary.Checked)
|
|
{
|
|
pnlSummary.Visible = true;
|
|
}
|
|
else
|
|
{
|
|
pnlSummary.Visible = false;
|
|
}
|
|
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
decimal total = (numRunningCorrect - numRunningIncorrect) * 100;
|
|
|
|
MessageBox.Show("Your Score:" +
|
|
"\nCorrect: " + numRunningCorrect +
|
|
"\nIncorrect: " + numRunningIncorrect +
|
|
"\nTotal Score : " + (total.ToString()+"!"));
|
|
|
|
}
|
|
|
|
private void ChangeOperator(string op)
|
|
{
|
|
isAdd = false;
|
|
isSubtract = false;
|
|
isMultiply = false;
|
|
isDivide = false;
|
|
|
|
if (op == "+")
|
|
{
|
|
isAdd = true;
|
|
lblOperator.Text = op;
|
|
}else if (op == "-")
|
|
{
|
|
isSubtract = true;
|
|
lblOperator.Text = op;
|
|
}else if (op == "*")
|
|
{
|
|
isMultiply = true;
|
|
lblOperator.Text = op;
|
|
}else if (op == "/")
|
|
{
|
|
isDivide = true;
|
|
lblOperator.Text = op;
|
|
}
|
|
else
|
|
{
|
|
lblOperator.Text = "";
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
private void rdoAddition_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
ChangeOperator("+");
|
|
UpdateButtons();
|
|
isError = false;
|
|
hasTried = false;
|
|
}
|
|
|
|
private void rdoSubtraction_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
ChangeOperator("-");
|
|
UpdateButtons();
|
|
isError = false;
|
|
hasTried = false;
|
|
}
|
|
|
|
private void rdoMultiplication_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
ChangeOperator("*");
|
|
UpdateButtons();
|
|
isError = false;
|
|
hasTried = false;
|
|
}
|
|
|
|
private void rdoDivision_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
ChangeOperator("/");
|
|
UpdateButtons();
|
|
isError = false;
|
|
hasTried = false;
|
|
}
|
|
|
|
private void btnExit_Click(object sender, EventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
private ErrorProvider ep1 = new ErrorProvider();
|
|
|
|
private decimal Calculate(decimal a, decimal b)
|
|
{
|
|
|
|
decimal answer = 0;
|
|
|
|
|
|
try
|
|
{
|
|
|
|
if (isAdd)
|
|
{
|
|
answer = a + b;
|
|
hasTried = true;
|
|
return answer;
|
|
}else if (isSubtract)
|
|
{
|
|
answer = a - b;
|
|
hasTried = true;
|
|
return answer;
|
|
}else if (isMultiply)
|
|
{
|
|
answer = a * b;
|
|
hasTried = true;
|
|
return answer;
|
|
}else if (isDivide)
|
|
{
|
|
hasTried = true;
|
|
if (b != 0)
|
|
{
|
|
string newan;
|
|
answer = a / b;
|
|
newan = answer.ToString("F");
|
|
answer = decimal.Parse(newan);
|
|
return answer;
|
|
}
|
|
else
|
|
{
|
|
ep1.SetError(btnChkAnswer, "Cannot Divide by 0!");
|
|
isError = true;
|
|
answer = new decimal(null);
|
|
return answer;
|
|
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
throw new Exception();
|
|
}
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ep1.SetError(btnChkAnswer, "Calculation Error");
|
|
}
|
|
|
|
|
|
return new decimal(null);
|
|
}
|
|
|
|
private void Win()
|
|
{
|
|
|
|
lblMessage.Text = "Good Job!";
|
|
numRunningCorrect++;
|
|
lblNumCorrect.Text = numRunningCorrect.ToString();
|
|
}
|
|
|
|
private void Fail()
|
|
{
|
|
|
|
lblMessage.Text = "Try Again";
|
|
numRunningIncorrect++;
|
|
lblNumIncorrect.Text = numRunningIncorrect.ToString();
|
|
}
|
|
|
|
|
|
private void btnChkAnswer_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
ep1.Clear();
|
|
|
|
decimal a = 0;
|
|
decimal b = 0;
|
|
decimal c = 0;
|
|
|
|
try
|
|
{
|
|
|
|
a = decimal.Parse(txtA.Text);
|
|
|
|
try
|
|
{
|
|
|
|
b = decimal.Parse(txtB.Text);
|
|
|
|
try
|
|
{
|
|
|
|
c = decimal.Parse(txtC.Text);
|
|
|
|
try
|
|
{
|
|
|
|
if (c == Calculate(a,b))
|
|
{
|
|
|
|
Win();
|
|
|
|
}
|
|
else
|
|
{
|
|
Fail();
|
|
}
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
MessageBox.Show("NaN", "Error", MessageBoxButtons.OK);
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
ep1.SetError(txtC, "Invalid Value");
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
ep1.SetError(txtB, "Invalid Value");
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
ep1.SetError(txtA, "Invalid Value");
|
|
|
|
}
|
|
|
|
UpdateButtons();
|
|
|
|
}
|
|
|
|
private void txtA_TextChanged(object sender, EventArgs e)
|
|
{
|
|
isError = false;
|
|
hasTried = false;
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void txtB_TextChanged(object sender, EventArgs e)
|
|
{
|
|
isError = false;
|
|
hasTried = false;
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void txtC_TextChanged(object sender, EventArgs e)
|
|
{
|
|
isError = false;
|
|
hasTried = false;
|
|
UpdateButtons();
|
|
lblMessage.Text = "";
|
|
}
|
|
|
|
private void btnClear_Click(object sender, EventArgs e)
|
|
{
|
|
txtA.Clear();
|
|
txtB.Clear();
|
|
txtC.Clear();
|
|
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void btnReset_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
DialogResult btnDialog;
|
|
string strResponse;
|
|
|
|
strResponse = "Do you really want to reset all statistics?";
|
|
|
|
btnDialog = MessageBox.Show(strResponse, "Chapter Four", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
|
|
|
|
if (btnDialog == DialogResult.Yes)
|
|
{
|
|
btnClear_Click(sender, e);
|
|
|
|
numRunningCorrect = 0;
|
|
numRunningIncorrect = 0;
|
|
|
|
UpdateButtons();
|
|
rdoAddition.Select();
|
|
|
|
lblNumCorrect.Text = "0";
|
|
lblNumIncorrect.Text = "0";
|
|
}
|
|
|
|
}
|
|
|
|
private void UpdateButtons()
|
|
{
|
|
|
|
if (txtA.TextLength > 0 && txtB.TextLength > 0 && txtC.TextLength > 0 && !hasTried)
|
|
{
|
|
|
|
btnChkAnswer.Enabled = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
btnChkAnswer.Enabled = false;
|
|
}
|
|
|
|
if (hasTried)
|
|
{
|
|
btnShowCorrectAnswer.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
btnShowCorrectAnswer.Enabled = false;
|
|
}
|
|
|
|
if (txtA.TextLength > 0 || txtB.TextLength > 0 || txtC.TextLength > 0)
|
|
{
|
|
|
|
btnClear.Enabled = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
btnClear.Enabled = false;
|
|
}
|
|
|
|
if (numRunningCorrect > 0 || numRunningIncorrect > 0)
|
|
{
|
|
|
|
btnReset.Enabled = true;
|
|
btnPrint.Enabled = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
btnReset.Enabled = false;
|
|
btnPrint.Enabled = false;
|
|
}
|
|
|
|
if (!isError)
|
|
{
|
|
ep1.Clear();
|
|
}
|
|
|
|
}
|
|
|
|
private void btnShowCorrectAnswer_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
|
|
ep1.Clear();
|
|
|
|
decimal a = 0;
|
|
decimal b = 0;
|
|
decimal c = 0;
|
|
|
|
try
|
|
{
|
|
|
|
a = decimal.Parse(txtA.Text);
|
|
|
|
try
|
|
{
|
|
|
|
b = decimal.Parse(txtB.Text);
|
|
|
|
|
|
try
|
|
{
|
|
|
|
lblMessage.Text = "Correct Answer: " + Calculate(a, b).ToString("F");
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
ep1.SetError(btnChkAnswer, "Calculation Error");
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
ep1.SetError(txtB, "Invalid Value");
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
ep1.SetError(txtA, "Invalid Value");
|
|
|
|
}
|
|
|
|
UpdateButtons();
|
|
|
|
}
|
|
|
|
private void label1_Click(object sender, EventArgs e)
|
|
{
|
|
// throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
} |