从堆栈的对象中提取信息

因此,我有一个堆栈,用于存储清单类的对象。库存类存储有关每个实例序列号的信息和相关信息。当我从堆栈中弹出某些内容时,我想显示该项目的内容库存信息ex。序列号,批号和日期。

为堆栈类实现打印功能

#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;
}
a199497917 回答:从堆栈的对象中提取信息

假设pop()返回了该项目,而不仅仅是将其从堆栈中删除,那么类似

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

应该工作。否则,您需要使用返回顶部项目的函数,例如类似top()peek()之类的东西,然后将对象弹出。

本文链接:https://www.f2er.com/3115818.html

大家都在问