-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap.c
202 lines (174 loc) · 4.23 KB
/
cap.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Simple capacitor plate area calculator
* compile with cc -Wall -o cap cap.c -lm
* 2021 The Lightning Stalker
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <ctype.h>
const long double e = 0.08854187812813; // ɛ - electric constant with cm/pF conversion
double roundval(long double inval)
// round to 2 decimal places
{
int i;
i = (int)(inval * 100 + 0.5);
return (double)i / 100;
}
void plate()
{
// variable declarations
long double C, d, A, K, l, i;
/* C = desired capacitance
* d = depth of dielectric material (thickness)
* A = plate area
* K = dielectric constant
* l = length of sides of a square plate
* i = intermediate temp variable
*/
// input section
puts("Plate type");
printf("Enter target capacitance (pF): ");
scanf("%Lf", &C);
printf("Enter spacing or depth of dielectric material (cm): ");
scanf("%Lf", &d);
printf("Enter dielectric K constant of dielectric material: ");
scanf("%Lf", &K);
// begin formulas
i = e * K;
A = C * d / i;
i = sqrt(A);
l = roundval(i);
i = roundval(A);
// end formulas
// output section
printf("\n \
The capacitor area shall be %.2Lf cm² such as a square\n \
having %.2Lf cm length sides.\n\n", i, l);
}
void cylinder()
{
// variable declarations
long double C, R1, R2, K, l, i;
/* C = desired capacitance
* R1 = inner diametr of dielectric
* R2 = outer diameter of dielectric
* K = dielectric constant
* l = length of capacitor
* i = intermediate temp variable
*/
// input section
puts("cylinder type");
printf("Enter target capacitance (pF): ");
scanf("%Lf", &C);
printf("Enter inner diameter of dielectric (cm): ");
scanf("%Lf", &R1);
printf("Enter outer diameter of dielectric (cm): ");
scanf("%Lf", &R2);
printf("Enter dielectric K constant of dielectric material: ");
scanf("%Lf", &K);
// begin formulas
i = logl(R2 / R1);
l = (C * i) / (2 * M_PI * e * K);
i = roundval(l);
// end formulas
// output section
printf("\nThe capacitor shall be %.2Lf cm in length.\n\n", i);
}
void sphere()
{
// variable declarations
long double C, R;
/* C = desired capacitance
* R = radius of sphere
*/
puts("sphere type");
printf("Enter target capacitance (pF): ");
scanf("%Lf", &C);
// begin formulas
R = C / (4 * M_PI * e) * 2;
// end formulas
// output section
printf("\nThe sphere shall have a diameter of %.2Lf cm.\n\n", R);
}
void disc()
{
// variable declarations
long double C, R;
/* C = desired capacitance
* R = radius of disc
*/
puts("disc type");
printf("Enter target capacitance (pF): ");
scanf("%Lf", &C);
// begin formulas
R = C / (8 * e) * 2;
// end formulas
// output section
printf("\nThe disc shall have a diameter of %.2Lf cm.\n\n", R);
}
void usage(int status)
{
// boiler plate
puts("\n \
cap is a capacitor calculator.\n \
cap will find dimensions of the conductors for a capacitor given the target\n \
capacitance, thickness and K constant of the dielectric material.\n \
Enter 'cap -?' for a list of command line options.\n \
\n");
puts(" \
Valid options are:\n \
\n \
(Specify one)\n \
-p = parallel plate type\n \
-c = cylinder type\n \
-s = sphere type\n \
-d = disc type\n \
-? = this usage text\n \
");
// handle exit status
if (status)
{
exit(0);
}
else
{
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
// variable declarations
char c;
// get options passed at the command line
c = getopt(argc, argv, "pcsd?");
if (getopt(argc, argv, "pcsd?") != -1) usage(1); // multiple opts
switch(c)
{
/* case -1: // Detect the end of the options.
plate();
break; */
case 'p':
plate(); // plate type
break;
case 'c':
cylinder(); //cylinder type
break;
case 's':
sphere(); // sphere type
break;
case 'd':
disc(); // disc type
break;
case '?':
usage(0);
break;
default:
if (!isalnum(c)) c = '?';
printf ("?? invalid option %c ??\n", c);
usage(1);
exit(EXIT_FAILURE);
break;
}
exit(0);
}