//[ExpectedException(typeof(FormatException))] public void ParseTest1() { string[] datas = new string[] { " 2 ", "3+0i", "+-i", "-2-2+i", "1.230.8i", "4i+8", "6+5ii", "..2i", "..2+3i", "a+3i", "a+bi" }; int n = 0; foreach (string input in datas) { try { ComplexNumber6335 expected = ComplexNumber6335.Parse(input); } catch (FormatException) { n++; } } Assert.AreEqual(datas.Length, n); }
//验证输入规范性,同时获取textbox内容 public void isValid() { try { isException = true; if (tabControl1.SelectedIndex == 0) { real_figure_1 = Convert.ToDouble(tbRfigure1.Text.Replace(" ", string.Empty)); //获取复数1 unreal_figure_1 = Convert.ToDouble(tbURfigure1.Text.Replace(" ", string.Empty)); real_figure_2 = Convert.ToDouble(tbRfigure2.Text.Replace(" ", string.Empty)); //获取复数2 unreal_figure_2 = Convert.ToDouble(tbURfigure2.Text.Replace(" ", string.Empty)); complexNumber1 = new ComplexNumber6335(real_figure_1, unreal_figure_1); complexNumber2 = new ComplexNumber6335(real_figure_2, unreal_figure_2); } else { complexNumber1 = ComplexNumber6335.Parse(tbcomplex1.Text); complexNumber2 = ComplexNumber6335.Parse(tbcomplex2.Text); } } catch (Exception) { MessageBox.Show("Bug千万条,报错第一条。编程不规范,头发两行泪。", "北京第三区交通委提醒您"); isException = false; } }
public void ParseTest() { TestCase[] datas = new TestCase[] { new TestCase(" i ", 0, 1), new TestCase(" - i", 0, -1), new TestCase(" + i", 0, 1), new TestCase(" +8.5 i", 0, 8.5), new TestCase("02+ i ", 2, 1), new TestCase("2 - i", 2, -1), new TestCase(" -02- i", -2, -1), new TestCase(" -2+ i", -2, 1), new TestCase("2 +3i", 2, 3), new TestCase("2-3 i", 2, -3), new TestCase("- 2-3 i", -2, -3), new TestCase("- .2-3 i", -0.2, -3), new TestCase("- 2-.3 i", -2, -0.3), new TestCase("-1. 2-2.3 i", -1.2, -2.3), new TestCase("+2.5+8.3 i", 2.5, 8.3), }; foreach (TestCase data in datas) { ComplexNumber6335 expected = ComplexNumber6335.Parse(data.Input); Assert.AreEqual(new ComplexNumber6335(data.A, data.B), expected); } }
//Subtraction event private void rbSub_Click(object sender, EventArgs e) { isValid(); //验证 if (isException) { complex = complexNumber1 - complexNumber2; //调用重载后的 - 运算 tbResult.Text = $"减法运算:{complexNumber1.ToString()}-({complexNumber2.ToString()})" + $"={complex.ToString()}"; } }
//Addition event private void rbAdd_Click(object sender, EventArgs e) { isValid(); //验证 if (isException) { complex = complexNumber1 + complexNumber2; //调用重载后的 + 运算 tbResult.Text = $"加法运算:{complexNumber1.ToString()}+" + $"{(complexNumber2.A < 0 ? "(" + complexNumber2.ToString() + ")" : complexNumber2.ToString())}" + $"={complex.ToString()}"; //中间的三目运算表达式表示当第二个复数的实部为负时,加括号 } }