이 코드 마지막에 스택에 남은 정보들 다 출력하려면 어떻게하죠???
#include <iostream>
#include <string>
using namespace std;
template <class T>
class DynStack
{
private:
struct StackNode
{
T value;
StackNode* next = NULL;
};
StackNode* top;
public:
DynStack() { top = NULL; }
void push(T);
void pop(T &);
bool isEmpty();
};
// Member function push pushes the argument onto
// the stack.
template <class T>
void DynStack<T>::push(T num)
{
StackNode *newNode;
// Allocate a new node & store Num
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}
// Member function pop pops the value at the top
// of the stack off, and copies it into the variable
// passed as an argument.
template <class T>
void DynStack<T>::pop(T &num)
{
StackNode* temp;
if(isEmpty())
{
cout << "The stack is empty.\n";
return;
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
// member funciton isEmpty returns true if the stack
// is empty, or false otherwise.
template <class T>
bool DynStack<T>::isEmpty()
{
if (!top) return true;
else return false;
}
class Inventoryltem
{
private:
long serialNum; // Serial number
string manufactDate; // Manufacture date
int lotNum; // Lot number
public:
// Default constructor
Inventoryltem()
{
serialNum = 0;
manufactDate = "";
lotNum = 0;
}
// Constructor
Inventoryltem(long s, string m, int lot)
{
serialNum = s; manufactDate = m; lotNum = lot;
}
void setSerialNum(long s) { serialNum = s; }
void setManufactDate(string m) { manufactDate = m; }
void setLotNum(int lot) { lotNum = lot; }
long getSerialNum() { return serialNum; }
string getManufactDate() { return manufactDate; }
int getLotNum() const { return lotNum; }
};
int main()
{
DynStack<Inventoryltem> stack;// create stack Inventoryltem item;
Inventoryltem item; // create inventory item object
int ch;// Menu choice
long serial;// Serial number
string mDate;// Manufacture date
int lnum; //lot number
do
{
cout << "1. add a part.\n";
cout << "2. remove a part.\n";
cout << "3. Quit.\n\n";
cout << "choose one of them(1,2,3): ";
cin >> ch; // Validate choice
while (ch < 1 || ch > 3)
{
cout << "invalid number. Enter one of 1, 2, 3: ";
cin >> ch;
};
// Act on the user's choice.
switch (ch)
{
case 1:
cout << "\nadding a new part...\n";
cout << "Enter the serial number : ";
cin >> serial;
item.setSerialNum(serial);
cout << "Enter the manufacture date : ";
cin >> mDate;
item.setManufactDate(mDate);
cout << "Enter the lot number: ";
cin >> lnum;
cout << endl;
item.setLotNum(lnum);
stack.push(item);
break;
case 2:
if (stack.isEmpty())
{
cout << "it's empty.\n\n\n";
break;
}
else stack.pop(item);
cout << "\nremoving a part...\n";
cout << "\nThis part was removed.\n";
cout << "\tSerial number: " << item.getSerialNum() << endl;
cout << "\tManufacture date: " << item.getManufactDate() << endl;
cout << "\tlot number: " << item.getLotNum() << endl;
cout << endl;
break;
case 3:
cout << "\nRemaining parts : ";
break;
}
} while (ch != 3);
return 0;
}
답변 4
1
안녕하세요.
스택을 사용하셨으니 모두 pop해서 출력하면 될 듯 합니다.
main 함수의 switch문 중 case 2 코드처럼요.
저도 학생이지만 답변이 될 것 같아 말씀드렸습니다.
0
님 말대로 case3를 case2 처럼 바꿨더니 되네요 감사합니다!!!!
case 3:
cout << "\nRemaining parts : ";
do
{
stack.pop(item);
cout << "\tSerial number: " << item.getSerialNum() << endl;
cout << "\tManufacture date: " << item.getManufactDate() << endl;
cout << "\tlot number: " << item.getLotNum() << endl;
cout << endl;
} while (!stack.isEmpty());
break;
0
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
466
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
499
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
679
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
549
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
443
1
Digit 뒤에 reference를 사용하는 이유
0
510
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
323
1
dat파일이...
0
539
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
644
1
복사 생성자 관련 질문이 있습니다.
0
454
1
수업 중 궁금한점이 있습니다.
1
390
1
라이브러리자체가 이해가 되지 않습니다.
0
561
1
마지막 예제 질문
0
302
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
375
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
411
1
friend함수 관련 질문이 있습니다.
0
312
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
447
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
448
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
445
2
const Something &st에서 const를 빼면 안되나요?
0
300
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
493
1





