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
|
#include <cmath>
enum colour {
black,
brown,
red,
orange,
yellow,
green,
blue,
violet,
gray,
white,
gold,
silver,
none
};
class Resistor {
int colour2int(colour c) {
switch(c) {
case black: return 0;
case brown: return 1;
case red: return 2;
case orange: return 3;
case yellow: return 4;
case green: return 5;
case blue: return 6;
case violet: return 7;
case gray: return 8;
case white: return 9;
case gold: return -1;
case silver: return -2;
default: return 0;
}
}
colour int2colour(int i) {
switch(i) {
case -2: return silver;
case -1: return gold;
case 0: return black;
case 1: return brown;
case 2: return red;
case 3: return orange;
case 4: return yellow;
case 5: return green;
case 6: return blue;
case 7: return violet;
case 8: return gray;
case 9: return white;
default: return none;
}
}
float colour2tolerance(colour c4) {
switch (c4) {
case brown : return 0.01f;
case red : return 0.02f;
case yellow: return 0.05f;
case green : return 0.005f;
case blue : return 0.0025f;
case violet: return 0.001f;
case gray : return 0.0005f;
case gold : return 0.05f;
case silver: return 0.10f;
case none :
default : return 0.20f;
}
}
float colour2temp(colour c5) {
switch (c5) {
case black : return 250.f;
case brown : return 100.f;
case red : return 50.f;
case orange: return 15.f;
case yellow: return 25.f;
case green : return 20.f;
case blue : return 10.f;
case violet: return 5.f;
case gray : return 1.f;
default : return 0.f;
}
}
float colour2resistance(colour c1, colour c2, colour c3) {
return (colour2int(C1)*10 + colour2int(C2)) * pow(10.f , (float)colour2int(C3));
}
public:
Resistor() : C1(none), C2(none), C3(none), C4(none), C5(none), R(0.f) {}
colour C1; // The colour bands
colour C2;
colour C3;
colour C4;
colour C5;
float R; // The resistance value
float Tol; // The tolerance
float TC; // Temperature Coeffecient
void setColours(colour c1, colour c2, colour c3, colour c4 = none, colour c5 = none) {
C1 = c1;
C2 = c2;
C3 = c3;
C4 = c4;
C5 = c5;
R = colour2resistance(c1,c2,c3);
Tol = colour2tolerance(c4);
TC = colour2temp(c5);
}
void setResistance(float r) {
int exp = (int) std::log10( r / 10.f );
R = r;
C3 = int2colour(exp);
C1 = int2colour( (int) R / pow(10,exp+1) );
C2 = int2colour( (int( R / pow(10,exp ))) % 10 );
}
};
|