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