please, Just comment this program ( just explain how each code works!!) #include
ID: 3631432 • Letter: P
Question
please, Just comment this program ( just explain how each code works!!)#include <string.h>
#include <iostream>
#include <conio.h>
using namespace std;
#define BLACK "Black"
#define BROWN "Brown"
#define RED "Red"
#define ORANGE "Orange"
#define YELLOW "Yellow"
#define GREEN "Green"
#define BLUE "Blue"
#define VIOLET "Violet"
#define GRAY "Gray"
#define WHITE "White"
#define GOLD "Gold"
#define SILVER "Silver"
struct ColorCodes
{
string Color1;
string Color2;
string Color3;
};
string getColorFromDigit(int digit)
{
string color = (string)BLACK;
switch(digit)
{
case 0:
color = BLACK;
break;
case 1:
color = BROWN;
break;
case 2:
color = RED;
break;
case 3:
color = ORANGE;
break;
case 4:
color = YELLOW;
break;
case 5:
color = GREEN;
break;
case 6:
color = BLUE;
break;
case 7:
color = VIOLET;
break;
case 8:
color = GRAY;
break;
case 9:
color = WHITE;
break;
}
return color;
}
string getColorFromDigit(char digit)
{
string color = (string)BLACK;
switch(digit)
{
case '0':
color = BLACK;
break;
case '1':
color = BROWN;
break;
case '2':
color = RED;
break;
case '3':
color = ORANGE;
break;
case '4':
color = YELLOW;
break;
case '5':
color = GREEN;
break;
case '6':
color = BLUE;
break;
case '7':
color = VIOLET;
break;
case '8':
color = GRAY;
break;
case '9':
color = WHITE;
break;
}
return color;
}
ColorCodes getUnaryCodes(int ohms)
{
ColorCodes codes;
codes.Color1 = BLACK;
codes.Color2 = getColorFromDigit(ohms);
codes.Color3 = BLACK;
return codes;
}
ColorCodes getBinaryCodes(int ohms)
{
ColorCodes codes;
int firstColor = ohms / 10;
int secondColor = ohms % 10;
codes.Color1 = getColorFromDigit(firstColor);
codes.Color2 = getColorFromDigit(secondColor);
codes.Color3 = BLACK;
return codes;
}
ColorCodes getTernaryCodes(int ohms)
{
ColorCodes codes;
int firstColor=0,secondColor = 0;
int remainder=0;
firstColor = ohms / 100;
remainder = ohms % 100;
secondColor = remainder / 10;
remainder = remainder % 10;
if(remainder > 0)
{
codes.Color1 = BLACK;
codes.Color2 = BLACK;
codes.Color3 = BLACK;
}
else
{
codes.Color1 = getColorFromDigit(firstColor);
codes.Color2 = getColorFromDigit(secondColor);
codes.Color3 = BROWN;
}
return codes;
}
ColorCodes getCodes(int ohms)
{
ColorCodes codes;
codes.Color1 = BLACK;
codes.Color2 = BLACK;
codes.Color3 = BLACK;
bool bInvalid = false;
char str_ohms[20];
itoa(ohms,str_ohms,10);
for(unsigned int index = 2; index < strlen(str_ohms);index++)
{
if(str_ohms[index] != '0')
{
bInvalid = true;
break;
}
}
if(bInvalid == false)
{
codes.Color1 = getColorFromDigit(str_ohms[0]);
codes.Color2 = getColorFromDigit(str_ohms[1]);
int radix = strlen(str_ohms) - 2;
codes.Color3 = getColorFromDigit(radix);
}
return codes;
}
void PrintColorCodes(ColorCodes codes)
{
cout<<endl<<"Color Codes are ("<<codes.Color1.c_str()<<","<<codes.Color2.c_str()<<","<<codes.Color3.c_str()<<")"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int option=0;
do{
cout<<"Select an option"<<endl;
cout<<"1- Enter Resistance in kilo Ohms(kOhms)>>"<<endl;
cout<<"2 - Calculate Voltage"<<endl;
cout<<"3- Quit Program"<<endl<<endl;
cin >>option;
if(option != 1 && option != 2 && option != 3)
{
cout<<"Enter correct option:"<<endl<<endl;
continue;
}
else if (option == 1)
{
double kiloOhms;
cout<<"Please enter your resistance in KiloOhms (kOhms)>>";
cin>>kiloOhms;
int ohms = int(kiloOhms * 1000);
ColorCodes finalCodes;
if(ohms >0 && ohms < 10)
{
finalCodes = getUnaryCodes(ohms);
PrintColorCodes(finalCodes);
}
else if(ohms >9 && ohms < 100)
{
finalCodes = getBinaryCodes(ohms);
PrintColorCodes(finalCodes);
}
else if (ohms > 99 && ohms < 1000)
{
finalCodes = getTernaryCodes(ohms);
if( strcmp(finalCodes.Color1.c_str(),BLACK) == 0
&& strcmp(finalCodes.Color2.c_str(),BLACK) == 0
&& strcmp(finalCodes.Color3.c_str(),BLACK) == 0)
{
cout<<"Color not in E12/E24 series"<<endl<<endl;
}
else
{
PrintColorCodes(finalCodes);
}
}
else if (ohms >999)
{
finalCodes = getCodes(ohms);
if( strcmp(finalCodes.Color1.c_str(),BLACK) == 0
&& strcmp(finalCodes.Color2.c_str(),BLACK) == 0
&& strcmp(finalCodes.Color3.c_str(),BLACK) == 0)
{
cout<<"Color not in E12/E24 series"<<endl<<endl;
}
else
{
PrintColorCodes(finalCodes);
}
}
else
{
cout<<"Color not in E12/E24 series"<<endl<<endl;
}
}
else if (option == 2)
{
double kiloOhms;
int current;
cout<<"Please enter your resistance in KiloOhms (kOhms)>>";
cin>>kiloOhms;
cout<<"Please enter your current in amperes (A)>>";
cin>>current;
int ohms = int(kiloOhms * 1000);
double voltage = ohms * current;
cout<<"Your Voltage is "<<voltage<<" Volts"<<endl;
}
}while(option != 3);
return 0;
}
Explanation / Answer
Here you go. #include #include #include using namespace std; #define BLACK "Black" #define BROWN "Brown" #define RED "Red" #define ORANGE "Orange" #define YELLOW "Yellow" #define GREEN "Green" #define BLUE "Blue" #define VIOLET "Violet" #define GRAY "Gray" #define WHITE "White" #define GOLD "Gold" #define SILVER "Silver" // This structure stores three colors to represent the three bands struct ColorCodes { string Color1; string Color2; string Color3; }; // This function maps a digit to a color based on the specified table for resistor colors string getColorFromDigit(int digit) { string color = (string)BLACK; switch(digit) { case 0: color = BLACK; break; case 1: color = BROWN; break; case 2: color = RED; break; case 3: color = ORANGE; break; case 4: color = YELLOW; break; case 5: color = GREEN; break; case 6: color = BLUE; break; case 7: color = VIOLET; break; case 8: color = GRAY; break; case 9: color = WHITE; break; } return color; } // This function maps a digit to a color based on the specified table for resistor colors. // It's just overloaded to accept a char instead of an int. I'm not sure why they did this. string getColorFromDigit(char digit) { string color = (string)BLACK; switch(digit) { case '0': color = BLACK; break; case '1': color = BROWN; break; case '2': color = RED; break; case '3': color = ORANGE; break; case '4': color = YELLOW; break; case '5': color = GREEN; break; case '6': color = BLUE; break; case '7': color = VIOLET; break; case '8': color = GRAY; break; case '9': color = WHITE; break; } return color; } // For single digit resistances it goes "BLACK BLACK" // so just compute the color for the digit and put it in the middle. ColorCodes getUnaryCodes(int ohms) { ColorCodes codes; codes.Color1 = BLACK; codes.Color2 = getColorFromDigit(ohms); codes.Color3 = BLACK; return codes; } // For two digit resitances it goes " BLACK" // so compute the colors for each digit separately. ColorCodes getBinaryCodes(int ohms) { ColorCodes codes; int firstColor = ohms / 10; // find the tens digit int secondColor = ohms % 10; // find the ones digit // note that % is a "mod" or remainder operator codes.Color1 = getColorFromDigit(firstColor); codes.Color2 = getColorFromDigit(secondColor); codes.Color3 = BLACK; return codes; } // For three colors perform a similar action to two colors by // separating the individual digits. ColorCodes getTernaryCodes(int ohms) { ColorCodes codes; int firstColor=0,secondColor = 0; int remainder=0; firstColor = ohms / 100; // hundreds digit remainder = ohms % 100; secondColor = remainder / 10; // tens digit remainder = remainder % 10; // ones digit // If there's a ones digit that's non-zero it's "BLACK BLACK BLACK" to // indicate an invalid value. if(remainder > 0) { codes.Color1 = BLACK; codes.Color2 = BLACK; codes.Color3 = BLACK; } else // If the ones digit is zero then it's " BROWN" { codes.Color1 = getColorFromDigit(firstColor); codes.Color2 = getColorFromDigit(secondColor); codes.Color3 = BROWN; } return codes; } // Computes the color codes for a resistance by converting the resistance // to a string and looking at each digit individually. This is to work // with resitances over 999 that have only two non-zero digits after the // first two. That is ohms must be of the form "xy000..." where x,y are // between zero and 9. ColorCodes getCodes(int ohms) { ColorCodes codes; codes.Color1 = BLACK; codes.Color2 = BLACK; codes.Color3 = BLACK; bool bInvalid = false; char str_ohms[20]; itoa(ohms,str_ohms,10); for(unsigned int index = 2; indexRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.