下面是我的代码(根据实验指导上面的思想做的):
#include<string> #include<iostream> using namespace std; struct Page//页表结构 { int numPage;//页号 bool flagPage;//装入标志 int numMemory;//内存块号 bool flagChang;//修改标志 int hardPosition;//外存地址 }; Page job[7]; int p[4]={0,1,2,3};//已装入内存的页面 int k=0;//指向当前需置换的页面 void creatJob()//创建Job { job[0].numPage=0; job[0].flagPage=true; job[0].numMemory=5; job[0].flagChang=false; job[0].hardPosition=011; job[1].numPage=1; job[1].flagPage=true; job[1].numMemory=8; job[1].flagChang=false; job[1].hardPosition=012; job[2].numPage=2; job[2].flagPage=true; job[2].numMemory=9; job[2].flagChang=false; job[2].hardPosition=013; job[3].numPage=3; job[3].flagPage=true; job[3].numMemory=1; job[3].flagChang=false; job[3].hardPosition=021; job[4].numPage=4; job[4].flagPage=false; job[4].numMemory=0; job[4].flagChang=false; job[4].hardPosition=022; job[5].numPage=5; job[5].flagPage=false; job[5].numMemory=0; job[5].flagChang=false; job[5].hardPosition=023; job[6].numPage=6; job[6].flagPage=false; job[6].numMemory=0; job[6].flagChang=false; job[6].hardPosition=121; } struct Command//指令结构(简化版) { string OperateOne;//第一个操作 int PageOne;//第一个操作数 int UnitOne;//第一个操作数的单元号 string OperateTwo;//第二个操作 int PageTwo;//第二个操作数 int UnitTwo;//第二个操作数的单元号 }; Command command[6]; void creatCommand() { command[0].OperateOne="+"; command[0].PageOne=0; command[0].UnitOne=70; command[0].OperateTwo="移位"; command[0].PageTwo=4; command[0].UnitTwo=53; command[1].OperateOne="+"; command[1].PageOne=1; command[1].UnitOne=50; command[1].OperateTwo="+"; command[1].PageTwo=5; command[1].UnitTwo=23; command[2].OperateOne="*"; command[2].PageOne=2; command[2].UnitOne=15; command[2].OperateTwo="存"; command[2].PageTwo=1; command[2].UnitTwo=73; command[3].OperateOne="存"; command[3].PageOne=3; command[3].UnitOne=21; command[3].OperateTwo="取"; command[3].PageTwo=2; command[3].UnitTwo=78; command[4].OperateOne="取"; command[4].PageOne=0; command[4].UnitOne=56; command[4].OperateTwo="+"; command[4].PageTwo=4; command[4].UnitTwo=1; command[5].OperateOne="-"; command[5].PageOne=6; command[5].UnitOne=40; command[5].OperateTwo="存"; command[5].PageTwo=6; command[5].UnitTwo=84; } int main() { creatJob(); creatCommand(); for(int i=0;i<6;i++) { Loop1: if(job[command[i].PageOne].flagPage==true)//如果该页存在则输出,若不存在则按照FIFO算法调入 { if(command[i].OperateOne=="存"||command[i].OperateOne=="移位")//如果是“存”,则“修改标志位”为true job[command[i].PageOne].flagChang=true; cout<<"页号 "<<command[i].PageOne<<"的绝对地址为:"<<job[command[i].PageOne].numMemory*128+command[i].UnitOne<<"\n"; } else { if(job[p[k]].flagChang==true)//如果修改过则先调出 cout<<"out "<<p[k]<<"\n"; //装入页面:1.打印要装入的页号2.修改要装入页面的装入标志位3.将调出页面的装入标志位置为false4.将当前页面的内存块号赋值给要装入的内存块号5.修改数组p[]6.修改指针k cout<<"in "<<command[i].PageOne<<"\n";//调入 job[command[i].PageOne].flagPage=true;//装入标志置为true job[p[k]].flagPage=false; job[command[i].PageOne].numMemory=job[p[k]].numMemory;//将要覆盖的页面的内存块号赋值给要装入的内存块号 p[k]=command[i].PageOne; k=(k+1)%4; goto Loop1; } Loop2: if(job[command[i].PageTwo].flagPage==true) { if(command[i].OperateTwo=="存"||command[i].OperateTwo=="移位") job[command[i].PageTwo].flagChang=true; cout<<"页号 "<<command[i].PageTwo<<"的绝对地址为:"<<job[command[i].PageTwo].numMemory*128+command[i].UnitTwo<<"\n"; } else { if(job[p[k]].flagChang==true)//如果修改过则先调出 cout<<"out "<<p[k]<<"\n"; cout<<"in "<<command[i].PageTwo<<"\n";//调入 job[command[i].PageTwo].flagPage=true;//装入标志置为true job[p[k]].flagPage=false; job[command[i].PageTwo].numMemory=job[p[k]].numMemory;//将要覆盖的页面的内存块号赋值给要装入的内存块号 job[command[i].PageTwo].flagPage=true; p[k]=command[i].PageTwo;//调入 k=(k+1)%4; goto Loop2; } } for(int a=0;a<4;a++) cout<<p[a]<<" "; cout<<"\n"; return 0; }结果如下: