CNSA-212-Personal/ElementaryMathematics/Form1.cs

253 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
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;
public Form1()
{
InitializeComponent();
//test
}
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 checkBox1_CheckedChanged(object sender, EventArgs e)
{
// throw new System.NotImplementedException();
}
private void button1_Click(object sender, EventArgs e)
{
// throw new System.NotImplementedException();
}
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("+");
}
private void rdoSubtraction_CheckedChanged(object sender, EventArgs e)
{
ChangeOperator("-");
}
private void rdoMultiplication_CheckedChanged(object sender, EventArgs e)
{
ChangeOperator("*");
}
private void rdoDivision_CheckedChanged(object sender, EventArgs e)
{
ChangeOperator("/");
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private ErrorProvider ep1 = new ErrorProvider();
private decimal Calculate(decimal a, decimal b, decimal c)
{
decimal answer = 0;
try
{
if (isAdd)
{
answer = a + b;
}else if (isSubtract)
{
answer = a - b;
}else if (isMultiply)
{
answer = a * b;
}else if (isDivide)
{
answer = a / b;
}
else
{
throw new Exception();
}
}
catch (Exception e)
{
MessageBox.Show("Whoops!");
}
return answer;
}
private void Win()
{
lblMessage.Text = "Good Job!";
numRunningCorrect++;
}
private void Fail()
{
lblMessage.Text = "Try Again";
numRunningIncorrect++;
}
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,c))
{
Win();
}
else
{
Fail();
}
}
catch (Exception exception)
{
ep1.SetError(btnChkAnswer, "Calculation Error");
}
}
catch (Exception exception)
{
ep1.SetError(txtC, "Invalid Value");
}
}
catch (Exception exception)
{
ep1.SetError(txtB, "Invalid Value");
}
}
catch (Exception exception)
{
ep1.SetError(txtA, "Invalid Value");
}
}
private void txtA_TextChanged(object sender, EventArgs e)
{
ep1.Clear();
}
private void txtB_TextChanged(object sender, EventArgs e)
{
ep1.Clear();
}
private void txtC_TextChanged(object sender, EventArgs e)
{
ep1.Clear();
}
}
}