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 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;
- }
- //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 Math.Pow(this.b, 2) - 4 * this.a * this.c < 0;
- }
- private void EquationHelper()
- {
- if (Complex())
- {
- SolveComplex();
- }
- else if (SecondDegree())
- {
- SolveQuadratic();
- }
- else if(this.a==0)
- {
- SolveSimple();
- }
- throw new ArgumentException();
- }
- private void SolveQuadratic()
- {
- double partOne = -this.b;
- double partTwo = Math.Sqrt(Math.Pow(this.b, 2) - 4 * this.a * this.c);
- 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 = Math.Pow(this.b, 2) - 4 * this.a * this.c;
- double rootPartTwo = Math.Sqrt(-partTwoHelper);
- double partTwo = rootPartTwo / (2 * this.a);
- this.ComplexSolution= String.Format("{0}+/-{1}i", partOne, partTwo);
- }
- private void SolveSimple()
- {
- this.FirstDegreeSolution = -this.b / (2 * this.a);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement