//重载运算符:- public static ComplexNumber6335 operator -(ComplexNumber6335 c1, ComplexNumber6335 c2) { ComplexNumber6335 c3 = new ComplexNumber6335 { A = c1.A - c2.A, B = c1.B - c2.B }; return(c3); }
//重写Equal() public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj is ComplexNumber6335) { ComplexNumber6335 complexNumber = (ComplexNumber6335)obj; return(complexNumber.A == A && complexNumber.B == B); } return(false); }
/// <summary> /// 解析思路 /// 1、先解析到复数虚部,如果没有虚部则匹配失败 /// 2、将源字符串中的虚部剔除 /// 3、根据实际匹配情况进行赋值 /// </summary> /// <param name="input">value</param> /// <returns>ComplexNumber6335</returns> public static ComplexNumber6335 Parse(string input) { input = input.Replace(" ", string.Empty); ComplexNumber6335 complex = new ComplexNumber6335(); /* 正则匹配规则 * (?<=(\-|\+?)\d*\.?\d*) 零宽度正回顾后发断言:确保匹配目标前方是实部,但不匹配实部 * (\-|\+?)\d*\.?\d*i 匹配虚部字符串 * (?!(\-|\+)\d) 零宽度负预测先行断言:确保匹配目标后方没有其他数字 */ Regex rex_real = new Regex(@"(?<=(\-|\+?)\d*\.?\d*)(\-|\+?)\d*\.?\d*i(?!(\-|\+)\d)"); try { Match m1 = rex_real.Match(input); input = m1.Success ? input.Replace(m1.Value, string.Empty) : input; if (m1.Value.Contains("i")) { if (input != string.Empty) { complex.A = Convert.ToDouble(input); } else { complex.A = 0.0; } } else { throw new FormatException(); } /* 判断 * 1、如果匹配到了虚部 * 2、虚部不为0 * 否则 * 抛异常 */ if (m1.Success && !Regex.IsMatch(m1.Value, @"(?<=(\-|\+?))[0]+\.[0]+|[0]+|\.[0](?=i)")) { if (Regex.IsMatch(m1.Value, @"\-i")) { complex.B = -1.0; } else if (Regex.IsMatch(m1.Value, @"^\+?i")) { complex.B = 1.0; } else { complex.B = Convert.ToDouble(m1.Value.Replace("i", "")); } } else { throw new FormatException(); } } catch (FormatException) { throw new FormatException(); } return(complex); }