Code:
#include<stdio.h>
#include<conio.h>
#include<stdbool.h>
#include<string.h>
bool color_check(char (*col)[7], const char (*r_col)[7]);
void get_resistance(char (*col)[7], const char (*r_col)[7]);
void get_tolerance(char (*col)[7], const char (*r_col)[7]);
void get_tempco(char (*col)[7], const char (*r_col)[7]);
char ch;
int main(void)
{
do{
char col[6][7];
const char r_col[12][7]={"silver","gold","black","brown","red","orange","yellow","green","blue","violet","grey","white"};
input:
printf("\t\t*****Resistor Calculator*****\n");
printf("Please enter the color of the six bands seperated by a space.\n");
scanf("%s %s %s %s %s %s", &col[0], &col[1], &col[2], &col[3], &col[4], &col[5]);
printf("%s %s %s %s %s %s", col[0], col[1], col[2], col[3], col[4], col[5]);
if(color_check(col, r_col))
{
void get_resistance(col, r_col);
void get_tolerance(col, r_col);
void get_tempco(col, r_col);
}
else
{
printf("You have entered an invalid color. Please try again");
goto input;
}
}while(ch == 'Y' || ch == 'y');
return 0;
}
bool color_check(char (*col)[7], const char (*r_col)[7])
{
int i, j;
for(i = 0; i < 12; i++)
{
for(j = 0; j < 7; j++)
{
if(strncmp(col[j], r_col[i], 7) != 0)
{
return false;
}
}
}
if((strncmp(col[0], r_col[2], 7) == 0)||(strncmp(col[0], r_col[1], 7) == 0)||(strncmp(col[0], r_col[0], 7) == 0))
{
return false;
}
else if((strncmp(col[1], r_col[1], 7) == 0)||(strncmp(col[1], r_col[0], 7) == 0)||(strncmp(col[2], r_col[1], 7) == 0)||(strncmp(col[2], r_col[0], 7) == 0))
{
return false;
}
else if(strncmp(col[4], r_col[11], 7) == 0)
{
return false;
}
else if((strncmp(col[5], r_col[7], 7) == 0)||(strncmp(col[5], r_col[10], 7) == 0)||(strncmp(col[5], r_col[11], 7) == 0)||(strncmp(col[5], r_col[1], 7) == 0)||(strncmp(col[5], r_col[0], 7) == 0))
{
return false;
}
else
{
return true;
}
}
void get_resistance(char (*col)[7], const char (*r_col)[7])
{
int i;
double color_pts, resist, exp;
for(i=0; i<4; i++)
{
if (strncmp(col[i], r_col[0], 7) == 0 )
{
color_pts = -2;
exp = .01;
}
else if (strncmp(col[i], r_col[1], 7) == 0 )
{
color_pts = -1;
exp = .1;
}
else if (strncmp(col[i], r_col[2], 7) == 0 )
{
color_pts = 0;
exp = 1;
}
else if (strncmp(col[i], r_col[3], 7) == 0 )
{
color_pts = 1;
exp = 10;
}
else if (strncmp(col[i], r_col[4], 7) == 0 )
{
color_pts = 2;
exp = 100;
}
else if (strncmp(col[i], r_col[5], 7) == 0 )
{
color_pts= 3;
exp = 1000;
}
else if (strncmp(col[i], r_col[6], 7) == 0 )
{
color_pts;
exp = 10000;
}
else if (strncmp(col[i], r_col[7], 7) == 0 )
{
color_pts = 5;
exp = 100000;
}
else if (strncmp(col[i], r_col[8], 7) == 0 )
{
color_pts = 6;
exp = 1000000;
}
else if (strncmp(col[i], r_col[9], 7) == 0 )
{
color_pts = 7;
exp = 10000000;
}
else if (strncmp(col[i], r_col[10], 7) == 0 )
{
color_pts = 8;
exp = 100000000;
}
else if (strncmp(col[i], r_col[11], 7) == 0 )
{
color_pts = 9;
exp = 1000000000;
}
if(i == 0)
{
resist = color_pts * 100;
}
if(i == 1)
{
resist += color_pts * 10;
}
if(i == 2)
{
resist += color_pts;
}
if(i == 3)
{
resist = color_pts * exp;
}
}
if(resist < 1000000)
{
printf("Resistance = %.2f kOhms\n", resist/1000);
}
else
{
printf("Resistance = %.2f MOhms\n", resist/1000000);
}
}
void get_tolerance(char (*col)[7], const char (*r_col)[7])
{
double tolerance;
if(strncmp(col[4], r_col[3], 7) == 0)
{
tolerance = 1;
}
else if(strncmp(col[4], r_col[4], 7) == 0)
{
tolerance = 2;
}
else if(strncmp(col[4], r_col[5], 7) == 0)
{
tolerance = 3;
}
else if(strncmp(col[4], r_col[6], 7) == 0)
{
tolerance = 4;
}
else if(strncmp(col[4], r_col[7], 7) == 0)
{
tolerance = .5;
}
else if(strncmp(col[4], r_col[8], 7) == 0)
{
tolerance = .25;
}
else if(strncmp(col[4], r_col[9], 7) == 0)
{
tolerance = .1;
}
else if(strncmp(col[4], r_col[10],7) == 0)
{
tolerance = .05;
}
else if(strncmp(col[4], r_col[1], 7) == 0)
{
tolerance = 5;
}
else if(strncmp(col[4], r_col[0], 7) == 0)
{
tolerance = 10;
}
printf("Tolerance = +/- %.2g%%\n", tolerance);
}
void get_tempco(char (*col)[7], const char (*r_col)[7])
{
int tempco;
if(strncmp(col[5], r_col[3], 7) == 0)
{
tempco = 100;
}
else if(strncmp(col[5], r_col[4], 7) == 0)
{
tempco = 50;
}
else if(strncmp(col[5], r_col[5], 7) == 0)
{
tempco = 15;
}
else if(strncmp(col[5], r_col[6], 7) == 0)\
{
tempco = 25;
}
else if(strncmp(col[5], r_col[8], 7) == 0)
{
tempco = 10;
}
else if(strncmp(col[5], r_col[9], 7) == 0)
{
tempco = 5;
}
printf("Tempco = %d ppm/degreeC\n", tempco);
}