1. Objective: Create a C++ console application that will model the characteristi
ID: 668306 • Letter: 1
Question
1.Objective: Create a C++ console application that will model the characteristics of a resistor.
Create a multifile project. Create and add to the project an h file containing the resistor-class definition.
Create and add to the project a cpp file containing the implementation of the class-member functions.
Create and add to the project a ccp file containing the main() function, which will instantiate a resistor object and test its member functions.
2.next
The resistor class will, at minimum, have members that do the following.
store the nominal resistance value of a resistor
store the tolerance of a resistor
initialize any and all nominal-resistance values to correct, EIA, nonzero values that are greater than 0 and less than 1,000,000 ohms
initialize any and all resistance-tolerance values to correct, E12, E24, E48, or E96 resistance-tolerance values
allow the nominal-resistance and tolerance values of a resistor object to be changed by the user
All member functions should have a test message stating the name of the function. All the test messages should be displayed or not displayed, depending on the value of a Boolean variable declared in main().
If the Boolean value = true, display the message.
If the Boolean value = false, do not display the message.
3.next
Function main() should instatiate two objects of class resistor.
Function main() should display the current values of all resistor objects.
Function main() should also calculate and display the minimum and maximum in-tolerance resistance values of each resistor object from the resistor data members.
Function main() should allow the user to change the values of the nominal resistance and the resistor tolerance of both resistor objects, and it should also correctly handle out of numeric-range input. Main() is also responsible for making sure that the user can successfully enter only correct, EIA resistance and tolerance values.The user should be given the following data-entry choices:
accept current EIA values for resistance and tolerance;
The function main() should display the new, modified values of the resistor object, including the new min and max in-tolerance resistance values.
The function main() should be executed twice: once with the test messages displayed and once without.
and will use this code
as part of it.
#include "Resistor.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//EIA Standard array element sizes per tolerance class
const int E12 = 12;
const int E24 = 24;
const int E48 = 48;
const int E96 = 96;
//EIA Standard Resistor Values
//E12 10% tolerance
//E24 5% tolerance
//E48 2% tolerance
//E96 1% tolerance
//10% standard values
const int nominalE12Values[E12] = { 100, 120, 150, 180, 220, 270, 330, 390, 470, 560, 680, 820 };
//5% standard values
const int nominalE24Values[E24] = { 100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910 };
//2% standard values
const int nominalE48Values[E48] = { 100, 105, 110, 115, 121, 127, 133, 140, 147, 154, 162, 169, 178, 187, 196, 205, 215, 226, 237, 249, 261, 274, 287, 301, 316, 332, 348, 365, 383, 402, 422, 442, 464, 487, 511, 536, 562, 590, 619, 649, 681, 715, 750, 787, 825, 866, 909, 953 };
//1% standard values
const int nominalE96Values[E96] = { 100, 102, 105, 107, 110, 113, 115, 118, 121, 124, 127, 130, 133, 137, 140, 143, 147, 150, 154, 158, 162, 165, 169, 174, 178, 182, 187, 191, 196, 200, 205, 210, 215, 221, 226, 232, 237, 243, 249, 255, 261, 267, 274, 280, 287, 294, 301, 309, 316, 324, 332, 340, 348, 357, 365, 374, 383, 392, 402, 412, 422, 432, 442, 453, 464, 475, 487, 499, 511, 523, 536, 549, 562, 576, 590, 604, 619, 634, 649, 665, 681, 698, 715, 732, 750, 768, 787, 806, 825, 845, 866, 887, 909, 931, 953, 976 };
void DisplayResistance(CResistor &, bool);
void EnterResistance(CResistor &, bool);
int main(void) //Control variable to display function names when called
{
cout << "LabWk2 by James Felts D01367113" << endl << endl;
bool fnDisp = false;
CResistor Res1(4700.0, 0.10, "Res1", fnDisp);
CResistor Res2(330.0, 0.10, "Res2", fnDisp);
CResistor Res3(10000.0, 0.10, "Res3", fnDisp);
DisplayResistance(Res1, fnDisp);
DisplayResistance(Res2, fnDisp);
DisplayResistance(Res3, fnDisp);
EnterResistance(Res1, fnDisp);
DisplayResistance(Res1, fnDisp);
cout << endl << endl << "Second run displaying all test messages" << endl; // Run a second time with test messages
fnDisp = true;
CResistor Res4(4700.0, 0.10, "Res1", fnDisp);
CResistor Res5(330.0, 0.10, "Res2", fnDisp);
CResistor Res6(10000.0, 0.10, "Res3", fnDisp);
DisplayResistance(Res4, fnDisp);
DisplayResistance(Res5, fnDisp);
DisplayResistance(Res6, fnDisp);
EnterResistance(Res4, fnDisp);
DisplayResistance(Res4, fnDisp);
system("pause");
}
void DisplayResistance(CResistor & resObj, bool fnDisplay) // Function displays all data member values of a CResistor object
{
//Local variables to hold CResistor data copies
double nom, tol, min, max;
nom = resObj.getNominal(fnDisplay);
tol = resObj.getTolerance(fnDisplay);
min = nom * (1.0 - tol);
max = nom * (1.0 + tol);
cout << endl << "Resistor object name is " << resObj.getName(fnDisplay) << endl;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(3);
cout << endl << "Nominal resistance value = " << setw(15) << nom << " ohms ";
cout << endl << "Tolerance = " << setw(15) << tol * 100 << " %";
cout << endl << "Minimum Resistance = " << setw(15) << min << " ohms";
cout << endl << "Maximum Resistance = " << setw(15) << max << " ohms";
cout << endl << endl;
}
void EnterResistance(CResistor & resObj, bool fnDisplay) // Function allows user to enter new values for CResistor data member
{
//Local variables to hold CResistor data copies
double nom, tol;
string name;
//Local variable for user data entry
int nomEdit = 0;
int tolEdit = 0;
int powerOfTen = 0;
//Valid EIA entry value check Boolean
bool validEIA = false;
//Local loop counter
int i = 0;
nom = resObj.getNominal(fnDisplay);
tol = resObj.getTolerance(fnDisplay);
name = resObj.getName(fnDisplay);
cout << endl << endl;
cout << "Resistor being edited: " << name << endl << endl;
do
{
cout << "Current resistance tolerance = " << tol * 100 << " %" << endl;
cout << "Valid tolerances are 1%, 2%, 5% or 10%" << endl << endl;
cout << "Enter 1, 2, 5 or 10: ";
cin >> tolEdit;
cout << endl;
if (tolEdit != 1 && tolEdit != 2 && tolEdit != 5 && tolEdit != 10)
{
cin.clear();
cin.ignore(255, ' ');
}
} while (tolEdit != 1 && tolEdit != 2 && tolEdit != 5 && tolEdit != 10);
cout << "Tolerance set to " << tolEdit << " %" << endl << endl;
resObj.setTolerance((double)tolEdit * 0.01, fnDisplay);
do
{
cout << "Current nomimal resistance = " << nom << " ohms" << endl;
if (tolEdit == 10)
{
cout << "Standard 10% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E12; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE12Values[i] << " ";
}
}
else if (tolEdit == 5)
{
cout << "Standard 5% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E24; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE24Values[i] << " ";
}
}
else if (tolEdit == 2)
{
cout << "Standard 2% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E48; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE48Values[i] << " ";
}
}
else
{
for (i = 0; i < E96; i++)
{
cout << "Standard 1% Resistance Values, First Three Digits" << endl << endl;
if (!(i % 10))
{
cout << endl;
}
cout << nominalE96Values[i] << " ";
}
}
cout << endl << endl << "Enter first three digits: ";
do
{
cin >> nomEdit;
if (cin.fail() == true)
{
cin.clear();
cin.ignore(255, ' ');
}
} while (cin.fail() == true);
cout << "You entered " << nomEdit << endl;
validEIA = false;
if (tolEdit == 10)
{
for (i = 0; i < E12; i++)
{
if (nomEdit == nominalE12Values[i])
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
else if (tolEdit == 5)
{
for (i = 0; i < E24; i++)
{
if (nomEdit == nominalE24Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
else if (tolEdit == 2)
{
for (i = 0; i < E48; i++)
{
if (nomEdit == nominalE48Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
else
{
for (i = 0; i < E96; i++)
{
if (nomEdit == nominalE96Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
if (validEIA == false)
{
cout << "Invalid EIA value entered, try again" << endl;
}
} while (validEIA == false);
do
{
cout << endl << "Enter a power of ten multiplier between -2 (0.01) and 3 (1000): ";
cin >> powerOfTen;
if (powerOfTen < -2 || powerOfTen > 3)
{
cin.clear();
cin.ignore(255, ' ');
}
cout << "You entered " << powerOfTen << endl;
}
while (powerOfTen < -2 || powerOfTen > 3);
resObj.setNominal((double)nomEdit * pow(10.0, (double)powerOfTen), fnDisplay);
Explanation / Answer
#include "Resistor.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//EIA Standard array element sizes per tolerance class
const int E12 = 12;
const int E24 = 24;
const int E48 = 48;
const int E96 = 96;
//EIA Standard Resistor Values
//E12 10% tolerance
//E24 5% tolerance
//E48 2% tolerance
//E96 1% tolerance
//10% standard values
const int nominalE12Values[E12] = { 100, 120, 150, 180, 220, 270, 330, 390, 470, 560, 680, 820 };
//5% standard values
const int nominalE24Values[E24] = { 100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910 };
//2% standard values
const int nominalE48Values[E48] = { 100, 105, 110, 115, 121, 127, 133, 140, 147, 154, 162, 169, 178, 187, 196, 205, 215, 226, 237, 249, 261, 274, 287, 301, 316, 332, 348, 365, 383, 402, 422, 442, 464, 487, 511, 536, 562, 590, 619, 649, 681, 715, 750, 787, 825, 866, 909, 953 };
//1% standard values
const int nominalE96Values[E96] = { 100, 102, 105, 107, 110, 113, 115, 118, 121, 124, 127, 130, 133, 137, 140, 143, 147, 150, 154, 158, 162, 165, 169, 174, 178, 182, 187, 191, 196, 200, 205, 210, 215, 221, 226, 232, 237, 243, 249, 255, 261, 267, 274, 280, 287, 294, 301, 309, 316, 324, 332, 340, 348, 357, 365, 374, 383, 392, 402, 412, 422, 432, 442, 453, 464, 475, 487, 499, 511, 523, 536, 549, 562, 576, 590, 604, 619, 634, 649, 665, 681, 698, 715, 732, 750, 768, 787, 806, 825, 845, 866, 887, 909, 931, 953, 976 };
void DisplayResistance(CResistor &, bool);
void EnterResistance(CResistor &, bool);
int main(void) //Control variable to display function names when called
{
cout << "LabWk2 by James Felts D01367113" << endl << endl;
bool fnDisp = false;
CResistor Res1(4700.0, 0.10, "Res1", fnDisp);
CResistor Res2(330.0, 0.10, "Res2", fnDisp);
CResistor Res3(10000.0, 0.10, "Res3", fnDisp);
DisplayResistance(Res1, fnDisp);
DisplayResistance(Res2, fnDisp);
DisplayResistance(Res3, fnDisp);
EnterResistance(Res1, fnDisp);
DisplayResistance(Res1, fnDisp);
cout << endl << endl << "Second run displaying all test messages" << endl; // Run a second time with test messages
fnDisp = true;
CResistor Res4(4700.0, 0.10, "Res1", fnDisp);
CResistor Res5(330.0, 0.10, "Res2", fnDisp);
CResistor Res6(10000.0, 0.10, "Res3", fnDisp);
DisplayResistance(Res4, fnDisp);
DisplayResistance(Res5, fnDisp);
DisplayResistance(Res6, fnDisp);
EnterResistance(Res4, fnDisp);
DisplayResistance(Res4, fnDisp);
system("pause");
}
void DisplayResistance(CResistor & resObj, bool fnDisplay) // Function displays all data member values of a CResistor object
{
//Local variables to hold CResistor data copies
double nom, tol, min, max;
nom = resObj.getNominal(fnDisplay);
tol = resObj.getTolerance(fnDisplay);
min = nom * (1.0 - tol);
max = nom * (1.0 + tol);
cout << endl << "Resistor object name is " << resObj.getName(fnDisplay) << endl;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(3);
cout << endl << "Nominal resistance value = " << setw(15) << nom << " ohms ";
cout << endl << "Tolerance = " << setw(15) << tol * 100 << " %";
cout << endl << "Minimum Resistance = " << setw(15) << min << " ohms";
cout << endl << "Maximum Resistance = " << setw(15) << max << " ohms";
cout << endl << endl;
}
void EnterResistance(CResistor & resObj, bool fnDisplay) // Function allows user to enter new values for CResistor data member
{
//Local variables to hold CResistor data copies
double nom, tol;
string name;
//Local variable for user data entry
int nomEdit = 0;
int tolEdit = 0;
int powerOfTen = 0;
//Valid EIA entry value check Boolean
bool validEIA = false;
//Local loop counter
int i = 0;
nom = resObj.getNominal(fnDisplay);
tol = resObj.getTolerance(fnDisplay);
name = resObj.getName(fnDisplay);
cout << endl << endl;
cout << "Resistor being edited: " << name << endl << endl;
do
{
cout << "Current resistance tolerance = " << tol * 100 << " %" << endl;
cout << "Valid tolerances are 1%, 2%, 5% or 10%" << endl << endl;
cout << "Enter 1, 2, 5 or 10: ";
cin >> tolEdit;
cout << endl;
if (tolEdit != 1 && tolEdit != 2 && tolEdit != 5 && tolEdit != 10)
{
cin.clear();
cin.ignore(255, ' ');
}
} while (tolEdit != 1 && tolEdit != 2 && tolEdit != 5 && tolEdit != 10);
cout << "Tolerance set to " << tolEdit << " %" << endl << endl;
resObj.setTolerance((double)tolEdit * 0.01, fnDisplay);
do
{
cout << "Current nomimal resistance = " << nom << " ohms" << endl;
if (tolEdit == 10)
{
cout << "Standard 10% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E12; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE12Values[i] << " ";
}
}
else if (tolEdit == 5)
{
cout << "Standard 5% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E24; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE24Values[i] << " ";
}
}
else if (tolEdit == 2)
{
cout << "Standard 2% Resistance Values, First Three Digits" << endl << endl;
for (i = 0; i < E48; i++)
{
if (!(i % 10))
{
cout << endl;
}
cout << nominalE48Values[i] << " ";
}
}
else
{
for (i = 0; i < E96; i++)
{
cout << "Standard 1% Resistance Values, First Three Digits" << endl << endl;
if (!(i % 10))
{
cout << endl;
}
cout << nominalE96Values[i] << " ";
}
}
cout << endl << endl << "Enter first three digits: ";
do
{
cin >> nomEdit;
if (cin.fail() == true)
{
cin.clear();
cin.ignore(255, ' ');
}
} while (cin.fail() == true);
cout << "You entered " << nomEdit << endl;
validEIA = false;
if (tolEdit == 10)
{
for (i = 0; i < E12; i++)
{
if (nomEdit == nominalE12Values[i])
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
else if (tolEdit == 5)
{
for (i = 0; i < E24; i++)
{
if (nomEdit == nominalE24Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
else if (tolEdit == 2)
{
for (i = 0; i < E48; i++)
{
if (nomEdit == nominalE48Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
else
{
for (i = 0; i < E96; i++)
{
if (nomEdit == nominalE96Values[i])
{
validEIA = true;
cout << "Valid EIA value entered" << endl;
}
}
}
if (validEIA == false)
{
cout << "Invalid EIA value entered, try again" << endl;
}
} while (validEIA == false);
do
{
cout << endl << "Enter a power of ten multiplier between -2 (0.01) and 3 (1000): ";
cin >> powerOfTen;
if (powerOfTen < -2 || powerOfTen > 3)
{
cin.clear();
cin.ignore(255, ' ');
}
cout << "You entered " << powerOfTen << endl;
}
while (powerOfTen < -2 || powerOfTen > 3);
resObj.setNominal((double)nomEdit * pow(10.0, (double)powerOfTen), fnDisplay);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.