1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
//Resistor color code // resistor calculating issue.
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "cstring"
#define SIZE = 24
double search(char array2[][7], char array1[], int size);
double resistorcheck( float t );
int main( void )
{
int v;
printf( "Please input which way you are inputting resistor values:\n\n" );
printf( " 1 = numerical valued resistors\n 2 = Color Codes.\n" );
scanf( "%d", &v );
if( v == 1)
{
int w = -1;
float ra, rb, series, parallel;
while( w = -1 )
{
printf( "Please input first resistor value\n");
scanf("%f", &ra);
w = resistorcheck( ra );
}
w = -1;
while( w = -1 )
{
printf( "Please input second resistor value\n");
scanf("%f", &rb);
w = resistorcheck( rb );
}
series = ( ra + rb );
parallel = ( (ra * rb ) / series );
printf( "Resistors in series are: %0.2f\n", series );
printf( "Resistors in Parallel ar: %0.2f\n\n", parallel );
}
if( v == 2)
{
char colorcodes[10][7] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white" };
char band1[7];
char band2[7];
char band3[7];
long float resistanceV;
int x;
int y;
int z;
int q;
printf( "Enter the color codes of the resistors starting left to right:\n\n" );
scanf("%s", band1);
x = search(colorcodes, band1, 10);
if( x == -1 || x == 0 )
{
printf( "invalid color %s", band1 );
}
else
{
printf("Enter band2: \n" );
scanf( "%s", band2);
y = search(colorcodes, band2, 10);
if( y == -1 )
{
printf("Invalid color %s", band2);
}
else
{
printf("Enter band3: \n" );
scanf( "%s", band3);
z = search(colorcodes, band3, 10 );
if( z == -1 )
{
printf( "Invalid color code %s", band3 );
}
else
{
resistanceV = ( 10*x+y )*( pow (10.f,z));
printf( " The resistance value is %0.2lf \n\n", resistanceV );
}
q = resistorcheck( resistanceV );
if ( q = 1 )
{
printf( "This is a standard resistor value\n\n" );
}
else
{
printf ( "This is not a standard resistor value\n\n" );
}
}
}
}
main();
return 0;
}
double search(char array2[][7], char array1[], int size)
{
int i = 0;
int found = 0;
int where;
while (!found && i<10)
{
if(strcmp(array2[ i ], array1) == 0)
{
found = 1;
}
else
{
++i;
}
}
if(found)
{
where = i;
}
else
{
where = -1;
}
return(where);
}
double resistorcheck( float t )
{
int i;
float array1[24] = { 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4,
2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1,
5.6, 6.2, 6.8, 7.5, 8.2, 9.1 };
while ( t >= 10 ){
t = t / 10;
}
for( i = 0; i <= 24; i++){
if( t == array1[i] ){
return 1;
}
printf( "Not a standrad value: %f\n", t );
return -1.0;
}
}
| |