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;
}