Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Faqs and RFCs
FileSearching
Google
Google Groups
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
resistor.c
/* * resistor.c * * Convert a resistor color code into the resistor * value. This program takes a three-color band list * and prints the value in Ohms. It does not do anything * with the tolerance band. * * Example: red violet orange -> 27000-Ohms */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <locale.h> #include <ctype.h> #define VERSION "2018.01a" #define VERSION_DATE "January 29, 2018" /* lookup table */ struct resistor_st { char *color; int digit; double mult; } resistor[] = { "black", 0, 1.0, "brown", 1, 10.0, "red", 2, 100.0, "orange", 3, 1000.0, "yellow", 4, 10000.0, "green", 5, 100000.0, "blue", 6, 1000000.0, "violet", 7, 10000000.0, "grey", 8, 0.01, "white", 9, 0.1, "gray", 8, 0.01, /* for those of us that spell grey gray */ NULL, -1, -1.0 }; /* function prototypes */ int color_to_digit(char *); double color_to_mult(char *); void error(char *); int resist_out(double, FILE *); int color_in(int, char **); int resist_in(int, char **); double find_mult(double); int find_band1(double); int find_band2(double); int mult_to_digit(double); void version(void); void help(void); int main(int argc, char **argv) { int result; setlocale(LC_ALL, ""); if (argc==1) { result = color_in(argc, argv); } if (argc==2) { if (isdigit(argv[1][0])) { result = resist_in(argc, argv); } else { switch (tolower(argv[1][1])) { case 'h': help(); return EXIT_SUCCESS; break; case 'v': version(); return EXIT_SUCCESS; break; default: result = -1; break; } } } return result; } void version() { printf("RESISTOR version %s\n", VERSION); printf("\nMichael J. Chappell %s\n\n", VERSION_DATE); return; } void help() { printf("RESISTOR\n"); printf("Usage: resistor [n[K|M]]\n"); printf("\n"); version(); return; } int resist_in(int argc, char **argv) { double base, ohms; int digit1, digit2; double mult; char band1[255], band2[255], band3[255]; char *p; base = strtod(argv[1], &p); if ((*p == 'M')||(*p == 'm')) ohms = base * 1000000.0; else if ((*p == 'K')||(*p == 'k')) ohms = base * 1000.0; else ohms = base; if (ohms <= 0.0) return EXIT_FAILURE; mult = find_mult(ohms); if (mult<0.0) return EXIT_FAILURE; digit1 = find_band1(ohms); if (digit1<0) return EXIT_FAILURE; digit2 = find_band2(ohms); if (digit2<0) return EXIT_FAILURE; /* printf("argv[1] = \"%s\".\n", argv[1]); printf("Ohms = %'.2lf\n", ohms); printf("(band1,band2,mult) = (%d,%d,%'.2lf)\n", digit1, digit2, mult); */ printf("The first three bands of an %s Ohm resistor are %s, %s, and %s.\n", argv[1], resistor[digit1].color, resistor[digit2].color, resistor[mult_to_digit(mult)].color); return EXIT_SUCCESS; } double find_mult(double ohms) { if (ohms< 1.0) return 0.01; if (ohms< 10.0) return 0.10; if (ohms< 100.0) return 1.00; if (ohms< 1000.0) return 10.00; if (ohms< 10000.0) return 100.00; if (ohms< 100000.0) return 1000.00; if (ohms< 1000000.0) return 10000.00; if (ohms< 10000000.0) return 100000.00; if (ohms< 100000000.0) return 1000000.00; if (ohms<1000000000.0) return 10000000.00; return -1.0; } int find_band1(double ohms) { double x; double mult; mult = find_mult(ohms); x = ohms/mult; if (x>=90.0) return 9; if (x>=80.0) return 8; if (x>=70.0) return 7; if (x>=60.0) return 6; if (x>=50.0) return 5; if (x>=40.0) return 4; if (x>=30.0) return 3; if (x>=20.0) return 2; if (x>=10.0) return 1; if (x>=00.0) return 0; return -1; } int find_band2(double ohms) { double x; double mult; int band1; mult = find_mult(ohms); band1 = find_band1(ohms); x = ohms/mult -10*band1; if (x>=9.0) return 9; if (x>=8.0) return 8; if (x>=7.0) return 7; if (x>=6.0) return 6; if (x>=5.0) return 5; if (x>=4.0) return 4; if (x>=3.0) return 3; if (x>=2.0) return 2; if (x>=1.0) return 1; if (x>=0.0) return 0; return -1; } int mult_to_digit(double n) { int i; for (i=0; i<10; i++) { if (n == resistor[i].mult) return i; } return -1; } int color_in(int argc, char **argv) { double ohms; int digit1, digit2; double mult; char band1[255], band2[255], band3[255]; /* get the color code */ puts("Type colors (Ex: brown black red) + Return:"); scanf("%s %s %s", band1, band2, band3); /* convert the code to a value */ if ((digit1 = color_to_digit(band1)) == -1) error(band1); if ((digit2 = color_to_digit(band2)) == -1) error(band2); if ((mult = color_to_mult(band3)) == -1.0) error(band1); ohms = (digit1*10 + digit2)*mult; printf( "The value of a resistor with bands of %s, %s and %s\nis ", band1, band2, band3); resist_out(ohms, stdout); printf(" Ohms.\n"); return EXIT_SUCCESS; } int color_to_digit(char *color) { struct resistor_st *p; /* look for the code in the resistor table */ p = resistor; while (p->color != NULL) { if ((strcmp(p->color, color))==0) break; ++p; } return p->digit; } double color_to_mult(char *color) { struct resistor_st *p; /* look for the code in the resistor table */ p = resistor; while (p->color != NULL) { if ((strcmp(p->color, color))==0) break; ++p; } return p->mult; } void error(char *text) { fprintf(stderr, "bad color spec: %s\n", text); exit(EXIT_FAILURE); } int resist_out(double value, FILE *fp) { if (value>1000000.0) { fprintf(fp, "%'.2lfM", value/1000000.0); return 0; } if (value>1000.0) { fprintf(fp, "%'.2lfM", value/1000.0); return 0; } fprintf(fp, "%'.2lf", value); return 0; }
This source has been viewed 18 times.
Friday, 6 December 2024
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org