📄 usestack.cpp
字号:
/*
4.请看下面程序清单10.10定义的Stack类的变量:
stack.h -- class declaration for the stack ADT
typedef unsigned long Item;
class Stack
{
private:
enum {MAX = 10}; //constant specific to class
Item * pitems; // holds stack items
int size; // number of elements in stack
int top; // index for top stack item
public:
Stack(int n = 10); // crates stack with n elements
Stack(const Stack & st);
~Stack();
bool isempty()const;
bool isfull()const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
Stack & operator = (const Stack & st);
};
正如私有成员表明的,这个类使用动态分配的数组堆栈项.请重新编写方法,以适应这
种新的表示法,并编写一个程序来演示所有的方法,包括复制构造函数和赋值操作符.
*/
// stacker.cpp -- testing the Stack class
#include <iostream>
#include <cctype>
#include "stack.h"
int main()
{
using namespace std;
Stack st;
char ch;
unsigned long po;
cout<<"Please enter A to add a purchase order.\n"
<<"P to process a po, or Q to quit.\n";
while( cin>>ch && toupper(ch) != 'Q')
{
while( cin.get() != '\n')
continue;
if ( !isalpha(ch) )
{
cout<<'\a';
continue;
}
switch(ch)
{
case 'A':
case 'a':
cout<<"Enter a PO number to add:";
cin>>po;
if( st.isfull() )
cout<<"stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p':
if( st.isempty() )
cout<<"stack already empty\n";
else
{
st.pop(po);
cout<<"PO #"<<po<<" popped\n";
}
break;
}
cout<<"Please enter A to add a purchase order.\n"
<<"P to process a po, or Q to quit.\n";
}
cout<<"st2 = st:\n";
Stack st2 = st;
while( !st2.isempty() )
{
cout<<"pop: ";
st2.pop(po);
cout<<po<<endl;
}
cout<<"Bye\n";
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -