#include #include #include #include #include int16_t value = 0; int16_t sixDigit = 0; float tolerance = 20; float multiplier = 0; int8_t hasThirdValueBand = 0; int8_t k,m,g = 0; int colorValues(char *colorInput) { if(!strcmp(colorInput, "black")) return 0; else if(!strcmp(colorInput, "brown")) return 1; else if(!strcmp(colorInput, "red")) return 2; else if(!strcmp(colorInput, "orange")) return 3; else if(!strcmp(colorInput, "yellow")) return 4; else if(!strcmp(colorInput, "green")) return 5; else if(!strcmp(colorInput, "blue")) return 6; else if(!strcmp(colorInput, "violet")) return 7; else if(!strcmp(colorInput, "purple")) return 7; else if(!strcmp(colorInput, "gray")) return 8; else if(!strcmp(colorInput, "grey")) return 8; else if(!strcmp(colorInput, "white")) return 9; else if(!strcmp(colorInput, "gold")) return -1; else if(!strcmp(colorInput, "golden")) return -1; else if(!strcmp(colorInput, "silver")) return -2; else return -3; } float toleranceValues(char *toleranceInput) { if(!strcmp(toleranceInput, "brown")) return 1; else if(!strcmp(toleranceInput, "red")) return 2; else if(!strcmp(toleranceInput, "orange")) return 3; else if(!strcmp(toleranceInput, "yellow")) return 4; else if(!strcmp(toleranceInput, "green")) return 0.5; else if(!strcmp(toleranceInput, "blue")) return 0.25; else if(!strcmp(toleranceInput, "violet")) return 0.1; else if(!strcmp(toleranceInput, "purple")) return 0.1; else if(!strcmp(toleranceInput, "gray")) return 0.05; else if(!strcmp(toleranceInput, "grey")) return 0.05; else if(!strcmp(toleranceInput, "gold")) return 5; else if(!strcmp(toleranceInput, "golden")) return 5; else if(!strcmp(toleranceInput, "silver")) return 10; else return -1; } int main(int argc, char **argv) { int8_t numargs = argc - 1; // this is more practical than doing (argc - 1) every call if(numargs > 6) { printf("Too many arguments! Exiting now.\n"); goto exit; } else if(numargs == 0) { printf("Usage: resistormaid first second [third] multiplier [tolerance] [temp_coefficient]\n"); printf("Valid arguments: black, brown, red, orange, yellow, green, blue, violet, purple, gray, grey, white, gold, golden, silver\n"); goto exit; } else if(numargs < 3) { printf("Too few arguments! Exiting now.\n"); goto exit; } for(int i = 1; i <= numargs; i++) { for(int j = 0; argv[i][j]; j++) argv[i][j] = tolower(argv[i][j]); } int8_t firstDigit = colorValues(argv[1]); int8_t secondDigit = colorValues(argv[2]); if(firstDigit < 0) { printf("Invalid first argument! Exiting now.\n"); goto exit; } else if(secondDigit < 0) { printf("Invalid second argument! Exiting now.\n"); goto exit; } value = firstDigit * 10 + secondDigit; // handles argv[1] and argv[2] if(numargs > 4) { hasThirdValueBand = 1; int8_t thirdDigit = colorValues(argv[3]); // handles argv[3] if(thirdDigit < -2) { printf("Invalid third argument! Exiting now.\n"); goto exit; } value = value * 10 + thirdDigit; } multiplier = colorValues(argv[3 + hasThirdValueBand]); // handles argv[3] and argv[4] if(multiplier < -2) { if(hasThirdValueBand) printf("Invalid fourth argument! Exiting now.\n"); else printf("Invalid third argument! Exiting now.\n"); goto exit; } if(numargs > 3) { // handles argv[4] and argv[5] tolerance = toleranceValues(argv[4 + hasThirdValueBand]); if(tolerance == -1) { if(hasThirdValueBand) printf("Invalid fifth argument! Exiting now.\n"); else printf("Invalid fourth argument! Exiting now.\n"); goto exit; } } multiplier = pow(10.0, multiplier); float output = value * multiplier; if(output >= 1000.0) { k = 1; output = output / 1000; } if(output >= 1000.0) { m = 1; output = output / 1000; } if(output >= 1000.0) { g = 1; output = output / 1000; } if(numargs > 5) { // handles argv[6] if(!strcmp(argv[6], "black")) sixDigit = 250; else if(!strcmp(argv[6], "brown")) sixDigit = 100; else if(!strcmp(argv[6], "red")) sixDigit = 50; else if(!strcmp(argv[6], "orange")) sixDigit = 15; else if(!strcmp(argv[6], "yellow")) sixDigit = 25; else if(!strcmp(argv[6], "green")) sixDigit = 20; else if(!strcmp(argv[6], "blue")) sixDigit = 10; else if(!strcmp(argv[6], "violet")) sixDigit = 5; else if(!strcmp(argv[6], "purple")) sixDigit = 5; else if(!strcmp(argv[6], "gray")) sixDigit = 1; else if(!strcmp(argv[6], "grey")) sixDigit = 1; else { printf("Invalid sixth value! Exiting now.\n"); goto exit; } } char stringOut[16]; // remove the unnecessary '.' and '0' characters sprintf(stringOut, "%.2f", output); int8_t counter = strlen(stringOut) - 1; int8_t exitTracker = 0; while(stringOut[counter] == '0' || stringOut[counter] == '.') { if(stringOut[counter] == '.') exitTracker = 1; // so we stop after the dot stringOut[counter] = '\0'; counter--; if(exitTracker) break; } printf("%s", stringOut); if(g) printf("G"); else if(m) printf("M"); else if(k) printf("K"); printf(" Ohm +-%g%%", tolerance); if(sixDigit != 0) printf(" %ippm", sixDigit); printf("\n"); exit: return 0; }