private void calculateButton_Click(object sender, EventArgs e) { string r1 = realTextBox1.Text; string i1 = imaginaryTextBox1.Text; string r2 = realTextBox2.Text; string i2 = imaginaryTextBox2.Text; var charsToRemove = new string[] { "i", " " }; foreach (var c in charsToRemove) { i1 = i1.Replace(c, string.Empty); i2 = i2.Replace(c, string.Empty); } try { double.TryParse(r1, out double realNumber1); double.TryParse(i1, out double imaginaryNumber1); double.TryParse(r2, out double realNumber2); double.TryParse(i2, out double imaginaryNumber2); if (operatorButton1.Text == "-") { imaginaryNumber1 = 0.0 - imaginaryNumber1; } if (operatorButton2.Text == "-") { imaginaryNumber2 = 0.0 - imaginaryNumber2; } ComplexData complexNumber1 = new ComplexData(realNumber1, imaginaryNumber1); ComplexData complexNumber2 = new ComplexData(realNumber2, imaginaryNumber2); if (operatorLabel.Text == "+") { ComplexData answer = complexArithmetic.Calculate(complexNumber1, complexNumber2, new Func <ComplexData, ComplexData, ComplexData>(complexArithmetic.Add)); if (answer.ImaginaryNumber < 0) { operatorAnswerTextBox.Text = "-"; answer.ImaginaryNumber = 0 - answer.ImaginaryNumber; } else { operatorAnswerTextBox.Text = "+"; } decimal realNumber = Convert.ToDecimal(answer.RealNumber); decimal imaginaryNumber = Convert.ToDecimal(answer.ImaginaryNumber); realAnswerTextBox.Text = Decimal.Round(realNumber, 2).ToString(); imaginaryAnswerTextBox.Text = Decimal.Round(imaginaryNumber).ToString() + "i"; } else if (operatorLabel.Text == "-") { ComplexData answer = complexArithmetic.Calculate(complexNumber1, complexNumber2, new Func <ComplexData, ComplexData, ComplexData>(complexArithmetic.Subtract)); if (answer.ImaginaryNumber < 0) { operatorAnswerTextBox.Text = "-"; answer.ImaginaryNumber = 0 - answer.ImaginaryNumber; } else { operatorAnswerTextBox.Text = "+"; } decimal realNumber = Convert.ToDecimal(answer.RealNumber); decimal imaginaryNumber = Convert.ToDecimal(answer.ImaginaryNumber); realAnswerTextBox.Text = Decimal.Round(realNumber, 2).ToString(); imaginaryAnswerTextBox.Text = Decimal.Round(imaginaryNumber).ToString() + "i"; } else if (operatorLabel.Text == "*") { ComplexData answer = complexArithmetic.Calculate(complexNumber1, complexNumber2, new Func <ComplexData, ComplexData, ComplexData>(complexArithmetic.Multiply)); if (answer.ImaginaryNumber < 0) { operatorAnswerTextBox.Text = "-"; answer.ImaginaryNumber = 0 - answer.ImaginaryNumber; } else { operatorAnswerTextBox.Text = "+"; } decimal realNumber = Convert.ToDecimal(answer.RealNumber); decimal imaginaryNumber = Convert.ToDecimal(answer.ImaginaryNumber); realAnswerTextBox.Text = Decimal.Round(realNumber, 2).ToString(); imaginaryAnswerTextBox.Text = Decimal.Round(imaginaryNumber).ToString() + "i"; } else if (operatorLabel.Text == "/") { ComplexData answer = complexArithmetic.Calculate(complexNumber1, complexNumber2, new Func <ComplexData, ComplexData, ComplexData>(complexArithmetic.Divide)); if (answer.ImaginaryNumber < 0) { operatorAnswerTextBox.Text = "-"; answer.ImaginaryNumber = 0 - answer.ImaginaryNumber; } else { operatorAnswerTextBox.Text = "+"; } decimal realNumber = Convert.ToDecimal(answer.RealNumber); decimal imaginaryNumber = Convert.ToDecimal(answer.ImaginaryNumber); realAnswerTextBox.Text = Decimal.Round(realNumber, 2).ToString(); imaginaryAnswerTextBox.Text = Decimal.Round(imaginaryNumber).ToString() + "i"; } } catch (FormatException) { MessageBox.Show("You entered an invalid input."); } }
public ComplexData Calculate(ComplexData cn1, ComplexData cn2, Func <ComplexData, ComplexData, ComplexData> calc) { return(calc(cn1, cn2)); }
public ComplexData Divide(ComplexData cn1, ComplexData cn2) { return(cn1 / cn2); }
public ComplexData Multiply(ComplexData cn1, ComplexData cn2) { return(cn1 * cn2); }
public ComplexData Subtract(ComplexData cn1, ComplexData cn2) { return(cn1 - cn2); }
public ComplexData Add(ComplexData cn1, ComplexData cn2) { return(cn1 + cn2); }