0

我对 C++ 很陌生。

我从作业中获得了这段代码,我不太了解它,但我必须让程序在最后提供一个选项,让用户回忆任何零件编号和型号年份/发动机编号。输入。

我不知道如何去做这项任务......也许每个节点都有某种 id 以便我能回忆起它?

还是使用数组或向量数据结构重写程序是我唯一的选择?

     #include <iostream>

     using namespace std;

     typedef unsigned long ULONG;
     typedef unsigned short USHORT;


     // **************** Part ************

     // Abstract base class of parts
     class Part
     {
     friend void showPart();

     public:
        Part():itsPartNumber(1) {}
        Part(ULONG PartNumber):itsPartNumber(PartNumber){}
        virtual ~Part(){};
        ULONG GetPartNumber() const { return itsPartNumber; }
        virtual void Display() const =0;  // must be overridden
     private:
        ULONG itsPartNumber;
     };

     // implementation of pure virtual function so that
     // derived classes can chain up
     void Part::Display() const
     {
         cout << "\nPart Number: " << itsPartNumber << endl;
     }
     // **************** Car Part ************

     class CarPart : public Part
     {
     friend void showPart();

     public:
        CarPart():itsModelYear(94){}
        CarPart(USHORT year, ULONG partNumber);
        virtual void Display() const
       {
          Part::Display(); cout << "Model Year: ";
          cout << itsModelYear << endl;
       }
     private:
        USHORT itsModelYear;
     };

     CarPart::CarPart(USHORT year, ULONG partNumber):
        itsModelYear(year),
        Part(partNumber)
     {}


     // **************** AirPlane Part ************

     class AirPlanePart : public Part
     {
     friend void showPart();

     public:
        AirPlanePart():itsEngineNumber(1){};
        AirPlanePart(USHORT EngineNumber, ULONG PartNumber);
        virtual void Display() const
       {
          Part::Display(); cout << "Engine No.: ";
          cout << itsEngineNumber << endl;
       }
     private:
        USHORT itsEngineNumber;
     };

     AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
        itsEngineNumber(EngineNumber),
        Part(PartNumber)
     {}

     // **************** Part Node ************
     class PartNode
     {
     public:
         PartNode (Part*);
         ~PartNode();
         void SetNext(PartNode * node) { itsNext = node; }
         PartNode * GetNext() const;
         Part * GetPart() const;
      private:
         Part *itsPart;
         PartNode * itsNext;
      };

      // PartNode Implementations...

      PartNode::PartNode(Part* pPart):
      itsPart(pPart),
      itsNext(0)
      {}

      PartNode::~PartNode()
      {
         delete itsPart;
         itsPart = 0;
         delete itsNext;
         itsNext = 0;
      }

      // Returns NULL if no next PartNode
      PartNode * PartNode::GetNext() const
      {
            return itsNext;
      }

      Part * PartNode::GetPart() const
      {
         if (itsPart)
            return itsPart;
         else
            return NULL; //error
      }

      // **************** Part List ************
      class PartsList
      {
      public:
         PartsList();
         ~PartsList();
         // needs copy constructor and operator equals!
         Part*      Find(ULONG & position, ULONG PartNumber)  const;
         ULONG      GetCount() const { return itsCount; }
         Part*      GetFirst() const;
         static     PartsList& GetGlobalPartsList()
       {
          return  GlobalPartsList;
       }
         void       Insert(Part *);
         void       Iterate(void (Part::*f)()const) const;
         Part*      operator[](ULONG) const;
      private:
         PartNode * pHead;
         ULONG itsCount;
         static PartsList GlobalPartsList;
      };

      PartsList PartsList::GlobalPartsList;

      // Implementations for Lists...

      PartsList::PartsList():
         pHead(0),
         itsCount(0)
         {}

      PartsList::~PartsList()
      {
         delete pHead;
      }

      Part*   PartsList::GetFirst() const
      {
         if (pHead)
            return pHead->GetPart();
         else
            return NULL;  // error catch here
      }

      Part *  PartsList::operator[](ULONG offSet) const
      {
         PartNode* pNode = pHead;

         if (!pHead)
            return NULL; // error catch here

         if (offSet > itsCount)
            return NULL; // error

         for (ULONG i=0;i<offSet; i++)
            pNode = pNode->GetNext();

        return   pNode->GetPart();
      }

      Part*   PartsList::Find(ULONG & position, ULONG PartNumber)  const
      {
         PartNode * pNode = 0;
         for (pNode = pHead, position = 0;
               pNode!=NULL;
               pNode = pNode->GetNext(), position++)
         {
            if (pNode->GetPart()->GetPartNumber() == PartNumber)
               break;
         }
         if (pNode == NULL)
            return NULL;
         else
            return pNode->GetPart();
      }

      void PartsList::Iterate(void (Part::*func)()const) const
      {
         if (!pHead)
            return;
         PartNode* pNode = pHead;
         do
            (pNode->GetPart()->*func)();
         while (pNode = pNode->GetNext());
      }

      void PartsList::Insert(Part* pPart)
      {
         PartNode * pNode = new PartNode(pPart);
         PartNode * pCurrent = pHead;
         PartNode * pNext = 0;

         ULONG New =  pPart->GetPartNumber();
         ULONG Next = 0;
         itsCount++;

         if (!pHead)
         {
            pHead = pNode;
            return;
         }

         // if this one is smaller than head
         // this one is the new head
         if (pHead->GetPart()->GetPartNumber() > New)
         {
            pNode->SetNext(pHead);
            pHead = pNode;
            return;
         }

         for (;;)
         {
            // if there is no next, append this new one
            if (!pCurrent->GetNext())
            {
               pCurrent->SetNext(pNode);
               return;
            }

            // if this goes after this one and before the next
            // then insert it here, otherwise get the next
            pNext = pCurrent->GetNext();
            Next = pNext->GetPart()->GetPartNumber();
            if (Next > New)
            {
               pCurrent->SetNext(pNode);
               pNode->SetNext(pNext);
               return;
            }
            pCurrent = pNext;
         }
      }

      int main()
      {
         PartsList pl = PartsList::GetGlobalPartsList();
         Part * pPart = 0;
         ULONG PartNumber;
         USHORT value;
         ULONG choice;

         while (1)
         {
            cout << "(0)Quit (1)Car (2)Plane: ";
            cin >> choice;

            if (!choice)
               break;

            cout << "New PartNumber?: ";
            cin >>  PartNumber;

            if (choice == 1)
            {
               cout << "Model Year?: ";
               cin >> value;
               pPart = new CarPart(value,PartNumber);
            }
            else
            {
               cout << "Engine Number?: ";
               cin >> value;
               pPart = new AirPlanePart(value,PartNumber);
            }

            pl.Insert(pPart);
         }
         void (Part::*pFunc)()const = &Part::Display;
         pl.Iterate(pFunc);

         cout << "\n\n\nThere are " << pl.GetCount() << " items in the list" << endl;


        return 0;
     }

