This commit is contained in:
Sateallia 2024-02-25 13:56:31 -08:00
parent d7b39b77c2
commit d8eb0617f6
2 changed files with 41 additions and 5 deletions

View File

@ -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

View File

@ -2,12 +2,14 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
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: