resistormaid/resistormaid.c
2024-02-25 13:55:52 -08:00

128 lines
4.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int16_t value = 0;
float tolerance = 20;
int8_t multiplier = 0;
int16_t sixDigit = 0;
int8_t hasThirdValueBand = 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[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;
}
}
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;
}
}
printf("%i 10^%i +-%g%%", value, multiplier, tolerance);
if(sixDigit != 0) printf(" %ippm", sixDigit);
printf("\n");
exit:
return 0;
}