齐桡吧 关注:9贴子:1,329
  • 2回复贴,共1

哲学家进餐问题

只看楼主收藏回复

0


1楼2005-11-26 14:20回复
    • 222.43.108.*
    #include<iostream.h>
    #include<stdlib.h>
    #include<conio.h>

    #define LEFT (i-1+PhNum)%PhNum
    #define RIGHT i

    const int QueueNum=5;//the real Num of the Queue is QueueNum-1
    const int PhNum=5;
    const int ERROR=0;
    const int OK=1;
    const int QueuePre=-1;

    typedef int ElemType;

     //head define....................
    /*************************************************************/
     class Queue_c
     {
     ElemType Q[QueueNum];
     int front,rear;

     public:
     //basic information of the class Queue_c:
     Queue_c();

     int qFull();
     int qEmpty();
     int qEnQueue(ElemType e);
     ElemType qDeQueue();
     int qGetNumber();
     int qGetElem(int);
     int qShowQueue();

    //other functions:
    //ElemType qGetFrontElem(){if(qEmpty()){cout<<"ERROR!"<<endl;return ERROR;}return Q[front];}
    //int qGetFront(){if(qEmpty()){cout<<"ERROR!"<<endl;return ERROR;}return front;}
    //ElemType qGetRearElem(){if(qEmpty()){cout<<"ERROR!"<<endl;return ERROR;}return Q[(rear-1+QueueNum)%QueueNum];}
    //int qGetRear(){if(qEmpty()){cout<<"ERROR!"<<endl;return ERROR;}return (rear-1+QueueNum)%QueueNum;}
    //int qShowAll();
     };

    //==================Define of Class Queue_c=====================================
    //==============================================================================

    class ClopStick_c
    {
    int e;
    public:
     ClopStick_c(){e=1;}
     int cGetE(){return e;}
     void cSTE(){e=1;}
     void cCLE(){e=0;}
    };


    //==================Define of Class ClopStick_c=================================
    //==============================================================================
    class Philosopher_c
    {public:
     Queue_c wq;
     Queue_c eq;
     ClopStick_c cs[PhNum];
     public:
     int phWait(int);
     void phSignal(int);
     int phScan(int);
     int phWork();
     int phShowWq(){return wq.qShowQueue();}
    };

    //==================Define of Class Philosopher_c=====================================
    //==============================================================================
    /*******************************==main==****************************************************
    ********************************==proc==***************************************************/

    void main()
    { Philosopher_c ph;
     clrscr();
     ph.phWork();
    //cout<<"scan()="<<ph.phScan(10)<<endl;
    //cout<<ph.phWait(0)<<endl;
    //ph.phShowWq();
    //ph.wq.qEnQueue(2);
    //cout<<ph.phScan(2)<<endl;



    }

    /********************************==END==*****************************************************
    *********************************==main==***************************************************/

    /************************Class Queue_c**********************************/
    //================Queue_c================================
    Queue_c::Queue_c()
     {
     int i;
     for(i=0;i<QueueNum;i++)Q[i]=QueuePre;
     rear=0;
     front=0;
     }
    //=====================qFull===========================
     int Queue_c::qFull()
     {
     if((rear+1)%QueueNum==front)
     {//cout<<"Queue is full"<<endl;
     return OK;}
     else return ERROR;
     }
     //================qEmpty================================
     int Queue_c::qEmpty()
     {
     if(rear==front)
    


    2楼2005-11-28 00:59
    回复
      • 222.43.108.*
       { //cout<<"Queue is Empty"<<endl;
       return OK;
       } //endif
       else return ERROR;
       }
      //===============qEnQueue=================================
       int Queue_c::qEnQueue(ElemType e)
       {
       if(qFull())return ERROR;
       Q[rear]=e;
       rear=(rear+1)%QueueNum;
       return OK;
       }

       //=================qDeQueue===============================
       ElemType Queue_c::qDeQueue()
       {
       ElemType e;
       if(qEmpty())return ERROR;
       e=Q[front];
       Q[front]=QueuePre;
       front=(front+1)%QueueNum;
       return e;
       }


      //===================qGetNumber=============================
       int Queue_c::qGetNumber()
       {int i;
       i=((rear-front+QueueNum)%QueueNum);
       return i;
       }
      //==================qGetElem==============================
       ElemType Queue_c::qGetElem(int i)
       {
       if(qEmpty()){
       //cout<<"ERROR!"<<endl;
       return ERROR;}
       if(i>qGetNumber()-1)
       {
       cout<<"ERROR! there is no "<<i<<",or no "<<i<<" in Queue now"<<endl;
       return ERROR;
       }
       return Q[(front+i)%QueueNum];

       }

      //======================qShowQueue==========================
       int Queue_c::qShowQueue()
       {int i;
       if(qEmpty())return ERROR;

       for(i=front;i!=rear%QueueNum;i=(i+1)%QueueNum)
       {
       cout<<Q[i]<<" ";
       }//endfor

       cout<<endl;
       return OK;
       }

      //======================qShowAll=========================
      /*int Queue_c::qShowAll()
      {
      int i;
      for(i=0;i<QueueNum;i++)cout<<Q[i]<<" ";
      cout<<endl;
      return OK;
      }*/

      //========================================================
      /*******************END Class Queue_c**********************************/


      /************************Class Philosopher_c**********************************/
      //============================================================================
      //======================phWait=========================
      int Philosopher_c::phWait(int i)
       {
       if(cs[LEFT].cGetE()&&cs[RIGHT].cGetE())
       {
       cs[LEFT].cCLE();
       cs[RIGHT].cCLE();
       return OK;
       }//endif
       else if(phScan(i))wq.qEnQueue(i);
       return ERROR;


       }
      //======================phSignal=========================
       void Philosopher_c::phSignal(int i)
       {
       cs[LEFT].cSTE();
       cs[RIGHT].cSTE();
       }
      //======================phScan=========================
       int Philosopher_c::phScan(int t)
       {
       if(t<0&&t>=PhNum)
       {//cout<<"ERROR"<<endl;
       return ERROR;}
       for(int i=0;i<wq.qGetNumber();i++)
       if(wq.qGetElem(i)==t)return ERROR;
       return OK;
       }//if t is in the wq return 0;if t is not in the wq,return 1;
      //======================phWork=========================
       int Philosopher_c::phWork()
       {
       int i,t,j;
       char flag;
       while('n'!=flag)
       { cout<<"------------------philosopher start eating--------------"<<endl;
       for(i=0;i<PhNum;i++)
      {
       if(wq.qEmpty())t=random(200)%5;
       else t=wq.qDeQueue();

      if(phWait(t))
       { cout<<"[H] philosopher "<<t<<" is eating"<<endl;
       eq.qEnQueue(t);
       }//endif1

       if(random(100)%2)
       {phSignal(t=eq.qDeQueue());
       cout<<"[f] phlosopher "<<t<<" is full"<<endl;
       }//endif2
      // cout<<"wq:";wq.qShowQueue();
      // cout<<"eq:";eq.qShowQueue();

      }//endfor
       cout<<"------------------now they have a break-----------------"<<endl;
      cout<<"continue? y/n";
      cin>>flag;
      clrscr();
       }//endwhile

       }
      //==============================================================================
      /*********************END Class Philosopher_c**********************************/


      3楼2005-11-28 00:59
      回复