Compare commits

...

3 Commits

Author SHA1 Message Date
1e7545637b Update resistormaid.c 2024-02-25 13:58:45 -08:00
f55645c7a7 Update Makefile 2024-02-25 13:57:48 -08:00
d8eb0617f6 2.0.0 2024-02-25 13:56:31 -08:00
2 changed files with 43 additions and 6 deletions

View File

@ -1,5 +1,6 @@
CC=gcc
CFLAGS=-Wall -O2
CFLAGS=-Wall -Wextra -Wpedantic -O2 -std=c99
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

@ -1,13 +1,16 @@
#include <stdio.h>
#include <stdint.h>
#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 +76,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 +94,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 +103,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 +136,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: