14/3

//worker.h
#ifndef WORKER_H
#define WORKER_H


#include <string>
class Worker
{
private:
	std::string  fullname;
	long id;
public:
	Worker():fullname("no one"), id(0L){}
	Worker(const std :: string & s,long n):fullname(s),id(n){}
	~Worker(){}
	Worker(const Worker &wo)
	{
		fullname = wo.fullname;
		id = wo.id;
	}
	const Worker & operator=(const Worker & wo)
	{
		fullname = wo.fullname;
		id = wo.id;
		return *this;
	}
	void show()const 
	{
		std::cout<<" fullname: ;"<<fullname<<"  "<<" id: "<<id;
	}
};
#endif  
//queue.h
#ifndef QUEUE_H
#define QUEUE_H


template<class Type>

class QueueTP
{
private:
	enum{SIZE =10};
	int stacksize;
	Type* items;
	int top;
public:
	explicit QueueTP(int ss = SIZE);
	QueueTP(const QueueTP &st);
	~QueueTP(){delete []items;}
	bool isempty(){return top==0;}
	bool isfull(){ return top ==stacksize;}
	bool push(const Type & item);
	bool pop(Type & item);
	QueueTP & operator =(const QueueTP &st);
};
template <class Type>
QueueTP<Type>::QueueTP(int ss):stacksize(ss),top(0)
{
	items = new Type[stacksize];
}
template <class Type>
QueueTP<Type>::QueueTP(const QueueTP &st)
{
	stacksize= st.stacksize;
	top =st.top;
	items= new Type[stacksize];
	for(int i =0;i<top;i++)
	
		items[i] =st.items[i];
}
template <class Type>
bool QueueTP<Type>::push(const Type &item)
{
	if(top<stacksize)
	{
		items[top++]=item;
		return true;
	}
	else
		return false;
}
template<class Type>
bool QueueTP<Type>::pop(Type& item)
{
	if(top>0)
	{
		item =items[--top];
		return true;
	}
	else
		return false;
}
template <class Type>
QueueTP<Type>& QueueTP<Type>::operator=(const QueueTP<Type>& st)
{
	if(this ==&st)
	
		return *this;
	delete[] items;
	stacksize =st.stacksize;
	top = st.top;
	items = new Type[stacksize];
	for(int i=0; i<top; i++)
		item[i] = st.item[i];
	return *this;
}
#endif
//main.cpp
#include<iostream>
#include"queue.h"
#include"worker.h"
#include<ctime>
#include<cstdlib>

const int Num =3;



int  main()
{
	using namespace std;
	srand(time(0));
	cout<<"please enter queue size: ";
	int stacksize ;
	cin>>stacksize;
	QueueTP<const Worker *>st(stacksize);
	const Worker *in[Num] ={new Worker("zhong",3),new Worker("wang",4),new Worker("li",5)};
	in[0]->show();
	const Worker *out[Num];
	int processed =0;
	int nextin = 0;
	while (processed<Num)
	{	
		if (st.isempty())
			st.push(in[nextin++]);
			

		else if(st.isfull())
			st.pop(out[processed++]);
		
		else if(std::rand()%2&&nextin<Num)
		 st.push(in[nextin++]);
		else 
			st.pop(out[processed++]);
	}
	for (int i = 0;i<Num; i++)
		out[i]->show();
	
	cout<<"bye"<<endl;
	system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值