-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
120 lines (92 loc) · 4.24 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using AngouriMath;
using ResistorSearch;
using Spectre.Console;
using System.Globalization;
using System.Text.RegularExpressions;
using static AngouriMath.Entity.Set;
const int minOhm = 1000;
const int maxOhm = 1000000;
const int maxResults = 200;
while (true) {
try {
AnsiConsole.MarkupLine("* Use [yellow]r1[/], [yellow]r2[/] and [yellow]v[/] in equality");
AnsiConsole.MarkupLine("* Equality must have a [yellow]=[/] sign");
AnsiConsole.MarkupLine("* The left and right parts of equality can be swapped, it does not matter");
AnsiConsole.MarkupLine("* The case does not matter");
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("* Examples of formulas");
AnsiConsole.MarkupLine("* For MT3608: [green]v = 0.6 * (1 + (r1 / r2))[/]");
AnsiConsole.MarkupLine("* For TPS61088: [green]r1 = ((v - 1.204) * r2) / 1.204[/]");
AnsiConsole.WriteLine();
var formula = AnsiConsole.Prompt(
new TextPrompt<string>("Formula: ")
.PromptStyle("green")
.Validate(formula => {
formula = formula.ToLower().Replace(" ", "");
string[] parts = formula.Split('=');
// Check formula structure and balance
if (parts.Length != 2) {
return ValidationResult.Error("[red]Formula must contain one '=' sign[/]");
}
// Extract and validate variable usage
var leftVars = new HashSet<string> { "r1", "r2", "v" }.Where(v => parts[0].Contains(v)).ToHashSet();
var rightVars = new HashSet<string> { "r1", "r2", "v" }.Where(v => parts[1].Contains(v)).ToHashSet();
if (!(leftVars.Count == 1 && rightVars.Count == 2) && !(leftVars.Count == 2 && rightVars.Count == 1)) {
return ValidationResult.Error("[red]One side must contain one variable and the other side two variables[/]");
}
try {
(var main, var equality) = Solver.NormalizeFormula(formula);
Entity fullFormula = $"{equality} = {main}";
fullFormula.Solve("v");
} catch (Exception ex) {
return ValidationResult.Error($"[red]{ex.Message}[/]");
}
return ValidationResult.Success();
})
);
var voltage = AnsiConsole.Prompt(
new TextPrompt<double>("Out voltage: ")
.PromptStyle("green")
.Validate(voltage => {
return double.TryParse(voltage.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out _)
? ValidationResult.Success()
: ValidationResult.Error("[red]That is not a valid number[/]");
})
);
var eSeries = AnsiConsole.Prompt(
new TextPrompt<int>("E-series (3, 6, 12, 24, 48, 96, 192): ")
.PromptStyle("green")
.Validate(eSeries => {
var validNumbers = new HashSet<int> { 3, 6, 12, 24, 48, 96, 192 };
return validNumbers.Contains(eSeries)
? ValidationResult.Success()
: ValidationResult.Error("[red]Number must be one of the specified values[/]");
})
);
var resistorValues = eSeries switch {
3 => ResistorValues.E3,
6 => ResistorValues.E6,
12 => ResistorValues.E12,
24 => ResistorValues.E24,
48 => ResistorValues.E48,
96 => ResistorValues.E96,
192 => ResistorValues.E192,
_ => ResistorValues.E24
};
var solver = new Solver() {
resistorValues = ResistorValues.GetRange(resistorValues, minOhm, maxOhm)
};
AnsiConsole.Clear();
AnsiConsole.MarkupLine("[yellow]Search...[/]");
var result = solver.SolveAll(formula, voltage);
AnsiConsole.Clear();
Utility.WriteInfo(result, maxResults);
} catch (Exception ex) {
AnsiConsole.Clear();
AnsiConsole.MarkupLine($"[red]{ex.Message}[/]");
}
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[green]Press any key to restart[/]");
Console.ReadKey();
AnsiConsole.Clear();
}