Advertisement
GbossMega9

Dtallmf8

Feb 25th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. using namespace std;
  2.  
  3. # include <iostream>
  4. # include <stdio.h>
  5. # include <14_5hSTATIC2.hpp>
  6.  
  7. ULONG&Part::Display(ULONG*)
  8. {
  9.     cout << "Display function called.\n\n";
  10.     cout << "\nPart Number: " << &Part::itsPartNumber << endl;
  11. }
  12.  
  13. CarPart::CarPart(USHORT year, ULONG PartNumber):
  14.     itsModelYear(year),
  15.     Part(PartNumber)
  16.     { cout << "Derived class copy constructor called.\n" ;
  17.       cout << "Derived class PartNumber = " << PartNumber << "\n\n" ;  }
  18.    
  19. AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
  20.     itsEngineNumber(EngineNumber),
  21.     Part(PartNumber)
  22.     {}
  23.         // PartNode Implementations...
  24.        
  25.         PartNode::PartNode(Part* pPart):
  26.         itsPart(pPart),
  27.         itsNext(0)
  28.         {}
  29.        
  30.         PartNode::~PartNode()
  31.         {
  32.             delete itsPart;
  33.             itsPart = 0;
  34.             delete itsNext;
  35.             itsNext = 0;
  36.         }
  37.        
  38.         // Returns NULL if no next PartNode
  39.         PartNode * PartNode::GetNext() const
  40.         {
  41.             return itsNext;
  42.         }
  43.            
  44.        
  45.         Part * PartNode::GetPart() const
  46.         {
  47.             if (itsPart)
  48.                 return itsPart;
  49.             else
  50.                 return NULL;    //error
  51.             }
  52.            
  53.            
  54.                
  55.         PartsList PartsList::GlobalPartsList;
  56.        
  57.        
  58.         // Implementation for Lists....
  59.        
  60.         PartsList::PartsList():
  61.         pHead(0),
  62.         itsCount(0)
  63.         {}
  64.        
  65.        
  66.         PartsList::~PartsList()
  67.         {
  68.             delete pHead;
  69.         }
  70.        
  71.         Part* PartsList::GetFirst() const
  72.         {
  73.             if (pHead)
  74.                 return pHead->GetPart();
  75.             else
  76.                 return NULL; // error catch here
  77.         }
  78.        
  79.         Part * PartsList::operator[](ULONG offset) const
  80.         {
  81.             PartNode* pNode = pHead;
  82.            
  83.             if(!pHead)
  84.                 return NULL; // error catch here
  85.            
  86.             if (offset > itsCount)
  87.                 return NULL; //error
  88.            
  89.             for (ULONG i=0;i<offset; i++)
  90.                 pNode->GetNext();
  91.            
  92.             return pNode->GetPart();
  93.         }
  94.        
  95.         Part* PartsList::Find(ULONG & position, ULONG PartNumber) const
  96.         {
  97.             PartNode * pNode = 0;
  98.         for (pNode = pHead, position = 0;
  99.             pNode!=NULL;
  100.             pNode = pNode->GetNext(),position++)
  101.         {
  102.         if (pNode->GetPart() ->GetPartNumber() == PartNumber)
  103.             break;
  104.             }
  105.         if (pNode == NULL)
  106.             return NULL;
  107.         else
  108.             return pNode ->GetPart();
  109.         }
  110.        
  111.         void PartsList::Iterate(ULONG& (*)(ULONG*))
  112.         {
  113.             if (!pHead)
  114.                 return;
  115.             PartNode* pNode = pHead;
  116.             do
  117.                 (pNode->GetPart());
  118.                
  119.  
  120.             while (pNode = pNode->GetNext());
  121.         }
  122.        
  123.         void PartsList::Insert(Part *pPart)
  124.         {
  125.             PartNode * pNode = new PartNode(pPart);
  126.             PartNode * pCurrent = pHead;
  127.             PartNode * pNext = 0;
  128.            
  129.             ULONG New = pPart->GetPartNumber();
  130.             ULONG Next = 0;
  131.             itsCount++;
  132.            
  133.             if (!pHead)
  134.             {
  135.                 pHead = pNode;
  136.                     return;
  137.             }
  138.            
  139.             // if this one is smaller than head
  140.             // this one is the new head
  141.             if (pHead->GetPart() ->GetPartNumber() > New)
  142.             {
  143.                 pNode->SetNext(pHead);
  144.                 pHead = pNode;
  145.                 return;
  146.             }
  147.            
  148.  
  149. for (;;)
  150. {
  151. // if there is no next, append this new one
  152. if (!pCurrent->GetNext())
  153. {
  154.     pCurrent->SetNext(pNode);
  155.     return;
  156.     }
  157.    
  158.     // if this goes after this one and before the next
  159.     // then insert it here, otherwise get the next
  160.     pNext = pCurrent->GetNext();
  161.     Next = pNext->GetPart()->GetPartNumber();
  162.     if (Next > New)
  163.     {
  164.         pCurrent->SetNext(pNode);
  165.         pNode->SetNext(pNext);
  166.         return;
  167.         }
  168.         pCurrent = pNext;
  169.         }
  170. }
  171.        
  172.        
  173.        
  174.             void PartsCatalog::Insert(Part * newPart)
  175.             {
  176.                 ULONG
  177.                 PartNumber = newPart->GetPartNumber();
  178.                 ULONG offset;
  179.                
  180.                 if (!thePartsList.Find(offset, PartNumber))
  181.                 thePartsList.Insert(newPart);
  182.                 else
  183.         {
  184.             cout << PartNumber << " was the ";
  185.             switch (offset)
  186.             {
  187.                 case 0:  cout << "first "; break;
  188.                 case 1:  cout << "second "; break;
  189.                 case 2:  cout << "third "; break;
  190.                 default: cout << offset+1 << "th ";
  191.             }
  192.             cout << "entry. Rejected!\n";
  193.         }
  194.     }
  195.        
  196.         ULONG PartsCatalog::Exists(ULONG PartNumber)
  197.         {
  198.             ULONG offset;
  199.             thePartsList.Find(offset,PartNumber);
  200.             return offset;
  201.         }
  202.        
  203.         Part * PartsCatalog::Get(ULONG PartNumber)
  204.         {
  205.             ULONG offset;
  206.             Part * thePart = thePartsList.Find(offset, PartNumber);
  207.             return thePart;
  208.         }
  209.        
  210.         void startof()
  211.         {
  212.             PartsCatalog pc;
  213.             Part * pPart = 0;
  214.             ULONG PartNumber;
  215.             USHORT value;
  216.             ULONG choice;
  217.            
  218.             while (1)
  219.             {
  220.                 cout << "(0)Quit (1)Car (2)Plane: ";
  221.                 //cout << "\n\n Base class current Part No: " << &Part::GetPartNumber << endl;
  222.                 pPart->print();
  223.                 cout << "\n";
  224.                 cin >> choice;
  225.                
  226.                 if (!choice)
  227.                 break;
  228.                
  229.                 cout << "New part Number?: ";
  230.                 cin >> PartNumber;
  231.                
  232.                 if (choice == 1)
  233.                 {
  234.                     cout << "ModelYear?:";
  235.                     cin >> value;
  236.                     cout << "\n";
  237.                     pPart = new CarPart(value,PartNumber);
  238.                 }
  239.                 else
  240.                 {
  241.             cout << "Engine Number?: ";
  242.             cin >> value;
  243.             pPart = new AirPlanePart(value,PartNumber);
  244.         }
  245.         pc.Insert(pPart);
  246.     }
  247.         pc.ShowAll();
  248.     }
  249.    
  250.     int main()
  251.     {
  252.         startof();
  253.         return 0;
  254.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement