From d8eb0617f6fc9834d8c497c3a67766ddd5fd4cf1 Mon Sep 17 00:00:00 2001 From: Sateallia Date: Sun, 25 Feb 2024 13:56:31 -0800 Subject: [PATCH] 2.0.0 --- Makefile | 4 +++- resistormaid.c | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 2ee922e..854951a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CC=gcc CFLAGS=-Wall -O2 +LDFLAGS=-lm ifndef VERBOSE .SILENT: @@ -7,7 +8,8 @@ endif all: clean resistormaid -resistormaid: resistormaid.o +resistormaid: resistormaid.c + $(CC) $(CFLAGS) resistormaid.c -o resistormaid $(LDFLAGS) clean: rm -f resistormaid *.o diff --git a/resistormaid.c b/resistormaid.c index e88a03f..af955c6 100644 --- a/resistormaid.c +++ b/resistormaid.c @@ -2,12 +2,14 @@ #include #include #include +#include int16_t value = 0; -float tolerance = 20; -int8_t multiplier = 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; @@ -73,6 +75,7 @@ int main(int argc, char **argv) { 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] @@ -90,7 +93,7 @@ int main(int argc, char **argv) { goto exit; } - if(numargs > 3) { // handles argv[5] + 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"); @@ -99,6 +102,22 @@ int main(int argc, char **argv) { } } + 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; @@ -116,8 +135,23 @@ int main(int argc, char **argv) { 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("%i 10^%i +-%g%%", value, multiplier, tolerance); + 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: