/*
This program will create pointers array to the objects of the class resistor. The sorting was done using the nominal resistance well using the pointers arithmetic.
My initial menu will give the option to create a resistor with a maximum of 10 letting the user know if they have hit three maximum amounts of resistors.my second option will give the user the ability to sort the resistors they have entered if they have not entered a maximum of 10 resistors it will tell the user they have not entered their maximum amount of resistors but will display the resistors unsorted. Then finally the last option will allow the user to quit the program.
*/
[resistor.h]
class Resistor
{
public:
static int objCount;
Resistor(void);//constructor
Resistor(double, double);
void setNominalResistance(double);//set nominal resistance
void setTolerance(double);//set tolerance
double getNominalResistance();//get nominal resistance
double getTolerance();//get tolerance
double getMinResTolerance();//get minimum tolerance
double getMaxResTolerance();//get maximum tolerance
~Resistor(void);//destructor
private:
double nominalResistance;
double tolerance;
};
[resistor.cpp]
#include "resistor.h"
#include<iostream>
using namespace std;
Resistor::Resistor(void) //constructor
{
}
Resistor::Resistor(double nomRes, double tol)
{
nominalResistance = nomRes;
tolerance = tol;
objCount = objCount + 1;
if(nomRes < 1000 || nomRes > 10000)
{
objCount = objCount - 1;
}
}
void Resistor::setNominalResistance(double nomRes)//set nominal resistance
{
nominalResistance = nomRes;
}
void Resistor::setTolerance(double tol) //set tolerance
{
tolerance = tol;
}
double Resistor::getNominalResistance() //get nominal resistance
{
return nominalResistance;
}
double Resistor::getTolerance()//get tolerance
{
return tolerance;
}
double Resistor::getMinResTolerance() //get minimum tolerance
{
return nominalResistance - tolerance;
}
double Resistor::getMaxResTolerance() //get maximum tolerance
{
return nominalResistance + tolerance;
}
Resistor::~Resistor(void) //destructor
{
}
[Main.cpp]
#include "resistor.h"
#include<iostream>
using namespace std;
//Prototype
void SortObj(Resistor **);
void displayMenu(void);
char getMenuSelection(void);
int Resistor::objCount = 0;
void main()
{
Resistor *ptrResistor[10];
int i = 0;
char action;
double res, tol;
do
{
displayMenu(); //displays the intitial menu
action = getMenuSelection(); //gets the menu selection
if(action == 'c' || action == 'C')
{
if(Resistor::objCount == 10)
{
cout << "you have entered the maximum number of 10 resistors." << endl;
}
do
{
cout << "You must enter a resistance value between 1000 and 10000" << endl;
if(Resistor::objCount < 10)
{
cout << "\nEnter R" << i + 1 << " Resistance:";
cin >> res;
cout << "Enter R" << i + 1 << " Tolerance:";
cin >> tol;
ptrResistor[i] = new Resistor(res, tol);
}
}
while(res < 1000 || res > 10000);
i++;
}
else if(action == 's' || action == 'S')
{
if(Resistor::objCount < 10)
{
cout << "You have not used the maximum of 10 resistors" << endl;
}
else
SortObj(&ptrResistor[0]);
for(int j = 0; j < Resistor::objCount; j++)
{
cout << "R" << j + 1 << "\n";
cout << "Nominal Resistance:" << ptrResistor[j] -> getNominalResistance() << "\n";
cout << "Tolerance:" << ptrResistor[j] -> getTolerance() << "\n";
cout << "Max Resistance Tolerance:" << ptrResistor[j] -> getMaxResTolerance() << "\n";
cout << "Min Resistance Tolerance:" << ptrResistor[j] -> getMinResTolerance() << "\n\n";
}
}
}
while(action != 'q' || action != 'Q'); //loop until program has been quit
cin.ignore(2);
delete [] ptrResistor;
}
void displayMenu() //Displays the initial starting menu options
{
cout << "==================================================================" << endl;
cout << "|****** Enter C if you would like to create another resistor ****|" << endl;
cout << "==================================================================" << endl;
cout << "==================================================================" << endl;
cout << "|*** Enter S if you have entered 10 resistors to sort results ***|" << endl;
cout << "==================================================================" << endl;
cout << "==================================================================" << endl;
cout << "|*********************** Enter Q To quit ************************|" << endl;
cout << "==================================================================" << endl;
}
char getMenuSelection() //Gets the menu selection
{
cout << "Enter your choice and press enter:";
char action;
cin >> action;
switch(action)
{
case 'c':
case 'C':
return action;
break;
case 's':
case 'S':
return action;
break;
case 'q':
case 'Q':
exit(0);
break;
default: //if menu selection is not valid let the user know
cout << "\nInvalid selection: try again" << endl;
displayMenu();
getMenuSelection();
break;
}
}
void SortObj(Resistor **R)
{
int i, j, mark = 1; // set marker to 1 for the first pass
Resistor *temp; //pointer to help swap pointers
for(i = 1; (i <= 10) && mark; i++)
{
mark = 0;
for (j=0; j < (9); j++)
{
if (R[j + 1] -> getNominalResistance() < R[j] -> getNominalResistance())
{
temp = R[j]; // swap elements
R[j] = R[j + 1];
R[j + 1] = temp;
mark = 1; // marks where the swap occurred.
}
}
}
delete temp;
return;
}