-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtankfreq.cpp
More file actions
91 lines (77 loc) · 1.57 KB
/
Copy pathtankfreq.cpp
File metadata and controls
91 lines (77 loc) · 1.57 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
/* compile with $ g++ -Wall -Os -o tankfreq tankfreq.cpp
*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <exception>
#include <locale> // std::locale, std::numpunct, std::use_facet
using namespace std;
// custom numpunct with grouping:
struct my_numpunct : numpunct<char> {
string
do_grouping() const {
return "\03";
}
};
class TankFreq {
private:
double L,
C;
public:
void
args(double H, double F)
{
L = H;
C = F;
}
double
calc()
{
return
1 /
(
M_PI * 2 * sqrt(
L * C * 1e-12
)
);
}
void
usage()
{
cerr
<< "tankfreq is an LC tank resonance frequency calculator." << endl
<< "output is frequency in Hz" << endl
<< endl
<< "Usage: tankfreq microfarads microhenries" << endl
<< "Example: tankfreq 3.3 0.86207" << endl
<< "Output should be: 94,360.861" << endl;
}
};
int
main(int argc, char * argv[])
{
TankFreq tf;
if (argc == 3)
{
// set up digit grouping
locale loc(cout.getloc(), new my_numpunct);
cout.imbue(loc);
// initialize vars
tf.args(0, 0);
try
{
tf.args(stod(argv[1]), stod(argv[2]));
} catch (invalid_argument& e)
{
cerr << "error: arguments must be numeric." << endl;
return(EXIT_FAILURE);
}
// 3 decimal places
cout << fixed << setprecision(3) << tf.calc() << endl;
}else
{
tf.usage();
return(EXIT_FAILURE);
}
return(EXIT_SUCCESS);
}