#include<iostream.h>
#define QueueMax 10
#define ERROR 0
#define OK 1
typedef int ElemType;
//head define....................
//==================================================================
class Queue_c
{
ElemType Q[QueueMax];
ElemType Queue_c_temp;
int front,rear;
public:
Queue_c()
{
// for(i=0;i<QueueMax;i++)Q[i]=0;
front=rear=0;
}
~Queue_c(){}
//basic information of the class Queue_c
void ZeroQueue();
int QFull();
int QEmpty();
int EnQueue(ElemType e);
ElemType DeQueue();
int GetNumber();
void ShowAll();
};
//define of the class Queue_c...........................
//------------------------------------------------------------------
class Producer_c
{
ElemType goods;
public:
int produce(Queue_c buffur,Consumer_c consumer);
int produce(Queue_c buffur);
};
//define of class Producer_c
//------------------------------------------------------------------
class Consumer_c
{
ElemType goods;
public:
int consume(Queue_c buffur,Producer_c producer);
int consume(Queue_c buffur);
};
//difine of class Consumer_c
//==================================================================
void main()
{
Queue_c buffur;
Producer_c producer;
Consumer_c consumer;
//int judge=1;
//char ExitLoop='n';
buffur.ZeroQueue();
cout<<"===================="<<endl;
/* while('y'!=ExitLoop)
{ judge%=2;
if(1==judge)producer.produce(buffur,consumer);
if(0==judge)consumer.consume(buffur,producer);
cout<<"============="<<endl;
cout<<"=1 produce ="<<endl;
cout<<"=2 consume ="<<endl;
cout<<"=others exit="<<endl;
cout<<"============="<<endl;
cin>>judge;
if(1==judge)cout<<"you pressed 1,product"<<endl;
if(2==judge)cout<<"you pressed 2,consume"<<endl;
if(1!=judge&&2!=judge){ExitLoop='y';break;}
} */
producer.produce(buffur,consumer);
consumer.consume(buffur,producer);
/* buffur.QFull();
buffur.QEmpty();
buffur.EnQueue(100);
buffur.QEmpty();
buffur.ShowAll();
buffur.DeQueue();
buffur.QEmpty(); */
buffur.ShowAll();
cout<<buffur.GetNumber()<<endl;
}
/*********************************************************************/
void Queue_c::ZeroQueue()
{
int i;
for(i=0;i<QueueMax;i++)Q[i]=0;
}
int Queue_c::QFull()
{
if((rear+1)%QueueMax==front)
{cout<<"Queue is full"<<endl;
return OK;
}
else return ERROR;
}
//QFull...................
int Queue_c::QEmpty()
{
if(rear==front)
{
cout<<"Queue is Empty"<<endl;
return OK;
}
else return ERROR;
}
//QEmpty..........................
int Queue_c::EnQueue(ElemType e)
{
if(QFull())return ERROR;
Q[rear]=e;
rear=(rear+1)%QueueMax;
return OK;
}
//EnQueue.........................
ElemType Queue_c::DeQueue()
{
if(QEmpty())return ERROR;
Queue_c_temp=Q[front];
Q[front]=0;
front=(front+1)%QueueMax;
return OK;
}
//DeQueue.......................
int Queue_c::GetNumber()
{int i;
i=((rear-front+QueueMax)%QueueMax);
return i;
}
//GetNumber.......................
void Queue_c::ShowAll()
{int i;
for(i=0;i<QueueMax;i++)
cout<<Q[i]<<" ";
cout<<endl;
}
//ShowAll.........................
/*********************************************************************/
int Producer_c::produce(Queue_c buffur,Consumer_c consumer);
{
if(buffur.QFull())
{
cout<<"ERROR:you must consume a product"<<endl;
consumer.consume(buffur);
}
cout<<"Produce:please input a number"<<endl;
cin>>goods;
buffur.EnQueue(goods);
return OK;
}
//produce_A...........................
int Producer_c::produce(Queue_c buffur)
{
cout<<"Produce:please input a number"<<endl;
cin>>goods;
buffur.EnQueue(goods);
return OK;
}//produce_B..........................
//class Producer_c
//******************************************************************
int Consumer_c::consume(Queue_c buffur,Producer_c producer)
{
if(buffur.QEmpty())
{
cout<<"ERROR:Need produce"<<endl;
producer.produce(buffur);
}
goods=buffur.DeQueue();
cout<<"Consume:you have killed"<<goods<<endl;
return OK;
}//consume_A..........................
int Consumer_c::consume(Queue_c buffur)
{
goods=buffur.DeQueue();
cout<<"Consume"<<goods<<"has been killed"<<endl;
return OK;
}
#define QueueMax 10
#define ERROR 0
#define OK 1
typedef int ElemType;
//head define....................
//==================================================================
class Queue_c
{
ElemType Q[QueueMax];
ElemType Queue_c_temp;
int front,rear;
public:
Queue_c()
{
// for(i=0;i<QueueMax;i++)Q[i]=0;
front=rear=0;
}
~Queue_c(){}
//basic information of the class Queue_c
void ZeroQueue();
int QFull();
int QEmpty();
int EnQueue(ElemType e);
ElemType DeQueue();
int GetNumber();
void ShowAll();
};
//define of the class Queue_c...........................
//------------------------------------------------------------------
class Producer_c
{
ElemType goods;
public:
int produce(Queue_c buffur,Consumer_c consumer);
int produce(Queue_c buffur);
};
//define of class Producer_c
//------------------------------------------------------------------
class Consumer_c
{
ElemType goods;
public:
int consume(Queue_c buffur,Producer_c producer);
int consume(Queue_c buffur);
};
//difine of class Consumer_c
//==================================================================
void main()
{
Queue_c buffur;
Producer_c producer;
Consumer_c consumer;
//int judge=1;
//char ExitLoop='n';
buffur.ZeroQueue();
cout<<"===================="<<endl;
/* while('y'!=ExitLoop)
{ judge%=2;
if(1==judge)producer.produce(buffur,consumer);
if(0==judge)consumer.consume(buffur,producer);
cout<<"============="<<endl;
cout<<"=1 produce ="<<endl;
cout<<"=2 consume ="<<endl;
cout<<"=others exit="<<endl;
cout<<"============="<<endl;
cin>>judge;
if(1==judge)cout<<"you pressed 1,product"<<endl;
if(2==judge)cout<<"you pressed 2,consume"<<endl;
if(1!=judge&&2!=judge){ExitLoop='y';break;}
} */
producer.produce(buffur,consumer);
consumer.consume(buffur,producer);
/* buffur.QFull();
buffur.QEmpty();
buffur.EnQueue(100);
buffur.QEmpty();
buffur.ShowAll();
buffur.DeQueue();
buffur.QEmpty(); */
buffur.ShowAll();
cout<<buffur.GetNumber()<<endl;
}
/*********************************************************************/
void Queue_c::ZeroQueue()
{
int i;
for(i=0;i<QueueMax;i++)Q[i]=0;
}
int Queue_c::QFull()
{
if((rear+1)%QueueMax==front)
{cout<<"Queue is full"<<endl;
return OK;
}
else return ERROR;
}
//QFull...................
int Queue_c::QEmpty()
{
if(rear==front)
{
cout<<"Queue is Empty"<<endl;
return OK;
}
else return ERROR;
}
//QEmpty..........................
int Queue_c::EnQueue(ElemType e)
{
if(QFull())return ERROR;
Q[rear]=e;
rear=(rear+1)%QueueMax;
return OK;
}
//EnQueue.........................
ElemType Queue_c::DeQueue()
{
if(QEmpty())return ERROR;
Queue_c_temp=Q[front];
Q[front]=0;
front=(front+1)%QueueMax;
return OK;
}
//DeQueue.......................
int Queue_c::GetNumber()
{int i;
i=((rear-front+QueueMax)%QueueMax);
return i;
}
//GetNumber.......................
void Queue_c::ShowAll()
{int i;
for(i=0;i<QueueMax;i++)
cout<<Q[i]<<" ";
cout<<endl;
}
//ShowAll.........................
/*********************************************************************/
int Producer_c::produce(Queue_c buffur,Consumer_c consumer);
{
if(buffur.QFull())
{
cout<<"ERROR:you must consume a product"<<endl;
consumer.consume(buffur);
}
cout<<"Produce:please input a number"<<endl;
cin>>goods;
buffur.EnQueue(goods);
return OK;
}
//produce_A...........................
int Producer_c::produce(Queue_c buffur)
{
cout<<"Produce:please input a number"<<endl;
cin>>goods;
buffur.EnQueue(goods);
return OK;
}//produce_B..........................
//class Producer_c
//******************************************************************
int Consumer_c::consume(Queue_c buffur,Producer_c producer)
{
if(buffur.QEmpty())
{
cout<<"ERROR:Need produce"<<endl;
producer.produce(buffur);
}
goods=buffur.DeQueue();
cout<<"Consume:you have killed"<<goods<<endl;
return OK;
}//consume_A..........................
int Consumer_c::consume(Queue_c buffur)
{
goods=buffur.DeQueue();
cout<<"Consume"<<goods<<"has been killed"<<endl;
return OK;
}