/* resistor.c by Nannette Thacker */ /* In the field of Electronics, a device by name resistor is often used to increase or decrease the flow of current. The resistance of a resistor is considered to be an important value that has to be determined while an electronic circuit is designed. The resistors are coded with colored bands through which you can find their values. The color codes are as follows: Color Code Value Black B 0 Brown N 1 Red R 2 Orange O 3 Yellow Y 4 Green G 5 Blue E 6 Violet V 7 Gray A 8 White W 9 This application takes three values input by the user, converts the character values to a number, returns the color strings and the converted number use -lm value at end of compilation string for pow() to work in UNIX */ #include #include /* prototypes */ char readinput(void); /* function to read in the input by the user */ int decode(int *, int *, int *, char, char, char); /* function to decode */ int decode1(char); /* subfunction used to decode */ float calcresistance(int, int, int); /* function to calculate the resistance */ void printresistance(float); /* function to print the resistance */ int main() { char color1, color2, color3 ; /* The character values input by the user */ int val1, val2, val3 ; /* the numeric values of the colors input */ float resisval ; /* the calculated value returned by the prg */ int quitval ; /* success or failure value returned */ int ctr = 0 ; /* initialize counter to get out of loop in case problems so won't endlessly loop */ printf("\n\nThe Really Cool Resistance Calculator.\n"); printf("Type QQQ as your response when you wish to quit."); do { printf("\n\nColor Chart\n"); printf("Black=B, Brown=N, Red=R, Orange=O, Yellow=Y\n"); printf("Green=G, Blue=E, Violet=V, Gray=A, White=W, Quit=QQQ\n"); printf("\nPlease enter three codes: "); color1 = readinput(); /* read in the values from the user */ color2 = readinput(); color3 = readinput(); getchar(); /* read in final enter for line */ /* program assumes user will enter 3 characters and an enter and not try to get weird. */ /* additional keys will be stored in the buffer and continue the loop. */ if(color1 != 'q' && color1 != 'Q') /* check if quit, if not, print the next line */ printf("\nColor code of the resistor you entered: "); val1 = decode1(color1); if(val1 != 10 && val1 != -1) /* if the user did not cancel or have an invalid input */ { val2 = decode1(color2); if(val2 != 10 && val2 != -1) /* continue if did not cancel or have invalid input */ { val3 = decode1(color3); if( val3 != -1 && val3 != 10) /* continue if did not cancel or have invalid input */ { resisval = calcresistance(val1, val2, val3); /* calculate the resistance */ printresistance(resisval); /* print it */ } else quitval = val3 ; /* store the value to quitval to see if cancelled code */ } else quitval = val3 ; /* store so can check if cancelled */ } else quitval = val1 ; /* assign the quitval in case is cancelled */ ctr++; /* increment the emergency exit counter */ } while (quitval !=10 && ctr <= 25) ; /* end do while, quit after 25 whether done or not. Allows emergency exit. */ return(0); } char readinput(void) { /* read in the value by the user */ char ccolor ; ccolor = getchar(); return(ccolor); } int decode(int *vval1, int *vval2, int *vval3, char color1, char color2, char color3) { /* call the subroutine to go through the switch statements and print the color */ printf("Color code of the resistor you entered: "); *vval1 = decode1(color1); if (*vval1 == -1 || *vval1 == 10) return(*vval1) ; *vval2 = decode1(color2); if (*vval2 == -1 || *vval2 == 10) return(*vval2) ; *vval3 = decode1(color3); if (*vval3 == -1 || *vval3 == 10) return(*vval3) ; else return 1 ; } int decode1(char color) { /* print the color and return the value of the color */ int returnval = 0 ; switch(color) { case 'Q': case 'q': printf("\n\nProgram terminated by user. Thank you!\n\n"); returnval = 10 ; break; case 'B': case 'b': printf("Black "); returnval = 0 ; break; case 'N': case 'n': printf("Brown "); returnval = 1; break; case 'R': case 'r': printf("Red "); returnval = 2; break; case 'O': case 'o': printf("Orange "); returnval = 3; break; case 'Y': case 'y': printf("Yellow "); returnval = 4; break; case 'G': case 'g': printf("Green "); returnval = 5; break; case 'E': case 'e': printf("Blue "); returnval = 6; break; case 'V': case 'v': printf("Violet "); returnval = 7; break; case 'A': case 'a': printf("Gray "); returnval = 8; break; case 'W': case 'w': printf("White "); returnval = 9; break; default: printf("\n\nInvalid input. Please type: QQQ if you wish to quit.\n\n"); returnval = -1; } return(returnval); } float calcresistance(int pass1, int pass2, int pass3) { /* calculate the resistance */ float resistval ; resistval = (10 * pass1 + pass2) * pow(10,pass3); return resistval ; } void printresistance(float resisprint) { /* print the resistance */ printf("\nResistance in OHMS: %f\n\n", resisprint); }