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


1楼2005-11-26 01:45回复
    #include<iostream.h>
    #include<stdlib.h>
    #include <conio.h>
    const int QueueNum=10;
    const int QueuePre=-1;
    const int ERROR=0;
    const int OK=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();

     int qDec()
     {
     if(qEmpty()){cout<<"ERROR!"<<endl;return ERROR;}//endif
     Q[front]--;
     return OK;
     }
     int qXChange(int);
     };
    //==================Define of Class Queue_c=====================================
    //==============================================================================
     class Work_c
     {
     public:
     Queue_c buf;
     int wCPU();
     ElemType wScan(int &t);
     int wMakeP();
     };
    //==================Define of Class Work_c=====================================
    //==============================================================================

    /*******************************==main==****************************************************
    ********************************==proc==***************************************************/

     void main()
     {
     Work_c w;
     int i;
     w.buf.qEnQueue(5);
     w.buf.qEnQueue(3);
     w.buf.qEnQueue(1);
     clrscr();
     char flag='y';

     for(i=0;i<10;i++)
     {
     //w.buf.qShowQueue();
     w.wMakeP();
     w.wCPU();
     w.buf.qShowQueue();
     if(9==i)
     {
     cout<<"Are you want to continue?y/n"<<endl;
     cin>>flag;
     if('n'==flag)break;else i=0;
    clrscr();
     }//endif
     }//endfor

     }

    /********************************==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)
     { 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;
    


    2楼2005-11-26 10:17
    回复
       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;
      }*/
       int Queue_c::qXChange(int i)
       {
       ElemType t;
       if(1==qGetNumber())return OK;
       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;
      }//endlf
       t=Q[front];
       Q[front]=Q[(i+front)%QueueNum];
       Q[(i+front)%QueueNum]=t;
       return OK;
       }
      //========================================================
      /************************End Of Class Queue_c**********************************/


      /************************Class Work_c******************************************/
      //======================wCPU=========================
       int Work_c::wCPU()
      { int t;
      if(wScan(t))buf.qXChange(t);
      buf.qDec();
      if(buf.qGetFrontElem()<=0)buf.qDeQueue();

      return OK;
      }
      //======================wScan=========================
      ElemType Work_c::wScan(int &t)
      {
       int i,m=12;
       ElemType temp;
       for(i=0;i<buf.qGetNumber();i++)
       {
       temp=buf.qGetElem(i);
       if(temp<m){m=temp;t=i;}//endif
       }//endfor
       if(m<=0||m>buf.qGetFrontElem())return ERROR;//如果是负数的话在这里就被拦下来了,所以,传出出去的m不可能是负数
       return m;
      }
      //======================wMakeP=========================
      int Work_c::wMakeP()
      {
       int t;
       t=random(200)%2;
       if(t){buf.qEnQueue(random(10)+1);return OK;}
       return ERROR;
      }
      //========================================================
      /**************************End of Class Work_c*********************************/


      3楼2005-11-26 10:17
      回复