commit d7b39b77c2612193e087e44143df5d405c6a22db Author: Sateallia Date: Sun Feb 25 13:55:52 2024 -0800 1.0.0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..37c0934 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright © 2022 Sateallia + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2ee922e --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CC=gcc +CFLAGS=-Wall -O2 + +ifndef VERBOSE +.SILENT: +endif + +all: clean resistormaid + +resistormaid: resistormaid.o + +clean: + rm -f resistormaid *.o diff --git a/resistormaid.c b/resistormaid.c new file mode 100644 index 0000000..e88a03f --- /dev/null +++ b/resistormaid.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include + +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; +} + +