gpt4 book ai didi

c++ - 从堆栈对象中提取信息

转载 作者:行者123 更新时间:2023-11-28 04:05:21 32 4
gpt4 key购买 nike

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

为栈类实现一个打印函数

#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;
}

最佳答案

假设 pop() 返回该项目而不只是将其从堆栈中删除,然后按照以下方式进行操作

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

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

关于c++ - 从堆栈对象中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822899/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com