diff --git a/README.md b/README.md index ca21116..c731522 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,14 @@ take more off but you can't put more back on." If you don't think that's funny at least consider it my gift to you as the only person who will ever read this. ## cap -cap.c is the capacitor complement to iloc. It takes your desired capacitance -value in picofarads and the thickness (or plate spacing in the case of air -dielectric) and dielectric K constant of the dielectric material. Though the -algorithm is extremely simple, even moreso than the other calculators in this -collection, it does speed up the process and make for less time entering -formulas into a calculator (and less chance for mistakes). +cap.c is the capacitor complement to iloc. It calculates dimensions for +capacitors of several different shapes. Simply provide the K permittivity +constant for the dielectric material and various dimensions as they are called +for. Though the algorithm is extremely simple, even moreso than the other +calculators in this collection, it does speed up the process and make for less +time entering formulas into a calculator (and less chance for mistakes). + +### Command line options are: +* -p = flat plate capacitor +* -c = cylindrical capacitor +* -? = list of command line options diff --git a/cap.c b/cap.c index 6c490b4..cecf450 100644 --- a/cap.c +++ b/cap.c @@ -6,10 +6,19 @@ #include #include #include +#include #define e 0.08854187812813 // ɛ - electric constant with cm/pF conversion -int main() +double roundval(long double inval) +// round to 2 decimal places +{ + int i; + i = (int)(inval * 100 + .5); + return (double)i / 100; +} + +void plate() { // variable declarations long double C, d, A, K, l, i; @@ -22,28 +31,104 @@ int main() * 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("\nThe capacitor area shall be %.2Lf cm² as in a square\nhaving %.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 usage() +{ + puts("\ + Valid options are:\n \ + \n \ + -p = square flat plate type (default)\n \ + -c = cylinder type.\n \ + -? = this usage text\ + \n"); +} + +int main(int argc, char *argv[]) +{ + // variable declarations + char c; + // boiler plate puts("\n \ - cap is a capacitor calculator.\n \ - cap will find area and side length of square plates for a capacitor given\n \ - the target capacitance, thickness and K constant of the dielectric\n \ - material.\n \ - \n"); - - // input section - 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; - l = sqrt(A); - // end formulas - - // output section - printf("\nThe capacitor area shall be %.1Lf cm² as in a square having %.1Lf cm length sides.\n\n", A, l); + 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"); + + // get options passed at the command line + c = getopt(argc, argv, "pc?"); + 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 '?': + usage(1); + break; + default: + printf ("?? invalid option 0%o ??\n", c); + break; + } + exit(0); } diff --git a/iloc.c b/iloc.c index b194b27..0e9bbbe 100644 --- a/iloc.c +++ b/iloc.c @@ -9,6 +9,14 @@ #define u 1.2566370621219E-6 // µ - magnetic constant +double roundval(long double inval) +// round to 1 decimal place +{ + int i; + i = (int)(inval * 10 + .5); + return (double)i / 10; +} + int main() { // variable declarations @@ -48,9 +56,10 @@ int main() A = M_PI * powl(D / 2, 2); // M_PI = Pi i = L * (1 / u) / A; N = i * TS; - l = N * TS * 1000; + l = roundval(N * TS * 1000); + i = roundval(N); // end formulas // output section - printf("\nThe coil shall be %.1Lf mm in length and have %.1Lf turns.\n\n", l, N); + printf("\nThe coil shall be %.1Lf mm in length and have %.1Lf turns.\n\n", l, i); }