Skip to content

Commit 2ae75d6

Browse files
MyLinden-devMyLinden-dev
authored andcommitted
Добавьте файлы проекта.
1 parent 40ec5d6 commit 2ae75d6

24 files changed

+641
-0
lines changed

DiscriminantAndVieta.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32922.545
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscriminantAndVieta", "DiscriminantAndVieta\DiscriminantAndVieta.csproj", "{4A3F408E-7AB4-4939-8E80-D9328DDB6A3C}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject1", "TestProject1\TestProject1.csproj", "{FDBBD416-F7A0-4ABE-ACD8-F756D72655A6}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{4A3F408E-7AB4-4939-8E80-D9328DDB6A3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{4A3F408E-7AB4-4939-8E80-D9328DDB6A3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{4A3F408E-7AB4-4939-8E80-D9328DDB6A3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{4A3F408E-7AB4-4939-8E80-D9328DDB6A3C}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{FDBBD416-F7A0-4ABE-ACD8-F756D72655A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{FDBBD416-F7A0-4ABE-ACD8-F756D72655A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{FDBBD416-F7A0-4ABE-ACD8-F756D72655A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{FDBBD416-F7A0-4ABE-ACD8-F756D72655A6}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {94E5FB90-EE64-4C03-8A29-833076578E43}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace DiscriminantAndVieta.CalculateEquation
2+
{
3+
/// <summary>
4+
/// Абстрактный класс для вычисления коней квадратного уравнения (Quadratic Equation)
5+
/// </summary>
6+
/// Используется абстрактный класс, а не интерфейс,
7+
/// потому что наследники могут использовать конструктор и свойства без изменений
8+
public abstract class ACalcQe
9+
{
10+
private protected ACalcQe(double[] variables)
11+
{
12+
IsSuccessfullyChecked = null;
13+
Variables = variables;
14+
}
15+
16+
public virtual double[] Variables { get; private protected set; }
17+
public virtual double[] Roots { get; private protected set; }
18+
public virtual bool? IsSuccessfullyChecked { get; private protected set; }
19+
public abstract void Calc();
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DiscriminantAndVieta.TypeEquation;
2+
3+
namespace DiscriminantAndVieta.CalculateEquation
4+
{
5+
/// <summary>
6+
/// Расчет неполных квадратичных уравнений (Incomplete Quadratic Equation)
7+
/// </summary>
8+
public class CalcIqe : ACalcQe
9+
{
10+
private ITypeIqe type;
11+
private double[] variables;
12+
13+
public CalcIqe(ITypeIqe type, double[] variables) : base(variables)
14+
{
15+
this.type = type;
16+
this.variables = variables;
17+
}
18+
19+
public override void Calc()
20+
{
21+
Roots = type.Calc(variables);
22+
}
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using DiscriminantAndVieta.TypeEquation;
2+
3+
namespace DiscriminantAndVieta.CalculateEquation
4+
{
5+
/// <summary>
6+
/// Расчет квадратичного уравнения с помощью дискриминанта
7+
/// </summary>
8+
public class CalcQeByD : ACalcQe
9+
{
10+
private ITypeQeCalcByD type;
11+
private ACalcQe calc;
12+
13+
public CalcQeByD(ITypeQeCalcByD type, double[] variables) : base(variables)
14+
{
15+
this.type = type;
16+
calc = new CalcRootsByD(type, variables);
17+
18+
IsSuccessfullyChecked = null;
19+
}
20+
21+
public override double[] Roots { get => calc.Roots; }
22+
public override double[] Variables { get => calc.Variables; }
23+
24+
public override void Calc()
25+
{
26+
calc.Calc();
27+
28+
if (type is TypeAIs1 && calc.Roots != null && calc.Roots.Length == 2)
29+
{
30+
ACalcQe checkWithVieta = new CheckRootsByVieta(calc.Variables, calc.Roots);
31+
checkWithVieta.Calc();
32+
33+
IsSuccessfullyChecked = checkWithVieta.Roots != null;
34+
}
35+
else
36+
IsSuccessfullyChecked = null;
37+
38+
}
39+
}
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using DiscriminantAndVieta.CalculateRoots;
2+
using DiscriminantAndVieta.TypeEquation;
3+
4+
namespace DiscriminantAndVieta.CalculateEquation
5+
{
6+
/// <summary>
7+
/// Расчет корней квадратичного уравнения в зависимости от его типа с помощью дискриминта
8+
/// </summary>
9+
public class CalcRootsByD : ACalcQe
10+
{
11+
private protected ITypeQeCalcByD type;
12+
13+
public CalcRootsByD(ITypeQeCalcByD type, double[] variables) : base(variables)
14+
{
15+
this.type = type;
16+
}
17+
18+
public override void Calc()
19+
{
20+
double d = type.CalcDiscriminant(Variables);
21+
22+
if (d < 0)
23+
{
24+
Roots = null;
25+
return;
26+
}
27+
28+
ACalculateRoots discriminant;
29+
if (d == 0)
30+
discriminant = new CalculateOneRoot(Variables);
31+
else
32+
discriminant = new CalculateTwoRoots(Variables, d);
33+
Roots = discriminant.CalcRoots();
34+
35+
if (type is TypeBqe)
36+
{
37+
List<double> doubles = new();
38+
foreach (var item in Roots)
39+
{
40+
if (item == 0)
41+
doubles.Add(0);
42+
else if (item > 0)
43+
{
44+
double res = Math.Sqrt(item);
45+
doubles.Add(res);
46+
doubles.Add(-res);
47+
}
48+
}
49+
Roots = doubles.ToArray();
50+
}
51+
}
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace DiscriminantAndVieta.CalculateEquation
2+
{
3+
/// <summary>
4+
/// Проверка правильности с помощью теоремы Виета вычисленных корней квадратичного уравнения
5+
/// </summary>
6+
public class CheckRootsByVieta : ACalcQe
7+
{
8+
public CheckRootsByVieta(double[] variables, double[] roots) : base(variables)
9+
{
10+
Roots = roots;
11+
}
12+
13+
private bool Sum()
14+
{
15+
return Roots[0] + Roots[1] == -1 * Variables[1];
16+
}
17+
18+
private bool Mult()
19+
{
20+
return Roots[0] * Roots[1] == Variables[2];
21+
}
22+
23+
public override void Calc()
24+
{
25+
if (Roots == null || Roots.Length != 2 || !(Sum() && Mult()))
26+
{
27+
Roots = null;
28+
}
29+
}
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace DiscriminantAndVieta.CalculateRoots
2+
{
3+
/// <summary>
4+
/// Абстрактный класс для вычисления корней с помощью дискриминанта
5+
/// </summary>
6+
/// Используется абстрактный класс, а не интерфейс,
7+
/// потому что наследники могут использовать конструктор и поле без изменений
8+
internal abstract class ACalculateRoots
9+
{
10+
private protected double[] variables;
11+
12+
protected ACalculateRoots(double[] variables)
13+
{
14+
this.variables = variables;
15+
}
16+
17+
public abstract double[] CalcRoots();
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace DiscriminantAndVieta.CalculateRoots
2+
{
3+
/// <summary>
4+
/// Вычисление одного корня
5+
/// </summary>
6+
internal class CalculateOneRoot : ACalculateRoots
7+
{
8+
public CalculateOneRoot(double[] variables) : base(variables)
9+
{
10+
this.variables = variables;
11+
}
12+
13+
public override double[] CalcRoots()
14+
{
15+
return new double[] { -1 * variables[1] / (2 * variables[0]) };
16+
}
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace DiscriminantAndVieta.CalculateRoots
2+
{
3+
/// <summary>
4+
/// Вычисление двух корней
5+
/// </summary>
6+
internal class CalculateTwoRoots : ACalculateRoots
7+
{
8+
private readonly double d;
9+
10+
public CalculateTwoRoots(double[] variables, double d) : base(variables)
11+
{
12+
this.d = d;
13+
}
14+
15+
private double CalcRoot(bool isSecondRoot)
16+
{
17+
return ((-1 * variables[1]) + ((isSecondRoot ? -1 : 1) * Math.Sqrt(d))) / (2 * variables[0]);
18+
}
19+
20+
public override double[] CalcRoots()
21+
{
22+
return new double[] { CalcRoot(false), CalcRoot(true) };
23+
}
24+
}
25+
}

DiscriminantAndVieta/DefineType.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using DiscriminantAndVieta.CalculateEquation;
2+
using DiscriminantAndVieta.TypeEquation;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace DiscriminantAndVieta
10+
{
11+
/// <summary>
12+
/// Определение типа квадратичного уравнения. Паттерн Синглтон (Одиночка)
13+
/// </summary>
14+
public class DefineType
15+
{
16+
private static DefineType instance;
17+
18+
private DefineType()
19+
{
20+
21+
}
22+
23+
public static DefineType Instance
24+
{
25+
get
26+
{
27+
if (instance == null)
28+
instance = new DefineType();
29+
return instance;
30+
}
31+
}
32+
33+
public ACalcQe? SetType(double[] variables, bool isBiqudratic = false)
34+
{
35+
// Уравнения нет. Данные введены неверно
36+
if (variables[0] == 0)
37+
{
38+
return null;
39+
}
40+
// Биквадратное уравнение
41+
else if (isBiqudratic)
42+
{
43+
if (variables[0] == 0 || variables[1] == 0 || variables[2] == 0)
44+
{
45+
return null;
46+
}
47+
else
48+
{
49+
return new CalcQeByD(new TypeBqe(), variables);
50+
}
51+
}
52+
// Неполное квадратичное уравнение
53+
else if (variables[1] == 0)
54+
{
55+
if (variables[2] != 0)
56+
{
57+
return new CalcIqe(new TypeIncompleteQeBIsZero(), variables);
58+
}
59+
else if (variables[2] == 0)
60+
{
61+
return new CalcIqe(new TypeIncompleteQeBAndCIsZero(), variables);
62+
}
63+
}
64+
else if (variables[1] != 0 && variables[2] == 0)
65+
{
66+
return new CalcIqe(new TypeIncompleteQeCIsZero(), variables);
67+
}
68+
// Полное квадратное уравнение при а == 1
69+
else if (variables[0] == 1)
70+
{
71+
return new CalcQeByD(new TypeAIs1(), variables);
72+
}
73+
// Полное квадратное уравнение с четным коэффициентом
74+
else if (variables[1] % 2 == 0)
75+
{
76+
return new CalcQeByD(new TypeBIsOdd(), variables);
77+
}
78+
// Полное квадратное уравнение
79+
return new CalcQeByD(new TypeStandart(), variables);
80+
}
81+
82+
}
83+
}

0 commit comments

Comments
 (0)