private void String2Complex(String str, ref ComplexCal num) { Regex regex = new Regex(@"\d*\,?\d+[+|-]\d*\,?\d+i"); if (!regex.IsMatch(str)) { label2.Text = "Некорректные данные"; return; } String n1 = null, n2 = null; Boolean minus_sign = false; int i, index = 0, first_index = 0; if (str[0] == '-' || str[0] == '+') { n1 += str[0]; first_index = 1; } for (i = first_index; i < str.Length; i++) { if (str[i] == '+' || str[i] == '-') { num.Re = Convert.ToSingle(n1); minus_sign = (str[i] == '-') ? true : false; index = i + 1; break; } n1 += str[i]; } if (minus_sign) { n2 = "-"; } for (i = index; i < str.Length; i++) { if (str[i] == 'i') { num.Im = Convert.ToSingle(n2); break; } n2 += str[i]; } }
private void button2_Click(object sender, EventArgs e) { String2Complex(textBox3.Text, ref left_num); String2Complex(textBox4.Text, ref right_num); if (!(label2.Text == "Неверно введены данные!")) { res = left_num - right_num; if (res.Im >= 0) { label2.Text = res.Re.ToString() + "+" + res.Im.ToString() + "i"; } else { label2.Text = res.Re.ToString() + res.Im.ToString() + "i"; } label8.Text = res.GetAbs().ToString(); label10.Text = res.GetArg().ToString(); } }