时间片轮转算法(C++版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeiostream.h#includestdlib.h#includeconio.hconstintQueueNum=10;constintQueuePre=-1;constintERROR=0;constintOK=1;constintTimePiece=1;typedefintElemType;//headdefine..................../*************************************************************/classQueue_c{ElemTypeQ[QueueNum];intfront,rear;public://basicinformationoftheclassQueue_c:Queue_c();intqFull();intqEmpty();intqEnQueue(ElemTypee);ElemTypeqDeQueue();intqGetNumber();intqGetElem(int);intqShowQueue();//otherfunctions:ElemTypeqGetFrontElem(){if(qEmpty()){coutERROR!endl;returnERROR;}returnQ[front];}//intqGetFront(){if(qEmpty()){coutERROR!endl;returnERROR;}returnfront;}//ElemTypeqGetRearElem(){if(qEmpty()){coutERROR!endl;returnERROR;}returnQ[(rear-1+QueueNum)%QueueNum];}//intqGetRear(){if(qEmpty()){coutERROR!endl;returnERROR;}return(rear-1+QueueNum)%QueueNum;}//intqShowAll();intqDec(){if(qEmpty()){coutERROR!endl;returnERROR;}//endifQ[front]-=TimePiece;returnOK;}//intqXChange(int);};//==================DefineofClassQueue_c=====================================//==============================================================================classWork_c{Queue_cbuf;public:intwCPU();//ElemTypewScan(int&t);intwMakeP();intwShowBUF(){returnbuf.qShowQueue();}intwInBUF(inti){returnbuf.qEnQueue(i);}};//==================DefineofClassWork_c=====================================//==============================================================================/*******************************==main==************************************************************************************==proc==***************************************************/voidmain(){Work_cw;inti;//w.wInBUF(5);//w.wInBUF(3);//w.wInBUF(1);clrscr();charflag='y';for(i=0;i10;i++){//w.buf.qShowQueue();w.wMakeP();w.wCPU();w.wShowBUF();if(9==i){coutAreyouwanttocontinue?y/nendl;cinflag;if('n'==flag)break;elsei=0;clrscr();}//endif}//endfor}/********************************==END==**************************************************************************************==main==***************************************************//************************ClassQueue_c**********************************///================Queue_c================================Queue_c::Queue_c(){inti;for(i=0;iQueueNum;i++)Q[i]=QueuePre;rear=0;front=0;}//=====================qFull===========================intQueue_c::qFull(){if((rear+1)%QueueNum==front){//coutQueueisfullendl;returnOK;}elsereturnERROR;}//================qEmpty================================intQueue_c::qEmpty(){if(rear==front){//coutQueueisEmptyendl;returnOK;}//endifelsereturnERROR;}//===============qEnQueue=================================intQueue_c::qEnQueue(ElemTypee){if(qFull())returnERROR;Q[rear]=e;rear=(rear+1)%QueueNum;returnOK;}//=================qDeQueue===============================ElemTypeQueue_c::qDeQueue(){ElemTypee;if(qEmpty())returnERROR;e=Q[front];Q[front]=QueuePre;front=(front+1)%QueueNum;returne;}//===================qGetNumber=============================intQueue_c::qGetNumber(){inti;i=((rear-front+QueueNum)%QueueNum);returni;}//==================qGetElem==============================ElemTypeQueue_c::qGetElem(inti){if(qEmpty()){coutERROR!endl;returnERROR;}if(iqGetNumber()-1){coutERROR!thereisnoi,ornoiinQueuenowendl;returnERROR;}returnQ[(front+i)%QueueNum];}//======================qShowQueue==========================intQueue_c::qShowQueue(){inti;if(qEmpty())returnERROR;for(i=front;i!=rear%QueueNum;i=(i+1)%QueueNum){coutQ[i];}//endforcoutendl;returnOK;}//======================qShowAll=========================/*intQueue_c::qShowAll(){inti;for(i=0;iQueueNum;i++)coutQ[i];coutendl;returnOK;}*//*intQueue_c::qXChange(inti){ElemTypet;if(1==qGetNumber())returnOK;if(qEmpty()){coutERROR!endl;returnERROR;}if(iqGetNumber()-1){coutERROR!thereisnoi,ornoiinQueuenowendl;returnERROR;}//endlft=Q[front];Q[front]=Q[(i+front)%QueueNum];Q[(i+front)%QueueNum]=t;returnOK;}*///========================================================/************************EndOfClassQueue_c**********************************//************************ClassWork_c******************************************///======================wCPU=========================intWork_c::wCPU(){intt;ElemTypetemp;//if(wScan(t))buf.qXChange(t);buf.qDec();temp=buf.qDeQueue();if(temp0)buf.qEnQueue(temp);returnOK;}//======================wScan=========================//ElemTypeWork_c::wScan(int&t)//{//inti,m=12;//ElemTypetemp;//for(i=0;ibuf.qGetNumber();i++)//{//temp=buf.qGetElem(i);//if(tempm){m=temp;t=i;}//endif//}//endfor//if(m=0||mbuf.qGetFrontElem())returnERROR;//如果是负数的话在这里就被拦下来了,所以,传出出去的m不可能是负数//returnm;//}//======================wMakeP=========================intWork_c::wMakeP(){intt;t=random(200)%2;if(t){buf.qEnQueue(random(10)+1);returnOK;}returnERROR;}//========================================================/**************************EndofClassWork_c*********************************/

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功