12.9-4

总结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 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值