下面是我的代码:
#include<iostream> using namespace std; struct Page//页表结构 { int numPage; bool flagPage; int numMemory; int hardPosition; }; Page job[7]; void creatJob()//创建Job { job[0].numPage=0; job[0].flagPage=true; job[0].numMemory=5; job[0].hardPosition=011; job[1].numPage=1; job[1].flagPage=true; job[1].numMemory=8; job[1].hardPosition=012; job[2].numPage=2; job[2].flagPage=true; job[2].numMemory=9; job[2].hardPosition=013; job[3].numPage=3; job[3].flagPage=true; job[3].numMemory=1; job[3].hardPosition=021; job[4].numPage=4; job[4].flagPage=false; job[4].numMemory=0; job[4].hardPosition=022; job[5].numPage=5; job[5].flagPage=false; job[5].numMemory=0; job[5].hardPosition=023; job[6].numPage=6; job[6].flagPage=false; job[6].numMemory=0; job[6].hardPosition=121; } struct Command//指令结构(简化版) { int PageOne;//第一个操作数 int UnitOne;//第一个操作数的单元号 int PageTwo;//第二个操作数 int UnitTwo;//第二个操作数的单元号 }; Command command[6]; void creatCommand() { command[0].PageOne=0; command[0].UnitOne=70; command[0].PageTwo=4; command[0].UnitTwo=53; command[1].PageOne=1; command[1].UnitOne=50; command[1].PageTwo=5; command[1].UnitTwo=23; command[2].PageOne=2; command[2].UnitOne=15; command[2].PageTwo=1; command[2].UnitTwo=73; command[3].PageOne=3; command[3].UnitOne=21; command[3].PageTwo=2; command[3].UnitTwo=78;//实验指导书中此处应是078,但在c,c++中以0开头的数字串被解释为8进制,即8不能出现在其中,编译时会通不过。且八进制的运算结果跟十进制预期结果不同(刚开始没注意,还以为是逻辑错误呢。)! command[4].PageOne=0; command[4].UnitOne=56; command[4].PageTwo=4; command[4].UnitTwo=1; command[5].PageOne=6; command[5].UnitOne=40; command[5].PageTwo=6; command[5].UnitTwo=84;//此处同上 } int main() { creatJob(); creatCommand(); for(int i=0;i<6;i++) { if(job[command[i].PageOne].flagPage==true) { cout<<"页号 "<<command[i].PageOne<<"的绝对地址为:"<<job[command[i].PageOne].numMemory*128+command[i].UnitOne<<"\n"; } else { cout<<"* "<<command[i].PageOne<<"\n"; } if(job[command[i].PageTwo].flagPage==true) { cout<<"页号 "<<command[i].PageTwo<<"的绝对地址为:"<<job[command[i].PageTwo].numMemory*128+command[i].UnitTwo<<"\n"; } else { cout<<"* "<<command[i].PageTwo<<"\n"; } } return 0; }
运行结果如下: