/****************分页存储**************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int i,j=0,m,n,num,q,q1=0,q2,p[1000],p1[1000];
int check(int x)
{
for(i=0;i<m;i++)
{
if(p[i]==x)
{
return 1;
break;
}
}
return 0;
}
void main()
{
cout<<"**********本程序实现请求式分页存储管理算法!**********"<<endl;
loop2:
cout<<"1.先进先出算法FIFO 2.最近最少使用页面淘汰算法LRU 3退出"<<endl;
cout<<"请输入您的选择:";cin>>n;
switch(n)
{
case 1:
cout<<"请输入分配一个作业的存储块数:";cin>>m;
cout<<"请输入存储块进入主存的先后顺序:"<<endl;
for(i=0;i<m;i++)
{
p1[i]=i+1;
do
{
q2=q1;
cin>>q;
q1=q;
if(!(q1==q2||q<1||q>m)) break;
}while(1);
p[i]=q;
}
cout<<"进入主存的先后顺序为:";
for(i=0;i<m;i++)
{
cout<<" "<<p[i];
}
cout<<endl;
loop:
cout<<" 块列表为:";
for(i=0;i<m;i++)
{
cout<<" "<<p1[i];
}
do{
cout<<endl<<"请输入新块的块号(新块号应与旧块号不同):";cin>>num;
}while(check(num));
for(i=0;i<m;i++)
{
if(p1[i]==p[j])
{
p[j]=num;
p1[i]=num;
}
}
j=j+1;
if(j==m)
j=0;
cout<<"替换后顺序为:";
for(i=0;i<m;i++)
{
cout<<" "<<p[i];
}
cout<<endl;
goto loop;
case 2:
cout<<"请输入列表页面个数:";cin>>m;
for(i=0;i<m;i++)
{
p[i]=i+1;
}
loop1:
cout<<"页面排列为:";
for(i=0;i<m;i++)
{
cout<<" "<<p[i];
}
cout<<endl<<"请输入新访问的页面:";cin>>n;
for(i=0;i<m;i++)
{
if(p[i]==n)
{
for(j=i;j<m;j++)
{
p[j]=p[j+1];
}
p[m-1]=n;
goto loop1;
}
}
for(i=0;i<m;i++)
{
p[i]=p[i+1];
}
p[m-1]=n;
goto loop1;
case 3:
exit(1);
default:
cout<<"输入错误!"<<endl;
goto loop2;
}
}
/**************内存分配*********************/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
int MAX[10]={50,70,30,90,10,0,0,0,0,0},NEED[10]={0,0,0,0,0,0,0,0,0,0},score[5],i,j,mark,n;
int wzx[10]={0,0,0,0,0,0,0,0,0,0};
int w=5,number=0;
cout<<"**********本程序实现内存分配算法!**********"<<endl;
loop:
int num[5]={0,0,0,0,0};
cout<<"分区: Part1 Part2 Part3 Part4 Part5 | Part6 Part7 Part8 Part9 Part10"<<endl;
cout<<"内存: "<<MAX[0]<<" "<<MAX[1]<<" "<<MAX[2]<<" "<<MAX[3]<<" "<<MAX[4]<<" | "<<setw(2)<<MAX[5]<<" "<<setw(2)<<MAX[6]<<" "<<setw(3)<<MAX[7]<<" "<<setw(3)<<MAX[8]<<" "<<setw(3)<<MAX[9]<<endl;
cout<<"分配: "<<setw(3)<<NEED[0]<<" "<<setw(3)<<NEED[1]<<" "<<setw(3)<<NEED[2]<<" "<<setw(3)<<NEED[3]<<" "<<setw(3)<<NEED[4]<<" | "<<setw(3)<<NEED[5]<<" "<<setw(3)<<NEED[6]<<" "<<setw(3)<<NEED[7]<<" "<<setw(3)<<NEED[8]<<" "<<setw(3)<<NEED[9]<<endl;
cout<<"信息: "<<setw(3)<<wzx[0]<<" "<<setw(3)<<wzx[1]<<" "<<setw(3)<<wzx[2]<<" "<<setw(3)<<wzx[3]<<" "<<setw(3)<<wzx[4]<<" | "<<setw(3)<<wzx[5]<<" "<<setw(3)<<wzx[6]<<" "<<setw(3)<<wzx[7]<<" "<<setw(3)<<wzx[8]<<" "<<setw(3)<<wzx[9]<<endl;
if(w==10||number==5)
{
cout<<"内存分配完毕!"<<endl;getch();exit(1);
}
loop1:
cout<<"1.首次适应算法 2.最佳适应算法 3.释放资源 4.退出"<<endl;
cout<<"请选择:";cin>>n;
switch(n)
{
case 1:
loop2:
cout<<"请分配资源:";cin>>mark;
if(mark<=0||mark>90)
{
cout<<"资源分配应在(0,90]之间,请重新输入!"<<endl;
goto loop2;
}
else
{
for(i=0;i<5;i++)
{
if((MAX[i]-NEED[i])>=mark)
{
if(w!=10)
{
wzx[i]=1;
NEED[i]+=mark;
MAX[w]=MAX[i]-NEED[i];
if(MAX[i]>NEED[i])
{
w+=1;
}
MAX[i]=mark;
number+=1;
}
break;
}
}
}
goto loop;
case 2:
cout<<"请分配资源:";cin>>mark;
while(mark<=0||mark>90)
{
cout<<"资源分配错误,请重新输入!"<<endl;cout<<"请分配资源:";cin>>mark;
}
for(i=0;i<5;i++)
{
if((MAX[i]-NEED[i]-mark)<0)
score[i]=100;
else if((MAX[i]-NEED[i]-mark)>=0)
score[i]=MAX[i]-NEED[i]-mark;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(score[i]<score[j])
{
num[i]=num[i]+1;
}
}
if(num[i]==4)
{
if(w!=10)
{
wzx[i]=1;
NEED[i]+=mark;
MAX[w]=MAX[i]-NEED[i];
if(MAX[i]>NEED[i])
{
w+=1;
}
MAX[i]=mark;
number+=1;
}
break;
}
}
goto loop;
case 3:
for(i=0;i<10;i++)
{
MAX[i]=0;NEED[i]=0;wzx[i]=0;
}
MAX[0]=50;MAX[1]=70;MAX[2]=30;MAX[3]=90;MAX[4]=10;
w=5;number=0;
goto loop;
case 4:
exit(1);
default:
cout<<"输入错误!"<<endl;
goto loop1;
}
}
[此贴子已经被作者于2005-12-30 17:13:04编辑过]
----------------解决方案--------------------------------------------------------
..........................
----------------解决方案--------------------------------------------------------