总结1:Stack::Stack( )new Item[1]
Stack::Stack(int n)
{
/*错误写法:
pitems = new Item[1];
pitems[0] = '\0';
size = 0;
top = 0;*/
pitems = new Item[n];
for (int i = 0; i < n; i++)
{
pitems[i] = 0;
}
top = 0;
size = n;
}
总结2: unsigned long 是int类型,不适用:strcpy_s((char)pitems, st.size + 1, (char)st.pitems)**
Stack::Stack(const Stack & st)
{
pitems = new Item[st.size +1];
//unsigned long 是int类型,不适用:strcpy_s((char*)pitems, st.size + 1, (char*)st.pitems);
for (int i = 0; i < st.size;i++)
{
pitems[i] = st.pitems[i];
}
size = st.size;
top = st.top;
}
总结3:
bool Stack::pop(Item & item)
{
//if (isempty())判断的不充分
if (top>0)
{
/*this->pitems[top] = item;
pitems[top] = pitems[top--];*///前缀递增,前缀递减和解除引用运算符的优先级相同,以右到左的方式进行结合
//pitems[top]=pitems[--top];
item = pitems[--top];
//size--;不能减少size
return true;
}
else
{
return false;
}
}
std::ostream & operator<<(std::ostream &os, Stack &s)
{
//os <<"s.size: " <<s.size <<"s.top: "<< s.top;
for (int i = 0; i < s.top; i++)
{
os << s.pitems[i] << std::endl;
}
return os;
}
最终代码
typedef unsigned long Item;
class Stack
{
public:
Stack(int n = MAX);
Stack(const Stack & st);
~Stack();
bool isempty()const;
bool isfull()const;
bool push(const Item & item);
bool pop(Item & item);
Stack & operator=(const Stack & st);
friend std::ostream & operator<<(std::ostream &os, Stack &s);
protected:
private:
enum {MAX =10};
Item*pitems;
int size;
int top;
};
#include <iostream>
#include "stack.h"
Stack::Stack(int n)
{
/*错误写法:
pitems = new Item[1];
pitems[0] = '\0';
size = 0;
top = 0;*/
pitems = new Item[n];
for (int i = 0; i < n; i++)
{
pitems[i] = 0;
}
top = 0;
size = n;
}
Stack::Stack(const Stack & st)
{
pitems = new Item[st.size +1];
//
//unsigned long 是int类型,不适用:strcpy_s((char*)pitems, st.size + 1, (char*)st.pitems);
for (int i = 0; i < st.size;i++)
{
pitems[i] = st.pitems[i];
}
size = st.size;
top = st.top;
}
Stack::~Stack()
{
delete[]pitems;
}
bool Stack::isempty()const
{
return top == 0;
}
bool Stack::isfull()const
{
return top == MAX;
}
bool Stack::push(const Item & item)
{
//if(isfull)
if (top<MAX)
{
//pitems = new Item[size++];
this->pitems[top++] = item;
return true;
}
else
{
return false;
}
}
bool Stack::pop(Item & item)
{
//if (isempty())
if (top>0)
{
/*this->pitems[top] = item;
pitems[top] = pitems[top--];*///前缀递增,前缀递减和解除引用运算符的优先级相同,以右到左的方式进行结合
//pitems[top]=pitems[--top];
item = pitems[--top];
//size--;
return true;
}
else
{
return false;
}
}
Stack & Stack::operator=(const Stack & st)
{
if (this == &st)
return *this;
delete[] pitems;
top = st.top;
size = st.size;
pitems = new Item[st.size];
for (int i = 0; i < st.size; i++)
{
pitems[i] = st.pitems[i];
}
return *this;
}
std::ostream & operator<<(std::ostream &os, Stack &s)
{
//os <<"s.size: " <<s.size <<"s.top: "<< s.top;
for (int i = 0; i < s.top; i++)
{
os << s.pitems[i] << std::endl;
}
return os;
}
#include <iostream>
#include <cctype>
#include "stack.h"
using namespace std;
void main()
{
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 << "So our stack is:\n" << st;
Stack st2;
st2 = st;
cout << "stack2 = stack is:\n" << st2;
cout << "Bye\n";
cout<<"hello.."<<endl;
system("pause");
return ;
}