i'm trying to create a calculator
After i rund the code... if i do a calculation like 9 x 9 i get result of 81
but when i try to use the result of 81 and divide by any number i get error System.ArgumentOutOfRangeException
9 x 9 = 81 then straigh / by any number i get error System.ArgumentOutOfRangeException
but
if i do type 81 and / by 9 i get correct answer of 9
the error comes after i do the first calculation...when i try to execute other calculation using the result i get that error
i tried to execute other calculation using the result of the first calculation
the code i try to execute is down below
namespace FormCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox.Text);
}
private void buttonReset_Click(object sender, EventArgs e)
{
previousOperation = Operation.None;
textBox.Clear();
}
private void buttonClear_Click(object sender, EventArgs e)
{
if(textBox.Text.Length > 0)
{
double d;
if (!double.TryParse(textBox.Text[textBox.Text.Length - 1].ToString(), out d))
{
previousOperation = Operation.None;
}
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1);
}
}
private void buttonDiv_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Div;
textBox.Text += (sender as Button).Text;
}
private void buttonMult_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Mul;
textBox.Text += (sender as Button).Text;
}
private void buttonSub_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Sub;
textBox.Text += (sender as Button).Text;
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Add;
textBox.Text += (sender as Button).Text;
}
private void PerformCalculation(Operation previousOperation)
{
List<double> listNumbers = new List<double>();
switch (previousOperation)
{
case Operation.Add:
listNumbers = textBox.Text.Split('+').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] + listNumbers[1]).ToString();
break;
case Operation.Sub:
listNumbers = textBox.Text.Split('-').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] - listNumbers[1]).ToString();
break;
case Operation.Mul:
listNumbers = textBox.Text.Split('*').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] * listNumbers[1]).ToString();
break;
case Operation.Div:
try
{
listNumbers = textBox.Text.Split('/').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] / listNumbers[1]).ToString();
break;
}
catch (DivideByZeroException)
{
textBox.Text = "kkkkkkkkk";
throw;
}
case Operation.None:
break;
default:
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox.Text += (sender as Button).Text;
}
enum Operation
{
Add,
Sub,
Mul,
Div,
None
}
static Operation previousOperation = Operation.None;
private void buttonResult_Click(object sender, EventArgs e)
{
if (previousOperation == Operation.None)
return;
else
PerformCalculation(previousOperation);
}
}
}