-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (97 loc) · 3.71 KB
/
main.cpp
File metadata and controls
118 lines (97 loc) · 3.71 KB
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
#include <iostream>
#include <string>
#include <array>
#include <limits> // for infinity()
#include <cmath> // for abs()
#include <iomanip> // for setting presicion of output
constexpr std::array<double, 12> E12 = {
1.0, 1.2, 1.5, 1.8, 2.2, 2.7,
3.3, 3.9, 4.7, 5.6, 6.8, 8.2
};
constexpr std::array<double, 36> E24 = {
1.0, 1.2, 1.5, 1.8, 2.2, 2.7, // series E12
3.3, 3.9, 4.7, 5.6, 6.8, 8.2,
1.0, 1.1, 1.2, 1.3, 1.5, 1.6, // series E24
1.8, 2.0, 2.2, 2.4, 2.7, 3.0, // there are some repeats but IDC
3.3, 3.6, 3.9, 4.3, 4.7, 5.1,
5.6, 6.2, 6.8, 7.5, 8.2, 9.1
};
constexpr std::array<double, 84> E48 = {
1.0, 1.2, 1.5, 1.8, 2.2, 2.7,
3.3, 3.9, 4.7, 5.6, 6.8, 8.2,
1.0, 1.1, 1.2, 1.3, 1.5, 1.6,
1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
3.3, 3.6, 3.9, 4.3, 4.7, 5.1,
5.6, 6.2, 6.8, 7.5, 8.2, 9.1,
1.00, 1.05, 1.10, 1.15, 1.21, 1.27,
1.33, 1.40, 1.47, 1.54, 1.62, 1.69,
1.78, 1.87, 1.96, 2.05, 2.15, 2.26,
2.37, 2.49, 2.61, 2.74, 2.87, 3.01,
3.16, 3.32, 3.48, 3.65, 3.83, 4.02,
4.22, 4.42, 4.64, 4.87, 5.11, 5.36,
5.62, 5.90, 6.19, 6.49, 6.81, 7.15,
7.50, 7.87, 8.25, 8.66, 9.09, 9.53
};
struct Solution {
double R1;
double R2;
};
template<size_t seriesSize>
Solution findBestR1andR2(const std::array<double, seriesSize>& series, double targetRatio) {
double minDeltaRatio = std::numeric_limits<double>::infinity();
double bestR1 = series[0];
double bestR2 = series[0];
for (double val1 : series) {
for (double val2 : series) {
double ratio = val1/val2;
double deltaRatio = std::abs(ratio - targetRatio);
if(deltaRatio < minDeltaRatio) {
bestR1 = val1;
bestR2 = val2;
minDeltaRatio = deltaRatio;
}
}
}
return {bestR1, bestR2};
}
int main() {
double targetRatio = 0;
std::string targetRatioString;
std::cout << "Resistor Value Calculator\nPlease input the target ratio R1/R2:\n";
std::cin >> targetRatioString;
// take user input until they input q or quit or exit or a valid ratio
while(true) {
if(targetRatioString == "q" or targetRatioString == "quit" or targetRatioString == "exit") {
// std::cout << "bye!" << std::endl;
return 0;
}
try{
targetRatio = stod(targetRatioString);
} catch(std::invalid_argument& e) {}
if(targetRatio > 0) {
break;
}
std::cout << "Invalid input. Please input a value > 0:\n";
std::cin >> targetRatioString;
}
//get ratio in range 0.1 to 1.
double nearestPow10 = powf(10.0f, std::ceil(log10f(targetRatio)));
// std::cout << "\t[DEBUG] nearest power of 10 = " << nearestPow10 << '\n';
targetRatio /= nearestPow10;
// std::cout << "\t[DEBUG] new targetRatio = " << targetRatio << '\n';
Solution E12Solution = findBestR1andR2(E12, targetRatio);
Solution E24Solution = findBestR1andR2(E24, targetRatio);
Solution E48Solution = findBestR1andR2(E48, targetRatio);
if(nearestPow10 > 1) {
E12Solution.R1 *= nearestPow10;
E24Solution.R1 *= nearestPow10;
E48Solution.R1 *= nearestPow10;
} else if(nearestPow10 < 1) {
E12Solution.R2 /= nearestPow10;
E24Solution.R2 /= nearestPow10;
E48Solution.R2 /= nearestPow10;
}
std::cout << "[E12] " << E12Solution.R1 << "/" << E12Solution.R2 << " (" << E12Solution.R1/E12Solution.R2 << ")\n";
std::cout << "[E24] " << E24Solution.R1 << "/" << E24Solution.R2 << " (" << E24Solution.R1/E24Solution.R2 << ")\n";
std::cout << "[E48] " << E48Solution.R1 << "/" << E48Solution.R2 << " (" << E48Solution.R1/E48Solution.R2 << ")\n";
}