public ComplexNumber() { realPart = 0; imaginaryPart = 0; }
public ComplexNumber(Fractional real) { realPart = real; imaginaryPart = 0; }
public ComplexNumber(Fractional real, Fractional imaginary) { realPart = real; imaginaryPart = imaginary; }
public ComplexNumber(string num) { realPart = 0; imaginaryPart = 0; string[] complexStr; if (num.Contains("+")) { complexStr = num.Replace(" ", "").Split('+'); } else if (num.Contains("-")) { complexStr = num.Replace(" ", "").Split('-'); complexStr[complexStr.Length - 1] = "-" + complexStr[complexStr.Length - 1]; } else { complexStr = new[] { num }; } if (complexStr.Length == 0) { imaginaryPart = 0; realPart = 0; } if (complexStr.Length == 1) { if (complexStr[0].EndsWith("i")) { imaginaryPart = complexStr[0].Equals("i") ? 1 : new Fractional(complexStr[0].Remove(complexStr[0].Length - 1)); } else { realPart = new Fractional(complexStr[0]); } } else if (complexStr.Length == 2) { if (complexStr[0].EndsWith("i")) { imaginaryPart = complexStr[0].Equals("i") ? 1 : new Fractional(complexStr[0].Remove(complexStr[0].Length - 1)); realPart = new Fractional(complexStr[1]); } if (complexStr[1].EndsWith("i")) { imaginaryPart = complexStr[0].Equals("i") ? 1 : new Fractional(complexStr[1].Remove(complexStr[1].Length - 1)); realPart = new Fractional(complexStr[0]); } } else if (complexStr.Length > 2) { throw new Exception("未能识别的复数形式:" + num + "\n请使用以下格式:\"a + bi\""); } }