Don't like ads? PRO users don't see any ads ;-)
Guest

COMP220 Week 5 iLab - Pointer Array's & Resistor Sorting

By: Atropos1337 on Jun 30th, 2012  |  syntax: C++  |  size: 5.82 KB  |  hits: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. 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.
  3. 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.
  4. */
  5.  
  6. [resistor.h]
  7.  
  8. class Resistor
  9. {
  10. public:
  11.         static int objCount;
  12.  
  13.         Resistor(void);//constructor
  14.         Resistor(double, double);
  15.         void setNominalResistance(double);//set nominal resistance
  16.         void setTolerance(double);//set tolerance
  17.         double getNominalResistance();//get nominal resistance
  18.         double getTolerance();//get tolerance
  19.         double getMinResTolerance();//get minimum tolerance
  20.         double getMaxResTolerance();//get maximum tolerance
  21.         ~Resistor(void);//destructor
  22.  
  23. private:
  24.         double nominalResistance;
  25.         double tolerance;
  26. };
  27.  
  28. [resistor.cpp]
  29.  
  30. #include "resistor.h"
  31. #include<iostream>
  32.  
  33. using namespace std;
  34.  
  35. Resistor::Resistor(void) //constructor
  36. {
  37. }
  38.  
  39. Resistor::Resistor(double nomRes, double tol)
  40. {
  41.         nominalResistance = nomRes;
  42.         tolerance = tol;
  43.         objCount = objCount + 1;
  44.         if(nomRes < 1000 || nomRes > 10000)
  45.         {
  46.                 objCount = objCount - 1;
  47.         }
  48. }
  49.  
  50. void Resistor::setNominalResistance(double nomRes)//set nominal resistance
  51. {
  52.         nominalResistance = nomRes;
  53. }
  54.  
  55. void Resistor::setTolerance(double tol) //set tolerance
  56. {
  57.         tolerance = tol;
  58. }
  59.  
  60. double Resistor::getNominalResistance() //get nominal resistance
  61. {
  62.         return nominalResistance;
  63. }
  64.  
  65. double Resistor::getTolerance()//get tolerance
  66. {
  67.         return tolerance;
  68. }
  69.  
  70. double Resistor::getMinResTolerance() //get minimum tolerance
  71. {
  72.         return nominalResistance - tolerance;
  73. }
  74.        
  75. double Resistor::getMaxResTolerance() //get maximum tolerance
  76. {
  77.         return nominalResistance + tolerance;
  78. }
  79.  
  80. Resistor::~Resistor(void) //destructor
  81. {
  82. }
  83.  
  84. [Main.cpp]
  85.  
  86. #include "resistor.h"
  87. #include<iostream>
  88.  
  89. using namespace std;
  90.  
  91. //Prototype
  92. void SortObj(Resistor **);
  93. void displayMenu(void);
  94. char getMenuSelection(void);
  95.  
  96. int Resistor::objCount = 0;
  97.  
  98.  
  99. void main()
  100. {
  101.         Resistor *ptrResistor[10];
  102.        
  103.         int i = 0;
  104.         char action;
  105.         double res, tol;
  106.        
  107.        
  108.         do
  109.         {
  110.                 displayMenu(); //displays the intitial menu
  111.                 action = getMenuSelection(); //gets the menu selection
  112.                 if(action == 'c' || action == 'C')
  113.                 {
  114.                                 if(Resistor::objCount == 10)
  115.                                 {
  116.                                         cout << "you have entered the maximum number of 10 resistors." << endl;
  117.                                 }
  118.                                 do
  119.                                 {
  120.                                         cout << "You must enter a resistance value between 1000 and 10000" << endl;
  121.                                         if(Resistor::objCount < 10)
  122.                                         {
  123.                                                 cout << "\nEnter R" << i + 1 << " Resistance:";
  124.                                                 cin >> res;
  125.                                                 cout << "Enter R" << i + 1 << " Tolerance:";
  126.                                                 cin >> tol;
  127.                                                 ptrResistor[i] = new Resistor(res, tol);
  128.                                         }
  129.                                 }
  130.                                 while(res < 1000 || res > 10000);
  131.                                 i++;
  132.                 }
  133.  
  134.                 else if(action == 's' || action == 'S')
  135.                 {
  136.                         if(Resistor::objCount < 10)
  137.                         {
  138.                                 cout << "You have not used the maximum of 10 resistors" << endl;
  139.                         }
  140.                         else
  141.                         SortObj(&ptrResistor[0]);
  142.                         for(int j = 0; j < Resistor::objCount; j++)
  143.                         {
  144.                                 cout << "R" << j + 1 << "\n";
  145.                                 cout << "Nominal Resistance:" << ptrResistor[j] -> getNominalResistance() << "\n";
  146.                                 cout << "Tolerance:" << ptrResistor[j] -> getTolerance() << "\n";
  147.                                 cout << "Max Resistance Tolerance:" << ptrResistor[j] -> getMaxResTolerance() << "\n";
  148.                                 cout << "Min Resistance Tolerance:" << ptrResistor[j] -> getMinResTolerance() << "\n\n";
  149.                         }
  150.                 }
  151.         }
  152.         while(action != 'q' || action != 'Q'); //loop until program has been quit
  153.         cin.ignore(2);
  154.         delete [] ptrResistor;
  155. }
  156.  
  157. void displayMenu()              //Displays the initial starting menu options
  158. {
  159.         cout << "==================================================================" << endl;
  160.         cout << "|****** Enter C if you would like to create another resistor ****|" << endl;
  161.         cout << "==================================================================" << endl;
  162.         cout << "==================================================================" << endl;
  163.         cout << "|*** Enter S if you have entered 10 resistors to sort results ***|" << endl;
  164.         cout << "==================================================================" << endl;  
  165.         cout << "==================================================================" << endl;
  166.         cout << "|*********************** Enter Q To quit ************************|" << endl;
  167.         cout << "==================================================================" << endl;  
  168. }
  169.  
  170. char getMenuSelection() //Gets the menu selection
  171. {
  172.         cout << "Enter your choice and press enter:";
  173.         char action;
  174.         cin >> action;
  175.  
  176.         switch(action)
  177.         {
  178.         case 'c':
  179.         case 'C':
  180.                 return action;
  181.                 break;
  182.         case 's':
  183.         case 'S':
  184.                 return action;
  185.                 break;
  186.         case 'q':
  187.         case 'Q':
  188.                         exit(0);
  189.                 break;
  190.         default:        //if menu selection is not valid let the user know
  191.                 cout << "\nInvalid selection: try again" << endl;
  192.                 displayMenu();
  193.                 getMenuSelection();
  194.                 break;
  195.  
  196.         }
  197. }
  198.  
  199. void SortObj(Resistor **R)
  200. {
  201.       int i, j, mark = 1;    // set marker to 1 for the first pass
  202.           Resistor *temp; //pointer to help swap pointers
  203.          
  204.       for(i = 1; (i <= 10) && mark; i++)
  205.      {
  206.                   mark = 0;
  207.           for (j=0; j < (9); j++)
  208.          {
  209.                          if (R[j + 1] -> getNominalResistance() < R[j] -> getNominalResistance())
  210.               {
  211.                     temp = R[j];             // swap elements
  212.                     R[j] = R[j + 1];
  213.                     R[j + 1] = temp;
  214.                     mark = 1;               // marks where the swap occurred.
  215.                }
  216.           }
  217.           }
  218.           delete temp;
  219.      return;  
  220. }