Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Resistor Colour Code Calculator
Index -> Programming, C -> C Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: Wed Feb 05, 2014 6:39 pm   Post subject: Resistor Colour Code Calculator

I'm so excited about this, being my first personal C project, that I wanted to put an exclamation mark in the title Razz (or maybe I've had too much wine)...
Anyone who's just starting Electrical Engineering, or uses resistors a lot could really appreciate this! Smile
Anyhow, here it is!
(It's not completely error-proofed, meaning you CAN crash it, but it works perfectly)

code:
/*
Author: Sean Sciberras
Last edited: Feb. 05/2014
Description: Calculates resistances of 4 and 5 banded resistors.
*/
#include <stdio.h>
#include <math.h>
#include <string.h>

int sigfigs(char band[10]);
float mult(char band[10]);
float tol(char band[10]);
//--------------------------------------------------------------------------------------------------------------
int main(void)
{
        int bands = 0;
        char band1[10];
        char band2[10];
        char band3[10];
        char band4[10];
        char band5[10];
        float resistance;
        float resistance_min;
        float resistance_max;
        printf("Number of colour bands: ");
        scanf_s("%i", &bands);
        printf("\nBand 1: ");
        scanf_s("%s", band1, 10);
        if (sigfigs(band1) == 100)
        {
                printf("Colour is invalid!");
        }
        fflush(stdin);
        printf("\nBand 2: ");
        scanf_s("%s", band2, 10);
        if (sigfigs(band2) == 100)
        {
                printf("Colour is invalid!");
        }
        fflush(stdin);
        printf("\nBand 3: ");
        scanf_s("%s", band3, 10);
        if (bands == 5)
        {
                if (sigfigs(band3) == 100)
                {
                        printf("Colour is invalid!");
                }
        }
        else if (bands == 4)
        {
                if (mult(band3) == 9)
                {
                        printf("Colour is invalid!");
                }
        }
        fflush(stdin);
        printf("\nBand 4: ");
        scanf_s("%s", band4, 10);
        if (mult(band4) == 9)
        {
                printf("Colour is invalid!");
        }
        fflush(stdin);
        if (bands == 5)
        {
                printf("\nBand 5: ");
                scanf_s("%s", band5, 10);
                if (tol(band5) == 9)
                {
                        printf("Colour is invalid!");
                }
                fflush(stdin);
        }

        if (bands == 4)
        {
                resistance = ((sigfigs(band1) * 10) + (sigfigs(band2) * 1)) * mult(band3);
                resistance_min = resistance - (resistance * tol(band4));
                resistance_max = resistance + (resistance * tol(band4));
                system("cls");
                printf("Band 1: %10s -> %i\n", band1, sigfigs(band1)*10);
                printf("Band 2: %10s -> %i\n", band2, sigfigs(band2)*1);
                printf("Band 3: %10s -> x%.2f\n", band3, mult(band3));
                printf("Band 4: %10s -> +/-%.4f%%\n", band4, tol(band4)*100);
                printf("Resistance = (%i x 10 + %i x 1) x %.2f = %.2fohms\n", sigfigs(band1), sigfigs(band2), mult(band3), resistance);
                printf("Max. Resistance = %.2f + %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band4), resistance_max);
                printf("Min. Resistance = %.2f - %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band4), resistance_min);
        }
        else if (bands == 5)
        {
                resistance = ((sigfigs(band1) * 100) + (sigfigs(band2) * 10) + (sigfigs(band3) * 1)) * mult(band4);
                resistance_min = resistance - (resistance * tol(band5));
                resistance_max = resistance + (resistance * tol(band5));
                system("cls");
                printf("Band 1: %10s -> %i\n", band1, sigfigs(band1) * 100);
                printf("Band 2: %10s -> %i\n", band2, sigfigs(band2) * 10);
                printf("Band 3: %10s -> %i\n", band3, sigfigs(band3) * 1);
                printf("Band 4: %10s -> x%.2f\n", band4, mult(band4));
                printf("Band 5: %10s -> +/-%.4f%%\n", band5, tol(band5) * 100);
                printf("Resistance = (%i x 100 + %i x 10 + %i x 1) x %.2f = %.2fohms\n", sigfigs(band1), sigfigs(band2), sigfigs(band3), mult(band4), resistance);
                printf("Max. Resistance = %.2f + %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band5), resistance_max);
                printf("Min. Resistance = %.2f - %.2f x %.4f = %.2fohms\n", resistance, resistance, tol(band5), resistance_min);
        }
        printf("Press any key to continue...");
        getch();
}
//----------------------------------------------------------------------------------------------------
int sigfigs(char band[10])
{
        if (strcmp(band, "black") == 0)
                return (0);
        else if (strcmp(band, "brown") == 0)
                return (1);
        else if (strcmp(band, "red") == 0)
                return (2);
        else if (strcmp(band, "orange") == 0)
                return (3);
        else if (strcmp(band, "yellow") == 0)
                return (4);
        else if (strcmp(band, "green") == 0)
                return (5);
        else if (strcmp(band, "blue") == 0)
                return (6);
        else if (strcmp(band, "violet") == 0)
                return (7);
        else if (strcmp(band, "grey") == 0)
                return (8);
        else if (strcmp(band, "white") == 0)
                return (9);
        else
                return (100);
}

float mult(char band[10])
{
        if (strcmp(band, "black") == 0)
                return (1);
        else if (strcmp(band, "brown") == 0)
                return (10);
        else if (strcmp(band, "red") == 0)
                return (100);
        else if (strcmp(band, "orange") == 0)
                return (1000);
        else if (strcmp(band, "yellow") == 0)
                return (10000);
        else if (strcmp(band, "green") == 0)
                return (100000);
        else if (strcmp(band, "blue") == 0)
                return (1000000);
        else if (strcmp(band, "violet") == 0)
                return (10000000);
        else if (strcmp(band, "gold") == 0)
                return (0.1);
        else if (strcmp(band, "silver") == 0)
                return (0.01);
        else
                return (9);
}

float tol(char band[10])
{
        if (strcmp(band, "brown") == 0)
                return (0.01);
        else if (strcmp(band, "red") == 0)
                return (0.02);
        else if (strcmp(band, "green") == 0)
                return (0.005);
        else if (strcmp(band, "blue") == 0)
                return (0.0025);
        else if (strcmp(band, "violet") == 0)
                return (0.001);
        else if (strcmp(band, "grey") == 0)
                return (0.0005);
        else if (strcmp(band, "gold") == 0)
                return (0.05);
        else if (strcmp(band, "silver") == 0)
                return (0.1);
        else
                return (9);
}
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Wed Feb 05, 2014 8:37 pm   Post subject: RE:Resistor Colour Code Calculator

So what does this do, exactly? I have no way to compile and run c code at the moment.
Insectoid




PostPosted: Wed Feb 05, 2014 8:58 pm   Post subject: RE:Resistor Colour Code Calculator

Looks like it takes the colours of the bands on a resister as input and returns the resistance and tolerance of that resistor.
Srlancelot39




PostPosted: Thu Feb 06, 2014 12:11 am   Post subject: RE:Resistor Colour Code Calculator

Precisely what Insectoid said Razz
Nice job on that console connect 4 by the way! Haven't tried it, but I examined the code and it looks pretty damn complex lol
Insectoid




PostPosted: Thu Feb 06, 2014 1:06 pm   Post subject: RE:Resistor Colour Code Calculator

This can be improved dramatically. Let's start with your sigfigs function. You have a list of strings that correspond to the numbers 0 through 9. An array and a loop can shorten that ugly function to five or six lines. Same with your mult function.

I notice you have a separate variable for each band. You only need one, because once you've processed a band, you do not need it any more, so you can discard the value and re-use the variable. While processing band 1, you don't need to reference band 2, and when processing band 2, you don't need band 1 anymore. All you need is a counter to total up the values. For example:

code:

int num_bands = number of bands on resistor;
int total = 0;
char band;

//this for loop gets all the significant figures and stores them in the variable total.
for (int i = 0; i < num_bands -2, i ++){ //num_bands includes multiplier and tolerance bands, and we don't want to deal with those yet
    get band;
    total = total + sigFigs (band);
    total = total * 10;
}

get band; //now we deal with the multiplier band
total = total * mult(band);
Srlancelot39




PostPosted: Wed Feb 12, 2014 8:15 am   Post subject: RE:Resistor Colour Code Calculator

Thanks for the improvements! Yeah I never went back afterwards to make the code more efficient, I sort of just wanted to see it working Razz

Very smart idea using one variable for all bands, but now I'm wondering, how would you display the information for each band with only the one variable? Would you need to display as you go, and also do the math as you go as well?
Insectoid




PostPosted: Wed Feb 12, 2014 5:05 pm   Post subject: RE:Resistor Colour Code Calculator

You can print the digit values of the bands as you go but without knowing the multiplier band you can't print the actual value. However, with an array of bands you can still use the looping algorithm and print the data after your calculations. This has the added benefit of eliminating one of your giant if blocks and replacing it with a one-line loop.
Display posts from previous:   
   Index -> Programming, C -> C Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: