//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;
}
14/3
最新推荐文章于 2023-06-07 10:43:27 发布