我尝试在 PartsList 类中使用 Find()。Find() 是否获取零件编号并返回零件的地址?

我写这个是为了取消引用检索到的地址,但它给了我错误no match for 'operator<<' in 'std::cout << * show'

    int findnumber;
         ULONG position;
         cout << "Enter Partnumber" << endl;
         cin >> findnumber;
         Part* show = pl.Find(position, findnumber);
         cout << *show;

我做错了吗?D:请给我看……

4

2 回答 2

1

函数 Find 接受一个零件编号,但返回一个指向零件的指针,该指针与零件的地址不同(这将是一个引用,用 & 表示)。此外,Find 引用了一个名为 'position' 的变量,因此在调用 Find 函数后,为 'position' 传入的变量将包含该零件在链表中的位置的值。

您不能使用 << 运算符的原因是它没有为 Part 类提供。但是,从提供的源代码来看,目标似乎是让您了解多态性,而不是尝试使用 <<,而是在您找到的部分上调用 Display 函数。例如: -

Part* part = pl.Find(position, findnumber);
part->Display();

这将显示相​​关零件类型的文本,因此如果返回的零件是 CarPart,则将调用 CarPart 的 Display 函数,而如果该零件是 AirPlane 零件,则调用其 Display 函数。

如果您想使用流运算符 (<<),则需要重载 io 运算符,您可以在此处阅读更多信息。

于 2013-06-20T08:31:58.900 回答
0

该类PartsList已经有一个Find()方法,可用于根据零件编号检索任何零件。然后您可以调用该Display()部分的方法。

于 2013-06-20T04:57:55.737 回答