Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Masodfoku
- {
- internal class Equation
- {
- private double a, b, c;
- public Equation(double[] values)
- {
- this.a = values[0]; this.b = values[1]; this.c = values[2];
- }
- public Equation(double a, double b, double c)
- {
- this.a = a; this.b = b; this.c = c;
- }
- public double FirstDegreeSolution { get; set; }
- public double[] SecondDegreeSolution { get; set; }
- public string ComplexSolution { get; set; }
- //Eldönti, hogy másodfokú-e az egyenlet. Ha igen, true
- //értékkel tér vissza.
- public bool SecondDegree()
- {
- return this.a != 0 && Determinant() > 0;
- }
- //Eldönti, hogy az egyenlet megoldása csak komplex szám
- //lehet-e. Ehhez az kell, hogy a diszkrimináns negatív legyen.
- public bool Complex()
- {
- return Determinant() < 0;
- }
- public void Solve()
- {
- if (Complex())
- {
- SolveComplex();
- }
- else if (SecondDegree())
- {
- SolveQuadratic();
- }
- else if (this.a == 0 || Determinant() == 0)
- {
- SolveSimple();
- }
- else
- {
- throw new ArgumentException();
- }
- }
- private void SolveQuadratic()
- {
- double partOne = -this.b;
- double partTwo = Math.Sqrt(Determinant());
- double partThree = 2 * this.a;
- this.SecondDegreeSolution = new double[] { (partOne + partTwo) / partThree, (partOne - partTwo) / partThree };
- }
- private void SolveComplex()
- {
- double partOne = -this.b / (2 * this.a);
- double partTwoHelper = Determinant();
- double rootPartTwo = Math.Sqrt(-partTwoHelper);
- double partTwo = rootPartTwo / (2 * this.a);
- this.ComplexSolution = String.Format("{0}+-{1}i", partOne, Math.Round(partTwo, 3));
- }
- private void SolveSimple()
- {
- if (a == 0)
- this.FirstDegreeSolution = -this.c / this.b;
- else if (Determinant() == 0)
- this.FirstDegreeSolution = -this.b / (2 * this.a);
- else
- throw new ArgumentException();
- }
- public override string ToString()
- {
- if (this.Complex())
- return this.ComplexSolution;
- else if (this.SecondDegree())
- return String.Join(", ", this.SecondDegreeSolution);
- else
- return this.FirstDegreeSolution.ToString();
- }
- private double Determinant()
- {
- return Math.Pow(this.b, 2) - 4 * this.a * this.c;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement