-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipz.c
85 lines (76 loc) · 2.02 KB
/
zipz.c
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
/* Compile with gcc -Wall -o zipz zipz.c -lm
The Lightning Stalker 2024 */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
void
usage(void) {
puts ("zipz calculates the impedance of open wire line and zip cord");
puts ("and reverse calculation of conductor spacing");
puts ("output is impedance in Ohms\n");
puts ("Usage: zipz distance radius k");
puts (" or: zipz -r impedance radius k");
puts ("Distance is distance between centers of conductors");
puts ("Radius is radius of one conductor");
puts ("k is relative permittivity in region between conductors\n");
puts ("Example 1: zipz 1.2 3.25 0.05");
puts ("Output should be: 456.8(Ω)\n");
puts ("Example 2: zipz -r 456.8 0.05 1.2");
puts ("Output should be: 3.2510 (distance d)\n");
exit (EXIT_FAILURE);
}
void
calculate(float k, float d, float r) {
printf ("%'.1f\n",
276.0 /
sqrtf(k) *
log10f(d / r)
);
return;
}
void
reverseCalc(float impedance, float r, float k) {
printf("%'.4f\n",
r *
powf(10,
impedance /
(
276 /
sqrtf(k)
)
)
);
return;
}
int main (int argc, char **argv)
{
int opt;
unsetenv ("LC_ALL");
setlocale (LC_NUMERIC, ""); // This should give us digit grouping
while ((opt = getopt(argc, argv, "r:")) != -1) {
switch (opt) {
case 'r':
if (argc == 5)
{
reverseCalc(atof(argv[2]),
atof(argv[3]),
atof(argv[4]));
exit(EXIT_SUCCESS);
}else {
usage();
}
break;
default: /* '?' */
usage();
}
}
if (argc == 4) // forward calculation
{
calculate(atof(argv[1]), atof(argv[2]), atof(argv[3]));
}else {
usage();
}
exit(EXIT_SUCCESS);
}