【问题标题】:Extracting the information from an object of a stack从堆栈的对象中提取信息
【发布时间】:2020-03-08 09:53:13
【问题描述】:

所以我有一个堆栈来存储库存类的对象。库存类存储有关每个实例序列号和相关信息的信息。当我从堆栈中弹出一些东西时,我想显示该项目库存信息前的内容。序列号、批号和日期。

实现堆栈类的打印功能

#include <iostream>
#include "DyIntStack.h"
#include "Inventory.h"
using namespace std;
int main()
{
    /*
    DyIntStack<int> ex;
    ex.push(5);
    ex.push(2);
    ex.push(7);
    ex.push(4);
    ex.printStack();

*/
    DyIntStack<Inventory> InvStack;


    bool stop = false;
    while(stop == false)
    {
        char input;
        cout << "A - add a part to the inventory" << endl;
        cout << "T - take a part to the inventory" << endl;
        cin >> input;
        if(input == 'A')
        {
            int serial;
            int lot;
            string date;
            Inventory item;
            cout << "Enter Serial #: ";
            cin >> serial;
            cout << "Enter Manufacturing Date:";
            cin >> date;
            cout << "Enter Lot #: ";
            cin >> lot;
            item.setSerial(serial);
            item.setManDate(date);
            item.setLot(lot);
            InvStack.push(item);
        }
        else if(input == 'T')
        {
            InvStack.pop();
            //print info of popped object
        }

        }


         return 0;

    }
#ifndef DYINTSTACK_H
#define DYINTSTACK_H
#include <iostream>
#include "Inventory.h"
template <class T>
class DyIntStack
{
    private:
    struct StackNode
    {
        T value;
        StackNode *next;
    };
    StackNode *top;
public:
    DyIntStack()
    {top=nullptr;}
    ~DyIntStack();
    void push(T);
    void pop(T &);
    bool isEmpty();
    void printStack();
};
template <class T>
DyIntStack<T>::~DyIntStack()
{
    StackNode *NodePtr = nullptr;
    StackNode *nextNode = nullptr;
    NodePtr = top;
    while(NodePtr != nullptr)
    {
        nextNode = NodePtr->next;
        delete NodePtr;
        NodePtr = nextNode;
    }
}
template <class T>
void DyIntStack<T>::push(T num)
{
    StackNode *newNode = nullptr;
    newNode = new StackNode;
    newNode->value = num;
    if(isEmpty())
    {
        top = newNode;
        newNode->next = nullptr;
    }
    else
    {
            newNode->next = top;
            top = newNode;
    }
}
template <class T>
void DyIntStack<T>::pop(T &val)
{
    StackNode *temp = nullptr;
    if(isEmpty())
    {
        std::cout << "Stack is empty!\n";
    }
    else
        {
            temp->value = val;
            temp = top->next;
            delete top;
            top = temp;
        }
}
template <class T>
bool DyIntStack<T>::isEmpty()
{
    bool status;
    if(!top)
    {
        status = true;
    }
    else
        status = false;
    return status;
}
template <class T>
void DyIntStack<T>::printStack()
{
    StackNode *iter = nullptr;
    iter = top;
    while(iter != nullptr)
    {
        std::cout << iter->value << " ";
        iter = iter->next;
    }
}

#endif // DYINTSTACK_H


#ifndef INVENTORY_H
#define INVENTORY_H
#include <iostream>

class Inventory
{
    private:
        int serialNum;
        std::string manufactDate;
        int lotNum;
    public:
        Inventory();
        void setSerial(int num);
        void setManDate(std::string date);
        void setLot(int lot);
        int getSerial();
        std::string getManDate();
        int getLotNum();
        void printStuff();

};

#endif // INVENTORY_H

#include "Inventory.h"

Inventory::Inventory()
{
    serialNum = 0;
    manufactDate = "";
    lotNum = 0;
}
void Inventory::setSerial(int num)
{
    serialNum = num;
}
void Inventory::setManDate(std::string date)
{
    manufactDate = date;
}
void Inventory::setLot(int lot)
{
    lotNum = lot;
}
int Inventory::getSerial()
{
    return serialNum;
}
std::string Inventory::getManDate()
{
    return manufactDate;
}
int Inventory::getLotNum()
{
    return lotNum;
}
void Inventory::printStuff()
{
    std::cout << "Serial #: " << serialNum <<  std::endl;
    std::cout << "Manf Date:" << manufactDate <<  std::endl;
    std::cout << "Lot #: " << lotNum <<  std::endl;
}

【问题讨论】:

  • 你的问题是什么?
  • auto item { InvStack.pop() }; std::cout &lt;&lt; item.serial() &lt;&lt; "\n" &lt;&lt; item.lot() &lt;&lt; "\n" &lt;&lt; item.date() &lt;&lt; "\n"; 怎么样?这当然假设 pop() 实际上返回了该项目。
  • 我们需要查看DyIntStack.h,因为我们不知道pop 函数在内部做什么。 pop 是返回顶部元素还是仅移除顶部元素?
  • 这个无法回答,因为我们不知道DyIntStack 是什么,也不知道Inventory 是什么!无论如何 - InvStack.pop(); 是错误的。也许应该是auto item = InvStack.pop(); 或者auto item = InvStack.top(); InvStack.pop();
  • 我将如何实现它以返回堆栈中的对象?

标签: c++ class object stack


【解决方案1】:

假设 pop() 返回项目并且不只是将其从堆栈中删除,那么类似于

else if(input == 'T')
{
    auto item { InvStack.pop() };
    std::cout
        << item.serial() << "\n"
        << item.lot() << "\n"
        << item.date() << "\n";
}

应该可以。否则,您需要使用返回顶部项目的函数,可能是 top()peek() 或类似的东西,然后将对象弹出。

【讨论】:

  • 我更新了代码以画出更好的画面。为了让它返回对象,我尝试了很多不同的方法,但我完全可以弄清楚。
  • 您可以更改 DyIntStack.h 吗?因为要么 pop() 必须返回一些东西,要么 top 需要公开。
  • 是的,我可以为所欲为。
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 2012-09-11
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多