資源簡介
編程實現LRU算法,模擬實現虛擬存儲器的地址變換過程。
代碼片段和文件信息
#include?“stdio.h“
#include?“math.h“
#include“malloc.h“
#include?“stdlib.h“
#include?“iostream.h“
#include?“math.h“
#define?OK?1
#define?ERROR?-1
#define?Max?5
typedef?int?status;
typedef?int?SElemType;
int?k=0;//記錄缺頁次數
/*--------------------棧及其操作---------------------*/
typedef?struct?{
????SElemType??*base;?//棧底指針???
????SElemType???*top;??//棧頂指針
????int??count;????//棧的大小
}SqStack;
//構造空棧
status?InitStack?(SqStack?&S){???
S.base=(SElemType?*)malloc(Max?*?sizeof(SElemType));
if?(!S.base)???return(OVERFLOW);?
S.count?=?0;
S.top?=?S.base;
return(OK);
}
//入棧
status?Push(SqStack?&sSElemType?e)
{
*s.top++=e;
s.count++;
// cout<<“插入“< return?OK;
}
//銷毀棧
status?DestroyStack(SqStack?&S)
{
S.top=NULL;
S.base=NULL;
delete[]?S.base;
????S.count=0;
return?OK;
}
//判斷棧是否為空
bool?EmptyStack(SqStack?s)
{
if(s.count==0)?return?true;
else??????????return?false;
}
//是否已滿
bool?full(SqStack?s)
{
if(s.count==5)?return?true;
return?false;
}
//判斷是否已經存在
int?equeal(SqStack?sSElemType?e)
{
int?num=s.count;
if(EmptyStack(s))?return?-1;
for?(int?i=1;i<=num;i++)
{
if(*(s.top-i)==e)
return?i;
}
return?-1;
}
//輸出
void?print(SqStack?s){
int?ainum=s.count;
//cout<<“有“< for?(i=0;i {
a=*(s.base+i);
cout< }
cout< }
/*----------------頁表---------------------*/
int?pageLen=15;//頁面數
int?l=1024;??//頁面大小
struct?page{
int?pageNum;
int?memNum;
}p[15];
int?initiate(){
int?ij;
for?(i=0;i {???
j=rand()%11;
p[i].pageNum=i;
p[i].memNum=i+j;
}
return?1;
}
int?locate(int?n){
for?(int?i=0;i {
if?(p[i].pageNum==n)
{
return?p[i].memNum;
}
}
return?-1;
}
//物理地址
int?adress(){
int?padadr;
cout<<“請輸入邏輯地址:“;
cin>>adr;
cout< pa=(int)adr/l;
if?(pa<0||pa>=l)
{
cout<<“越界“< return?-1;
}
d=adr%l;
int?n=locate(pa);
if?(n!=-1)
{
int?m=n
- 上一篇:MFC實現完整GoBang五子棋
- 下一篇:自動生成算式的四則運算機器
評論
共有 條評論