Как реализовать решение квадратного уравнения, если дискриминант отрицательный? - C#
Формулировка задачи:
Здравствуйте! Подскажите, пожалуйста, как реализовать решение квадратного уравнения, если дискриминант отрицательный? Очень прошу о помощи, вся программа рабоатет нормально, а с комплексными числами не могу справиться..
Решение задачи: «Как реализовать решение квадратного уравнения, если дискриминант отрицательный?»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SquareRoot
{
public class Complex : IComparable<Complex>
{
public double Real { get; set; }
public double Imaginary { get; set; }
public Complex()
{
}
public Complex(double real)
{
Real = real;
Imaginary = 0;
}
public Complex(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex()
{
Imaginary = a.Imaginary + b.Imaginary,
Real = a.Real + b.Real
};
}
public static Complex operator -(Complex a, Complex b)
{
return new Complex()
{
Imaginary = a.Imaginary - b.Imaginary,
Real = a.Real - b.Real
};
}
public static Complex operator +(Complex a, double k)
{
return new Complex()
{
Imaginary = a.Imaginary,
Real = a.Real + k
};
}
public static Complex operator -(Complex a, double k)
{
return new Complex()
{
Imaginary = a.Imaginary,
Real = a.Real - k
};
}
// Перегрузка оператора равенства. ВАЖНО, т.к. сравниваются числа с плавающей запятой, то сравнивать нужно их разницу по модулю
//
public static bool operator ==(Complex a, Complex b)
{
if ((Math.Abs(a.Imaginary - b.Imaginary) < 0.0000001 && Math.Abs(a.Real - b.Real) < 0.0000001))
{
return true;
}
return false;
}
public static bool operator !=(Complex a, Complex b)
{
if (Math.Abs(a.Imaginary - b.Imaginary) > 0.0000001 && Math.Abs(a.Real - b.Real) > 0.0000001)
{
return true;
}
return false;
}
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
Complex n = obj as Complex;
if ((System.Object)n == null)
{
return false;
}
return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
}
public bool Equals(Complex n)
{
if ((object)n == null)
{
return false;
}
return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
}
// Указаний по реализации нет. Реализован таким образом
public override int GetHashCode()
{
return (int)Imaginary ^ (int)Real;
}
public override string ToString()
{
if (Imaginary == 0) return string.Format("{0}", Math.Round(Real, 6));
return string.Format("{0} + {1}i", Math.Round(Real, 6), Math.Round(Imaginary, 6));
}
// Возможно лучшее решение. Т.к. указаний не дано по сравнению комплексных чисел (только равенства и неравенство),
// то реализовано таким образом
public int CompareTo(Complex other)
{
if (this == other) return 0;
if (Math.Abs(this.Real - other.Real) < 0.0000001)
{
if (this.Imaginary - other.Imaginary > 0) return 1;
else return -1;
}
if (this.Real > other.Real) return 1;
return -1;
}
// Задания по перегрузки операторов умножения и деления также нет, но вот они на всякий пожарный.
public static Complex operator *(Complex a, Complex b)
{
return new Complex()
{
Real = a.Real * b.Real - a.Imaginary * b.Imaginary,
Imaginary = a.Real * b.Imaginary + a.Imaginary * b.Real
};
}
public static Complex operator *(Complex a, double d)
{
return new Complex()
{
Real = a.Real * d,
Imaginary = a.Imaginary * d
};
}
public static Complex operator /(Complex a, Complex b)
{
return new Complex()
{
Real = (a.Real * b.Real + a.Imaginary * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary),
Imaginary = (a.Imaginary * b.Real - a.Real * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary)
};
}
public static Complex operator /(Complex a, double d)
{
return new Complex()
{
Real = a.Real / d,
Imaginary = a.Imaginary / d
};
}
}
public static class SquareRootEquation
{
public static void Solve(double a, double b, double c)
{
double D = b*b - 4*a*c;
if (Math.Abs(D)<0.00001)
{
double x = -b/(2*a);
Console.WriteLine("Уравнение имеет один корень х = {0}",x);
}
else if (D>0)
{
double x1 = (-b + Math.Sqrt(D)) / (2 * a);
double x2 = (-b - Math.Sqrt(D)) / (2 * a);
Console.WriteLine("Уравнение имеет два вещественных корня х1 = {0}; х2 = {1};",x1,x2);
}
else
{
Complex D1 = new Complex(0, Math.Sqrt(Math.Abs(D)));
Complex D2 = new Complex(0, -1*Math.Sqrt(Math.Abs(D)));
Complex x1 = (D1 - b) / (2 * a);
Complex x2 = (D2 - b) / (2 * a);
Console.WriteLine("Вещественных (действительных) корней нет, однако существуют два комплексных корня х1 = {0}; х2 = {1};", x1, x2);
}
}
}
class Program
{
static void Main(string[] args)
{
SquareRootEquation.Solve(2,-1,2);
Console.ReadLine();
}
}
}