📄 1.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#include<math.h>
#include "iomanip.h"
struct yebiao
{
int kh;
bool ztw;
};
struct yebiao *ye;
int k,ys,ks,ke[10][2]={-1},m=0;
float countm=0,countq=0;
int wst[8][8];
void csh()
{
int i,j;
for(j=0;j<8;j++)
{
wst[0][j]=1;
}
for(i=1;i<8;i++)
for(j=0;j<8;j++)
{
wst[i][j]=rand()%2;
}
}
void yazhan()
{
int i=ks-1;
while(i!=0)
{
ke[i][0]=ke[i-1][0];
ke[i][1]=ke[i-1][1];
i--;
}
}
int diaoru(int x)
{
yazhan();
int i=1,j=0,flag=0;
while(!flag)
{
while(!flag)
{
if(wst[i][j]==0)
{
wst[i][j]=1;
ke[0][0]=x;
ke[0][1]=i*8+j+1;
flag=1;
}j++;
}i++;
}return (ke[0][1]);
}
void printw()
{
int i,j;
cout<<endl;
cout<<" 位示图如下:"<<endl;
cout<<"______________________"<<endl;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
cout<<wst[i][j]<<" ";
}
cout<<endl;
}
cout<<"______________________"<<endl;
}
void printy()
{
int i;
cout<<"----------"<<endl;
cout<<"___________________________________"<<endl;
cout<<"页表为:"<<endl;
cout<<endl;
cout<<setw(3)<<"页号"<<" "<<setw(8)<<"物理块号"<<" "<<setw(3)<<"状态位"<<endl;
for(i=0;i<ys;i++)
{
cout<<setw(3)<<i<<" "<<setw(8)<<dec<<ye[i].kh<<" "<<setw(3)<<dec<<ye[i].ztw<<endl;
}
}
void printk()
{
int i;
cout<<"内存分给作业块的状态为:"<<endl;
cout<<setw(3)<<"逻辑块号"<<" "<<setw(8)<<"页号"<<" "<<setw(3)<<"物理块号"<<endl;
for(i=0;i<ks;i++)
{
cout<<setw(3)<<i<<" "<<setw(8)<<ke[i][0]<<" "<<setw(3)<<dec<<ke[i][1]<<endl;
}
cout<<"___________________________________"<<endl;
}
void cqq()
{
int i;
cout<<"请输入内存块的大小(kb):"<<endl;
cin>>k;
cout<<"请输入作业的页数:"<<endl;
cin>>i;
ys=i;
ye=new yebiao[i];
for(int j=0;j<i;j++)
{
ye[j].kh=-1;
ye[j].ztw=0;
}
cout<<"请输入系统分给该作业内存的块数:"<<endl;
cin>>i;
ks=i;
}
int cha(int x)
{
int i=0;
while(ke[i][1]!=x)
{i++;}
return (i);
}
void zqq()
{
int lj,y,yndz,wl,flag=0;
while(!flag)
{
cout<<"请输入要访问的逻辑地址:(退出系统--fffff)"<<endl;
cin>>hex>>lj;
if(lj==1048575)
{
ll: cout<<endl;
cout<<endl;
cout<<"谢谢你使用本程序!!! 【张旭制作】"<<endl;
cout<<"=========================================================================="<<endl;
cout<<endl;
exit(0);
}
else
y=lj/(1024*k);
yndz=lj%(1024*k);
if(y<ys)
{
if(ye[y].ztw==1)
{
int kk=cha(ye[y].kh);
while(kk!=0)
{
ke[kk][0]=ke[kk-1][0];
ke[kk][1]=ke[kk-1][1];
kk--;
}
ke[0][0]=y;
ke[0][1]=ye[y].kh;
wl=ye[y].kh*1024*k+yndz;
cout<<" ↓ "<<endl;
cout<<"此次命中!!!"<<endl;
cout<<"**************"<<endl;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countm++;
}
else if(m<ks)
{
cout<<" ↓ "<<endl;
cout<<"系统发生缺页!"<<endl;
cout<<"**************"<<endl;
ye[y].kh=diaoru(y);
m=m+1;
ye[y].ztw=1;
wl=ye[y].kh*1024*k+yndz;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countq++;
}
else
{
cout<<" ↓ "<<endl;
cout<<"系统发生缺页!"<<endl;
cout<<"**************"<<endl;
int n;
n=ke[ks-1][0];
ye[n].kh=-1;
ye[n].ztw=0;
ye[y].kh=ke[ks-1][1];
ye[y].ztw=1;
yazhan();
ke[0][0]=y;
ke[0][1]=ye[y].kh;
wl=ye[y].kh*1024*k+yndz;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countq++;
}
flag=1;
}
else
{
int n;
cout<<"地址超界!您可以选择:"<<endl;
cout<<"1、重新输入 0、退出系统"<<endl;
cin>>n;
if(n==0)
{
goto ll;
}
}
}
}
void qqq()
{
int lj,y,yndz,wl,flag=0;
while(!flag)
{
cout<<"请输入要访问的逻辑地址:(退出系统--fffff)"<<endl;
cin>>hex>>lj;
if(lj==1048575)
{
ll: cout<<endl;
cout<<endl;
cout<<"谢谢你使用本程序!!! 【张旭制作】"<<endl;
cout<<"=========================================================================="<<endl;
cout<<endl;
exit(0);
}
else
y=lj/(1024*k);
yndz=lj%(1024*k);
if(y<ys)
{
if(ye[y].ztw==1)
{
wl=ye[y].kh*1024*k+yndz;
cout<<" ↓ "<<endl;
cout<<"此次命中!!!"<<endl;
cout<<"**************"<<endl;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countm++;
}
else if(m<ks)
{
cout<<" ↓ "<<endl;
cout<<"系统发生缺页!"<<endl;
cout<<"**************"<<endl;
ye[y].kh=diaoru(y);
m=m+1;
ye[y].ztw=1;
wl=ye[y].kh*1024*k+yndz;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countq++;
}
else
{
cout<<" ↓ "<<endl;
cout<<"系统发生缺页!"<<endl;
cout<<"**************"<<endl;
int n;
n=ke[ks-1][0];
ye[n].kh=-1;
ye[n].ztw=0;
ye[y].kh=ke[ks-1][1];
ye[y].ztw=1;
yazhan();
ke[0][0]=y;
ke[0][1]=ye[y].kh;
wl=ye[y].kh*1024*k+yndz;
cout<<"物理地址(16进制)为:"<<endl;
cout<<"----------"<<endl;
cout<<hex<<wl<<endl;
countq++;
}
flag=1;
}
else
{
int n;
cout<<"地址超界!您可以选择:"<<endl;
cout<<"1、重新输入 0、退出系统"<<endl;
cin>>n;
if(n==0)
{
goto ll;
}
}
}
}
void main()
{
cout<<"^_^系统正在初始化程序并检测木马病毒.....请等待....."<<endl;
int times;
times=44;
for(int abc=0;abc<times;abc++)
{
if(abc%2==0)
cout<<"\b\b\b\b\b\b"<<" "<<"\b\b\b\b\b\b";
else
cout<<"********";
for(int j=0;j<50;j++)
for(int k=0;k<500;k++)
cout<<' '<<'\b';
}
cout<<"未发现木马病毒及其衍生物!"<<endl;
cout<<"现在是否运行程序?(是-1,否-0) ";
int abcd;
cin>>abcd;
if(abcd==0)
{
cout<<endl;
cout<<"============================================="<<endl;
cout<<"期待你下次使用本程序... 【张旭制作】"<<endl;
cout<<"============================================="<<endl;
cout<<endl;
cout<<endl;
exit(0);
}
cout<<" 页面置换算法 "<<endl;
cout<<" _________________________ 〖张旭制作〗 "<<endl;
cout<<" FIFO算法--1 LRU算法--2 "<<endl;
cout<<endl;
int ba;
cin>>ba;
while(ba==2)
{
cout<<"〓〓〓〓〓#〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓〓〓〓〓§〓#〓〓〓〓"<<endl;
cout<<" ↓ ↓ ↓ ↓ ↓ "<<endl;
cout<<" ☆★☆ ☆★☆ ☆★☆ ☆★☆ ☆★☆ "<<endl;
cout<<" ☆ LRU ☆ ☆ 置 ☆ ☆ 换 ☆ ☆ 算 ☆ ☆ 法 ☆ "<<endl;
cout<<" ☆★☆ ☆★☆ ☆★☆ ☆★☆ ☆★☆ "<<endl;
cout<<" ↓ ↓ ↓ ↓ ↓ "<<endl;
cout<<" ※ ※ ※ ※ ※ "<<endl;
cout<<"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<" 【20064486--张旭】"<<endl;
cout<<" ▓▓▓▓▓▓▓▓▓ ";
csh();
printw();
cqq();
while(1)
{
zqq();
printy();
printk();
printw();
cout<<"命中次数为:"<<countm<<endl;
cout<<"缺页次数为:"<<countq<<endl;
cout<<"访问次数为:"<<countm+countq<<endl;
cout<<"缺页率为:"<<(countq/(countq+countm))*100<<"%"<<endl;
cout<<"------------------------"<<endl;
}
exit(0);
}
while(ba==1)
{
cout<<"〓〓〓〓〓#〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓〓〓〓〓§〓#〓〓〓〓"<<endl;
cout<<" ↓ ↓ ↓ ↓ ↓ "<<endl;
cout<<" ☆★☆ ☆★☆ ☆★☆ ☆★☆ ☆★☆ "<<endl;
cout<<" ☆ FIFO ☆ ☆ 置 ☆ ☆ 换 ☆ ☆ 算 ☆ ☆ 法 ☆ "<<endl;
cout<<" ☆★☆ ☆★☆ ☆★☆ ☆★☆ ☆★☆ "<<endl;
cout<<" ↓ ↓ ↓ ↓ ↓ "<<endl;
cout<<" ※ ※ ※ ※ ※ "<<endl;
cout<<"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<" 【20064486--张旭】"<<endl;
cout<<" ▓▓▓▓▓▓▓▓▓ ";
csh();
printw();
cqq();
while(1)
{
qqq();
printy();
printk();
printw();
cout<<"命中次数为:"<<countm<<endl;
cout<<"缺页次数为:"<<countq<<endl;
cout<<"访问次数为:"<<countm+countq<<endl;
cout<<"缺页率为:"<<(countq/(countq+countm))*100<<"%"<<endl;
cout<<"------------------------"<<endl;
}
exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -