GbossMega9

cppf2

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