//========================================================================= #define PROGRAMMER "BAHN, William" #define PROG_CODE "bahw" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "RWA 5.4" #define REVISION (0) #define TITLE "Real World Application from Section 5.4" #define SUBTITLE "Computing Resistance" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "rwa0504.c" //========================================================================= // PROBLEM: // // MODIFIED FROM TEXT // // Given single-character codes for the colored bands that mark a resistr, // compute and display the resistance. Print the value in engineering // notation using the proper prefixes for the unit of resistance (ohms). // // Note that the characters in this program are slightly different // than the section in the book. // // Color Character Value Multiplier Tolerance // Black K 0 10^0 - // Brown B 1 10^1 1% // Red R 2 10^2 2% // Orange O 3 10^3 - // Yellow Y 4 10^4 - // Green G 5 10^5 - // Blue U 6 10^6 - // Violet V 7 10^7 - // Gray A 8 10^8 - // White W 9 10^9 - // Gold D - 10^-1 5% // Silver S - 10^-2 10% // None N - -- 20% // // The resistors can have three, four, or five bands. // // Three bands: // R = [10^(1st band) + (2nd band)] * 10^(3rd band) // Tolerance = 20% // Four bands: // R = [10^(1st band) + (2nd band)] * 10^(3rd band) // Tolerance = (fourth band) (must be silver or gold) // Five bands: // R = [100^(1st band) + 10*(2nd band) + (3rd band)] * 10^(4th band) // Tolerance = (fifth band) (must be red or brown) // // Engineering notation generally expresses a value as: // // (mantissa) * 10^(exponent) // // where 1 <= mantissa < 1000 // and |(exponent)| is evenly divisible by 3. // // Furthermore, the multipler portion [ * 10^(exponent) ] is generally // replaced with a prefix as follows: // // 10^-24 yocto (y) // 10^-21 zepto (z) // 10^-18 atto (a) // 10^-15 femto (f) // 10^-12 pico (p) // 10^-9 nano (n) // 10^-6 micro (u) (or the Greek letter mu if feasible) // 10^-3 milli (m) // 10^0 (none) // 10^3 kilo (k) // 10^6 mega (M) // 10^9 giga (G) // 10^12 tera (T) // 10^15 peta (P) // 10^18 exa (E) // 10^21 zetta (Z) // 10^24 yotta (Y) // // The largest value of resistance that can be represented by any of // these codes is: 999x10^9 or 999 gigaohms. // // The smallest value of resistance that can be represented by any of // these codes is: 01x10^-2 or 10 milliohms. // // PSEUDOCODE // // 1) REM: Get the five character-codes from the user. // Store them in five different variables. // Store 'N' for codes not entered if less than five codes. // 2) REM: Determine the resistor value // 3) REM: Scale to engineering notation // 4) REM: Determine the prefix to print // 5) REM: Determine the tolerance // // Without the use of arrays, getting the five codes is a bit tricky // since the user may enter 3, 4, or 5 characters. // // This lends itself to a switch() statement // //== INCLUDE FILES ======================================================== #include // printf(), scanf() #include // pow() //== FUNCTION PROTOTYPES ================================================== void PrintHeader(void); void PrintCodes(void); void GetCodes(char *b1, char *b2, char *b3, char *b4, char *b5); double GetResistance(char b1, char b2, char b3, char b4, char b5); int GetTolerance(char b1, char b2, char b3, char b4, char b5); void PrintColors(char b1, char b2, char b3, char b4, char b5); void PrintValue(double r, int tol); //== MAIN FUNCTION ======================================================== int main(void) { char band1, band2, band3, band4, band5; double r; int tol; PrintHeader(); printf("\n"); // Print a blank line PrintCodes(); GetCodes(&band1, &band2, &band3, &band4, &band5); r = GetResistance(band1, band2, band3, band4, band5); tol = GetTolerance(band1, band2, band3, band4, band5); PrintColors(band1, band2, band3, band4, band5); PrintValue(r, tol); return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { printf("========================================" "=======================================\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printf("========================================" "=======================================\n"); return; } void PrintCodes(void) { printf(" Color Character Value Multiplier Tolerance\n"); printf(" Black K 0 10^0 - \n"); printf(" Brown B 1 10^1 1% \n"); printf(" Red R 2 10^2 2% \n"); printf(" Orange O 3 10^3 - \n"); printf(" Yellow Y 4 10^4 - \n"); printf(" Green G 5 10^5 - \n"); printf(" Blue U 6 10^6 - \n"); printf(" Violet V 7 10^7 - \n"); printf(" Gray A 8 10^8 - \n"); printf(" White W 9 10^9 - \n"); printf(" Gold D - 10^-1 5% \n"); printf(" Silver S - 10^-2 10% \n"); printf(" None N - -- 20% \n"); printf("========================================" "=======================================\n"); return; } void GetCodes(char *b1, char *b2, char *b3, char *b4, char *b5) { char c; int n; n=1; c = 0; printf("Enter the code for each band on the resistor: "); while(n<=5) { scanf("%c", &c); switch(c) { case '\n': switch(n) { case 1: *b1 = 'N'; n++; case 2: *b2 = 'N'; n++; case 3: *b3 = 'N'; n++; case 4: *b4 = 'N'; n++; case 5: *b5 = 'N'; n++; } break; default: switch(n) { case 1: *b1 = c; break; case 2: *b2 = c; break; case 3: *b3 = c; break; case 4: *b4 = c; break; case 5: *b5 = c; break; } n++; } } return; } int GetValue(char color) { switch(color) { case 'K': return(0); case 'B': return(1); case 'R': return(2); case 'O': return(3); case 'Y': return(4); case 'G': return(5); case 'U': return(6); case 'V': return(7); case 'A': return(8); case 'W': return(9); case 'D': return(-1); case 'S': return(-1); case 'N': return(-1); } return(-99); } int GetMultiplier(char color) { switch(color) { case 'D': return(-2); case 'S': return(-1); default : return(GetValue(color)); } } double GetResistance(char b1, char b2, char b3, char b4, char b5) { double r; r = 10.0*GetValue(b1) + GetValue(b2); if('N' == b5) { r *= pow(10.0, GetMultiplier(b3)); } else { r = 10.0 * r + GetValue(b3); r *= pow(10.0, GetMultiplier(b4)); } return(r); } int GetTolerance(char b1, char b2, char b3, char b4, char b5) { // NOTE: Warnings may be issued because b1, b2, and b3 are not // actually used in the function. int tol; switch(b5) { case 'B': tol = 1; break; case 'R': tol = 2; break; case 'N': switch(b4) { case 'D': tol = 5; break; case 'S': tol = 10; break; case 'N': tol = 20; break; } } return(tol); } void PrintColors(char b1, char b2, char b3, char b4, char b5) { int n; char color; printf("Resistor Band Colors:\n"); for(n = 1; n <= 5; n++) { printf(" Band %1i:", n); switch(n) { case 1: color = b1; break; case 2: color = b2; break; case 3: color = b3; break; case 4: color = b4; break; case 5: color = b5; break; } printf(" (%c) ",color); switch(color) { case 'K': printf("Black"); break; case 'B': printf("Brown"); break; case 'R': printf("Red"); break; case 'O': printf("Orange"); break; case 'Y': printf("Yellow"); break; case 'G': printf("Green"); break; case 'U': printf("Blue"); break; case 'V': printf("Violet"); break; case 'A': printf("Gray"); break; case 'W': printf("White"); break; case 'D': printf("Gold"); break; case 'S': printf("Silver"); break; case 'N': printf("NONE"); break; } printf("\n"); } return; } void PrintValue(double r, int tol) { int steps; printf("Resistor Value: "); steps = 0; if(1.0 > r) { r *= 1000.0; steps--; } while(1000.0 < r) { r /= 1000.0; steps++; } printf("%7.2f",r); switch(steps) { case -1: printf(" milliohms"); break; case 0: printf(" ohms"); break; case 1: printf(" kilohms"); break; case 2: printf(" megohms"); break; case 3: printf(" gigohms"); break; } printf(" (%i%%)\n", tol); return; }