#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MaxNumber=100;
int FreePartition[MaxNumber];//空闲分区大小P1, … ,Pn
int ProcessNeed[MaxNumber];//进程需要的分区大小S1, … ,Sm
int FirstPartition[MaxNumber];//存放首次适应算法(FistFit)结果
int CycleFirstPartition[MaxNumber];//存放循环首次适应算法(NextFit)结果
int BestPartition[MaxNumber];//存放最佳适应算法(BestFit)结果
int WorstPartition[MaxNumber];//存放最坏适应算法(WorstFit)
char ProcessName[MaxNumber];//进程名,默认为A、B、C、D、E...
int M,N;//进程个数M,空闲分区个数N
char order[MaxNumber][MaxNumber];
int state[MaxNumber];//进程分配标志
void inputData()
{
cout<<"请设置进程的个数M:";
cin>>M;
cout<<endl;
cout<<"请设置"<<"M"<<"个进程需要的分区大小(ProcessNeed[MaxNumber]),此部分由文件读入。\n\n";
fstream fin("E://ProcessNeed.txt");
int flag=1;
if(flag==1)
{
for(int i=0;i<M;i++)
fin>>ProcessNeed[i];
flag=0;
}
cout<<"请设置空闲分区的个数N:";
cin>>N;
cout<<endl;
cout<<"请设置"<<"N"<<"个空闲分区的大小(FreePartition[MaxNumber](KB)),此部分由文件读入。\n\n";
if(flag==0)
{
fstream fin("E://FreePartition.txt");
for(int i=0;i<N;i++)
fin>>FreePartition[i];
}
for(int i=0;i<M;i++)
{
char c=i+65;
ProcessName[i]=c;
}
}
void outputData()
{
cout<<" 进程名 \t";
for(int i=0;i<M;i++)
cout<<ProcessName[i]<<"\t";
cout<<endl;
cout<<"进程所需分区\t";
for(int i=0;i<M;i++)
cout<<ProcessNeed[i]<<"\t";
cout<<endl;
cout<<"空闲分区大小\t";
for(int i=0;i<N;i++)
cout<<FreePartition[i]<<"\t";
cout<<endl;
}
void FirstFit()
{
int i,j,k;
int FFProcessNeed[MaxNumber];
for(int i=0;i<M;i++)
FFProcessNeed[i]=ProcessNeed[i];
//for(int i=0;i<N;i++)
// FirstPartition[i]=FFFreePartition[i];
//for(i=0;i<M;i++)//找出第一块满足作业的分区
// for(j=0;j<N;j++)
// {
// if(FFProcessNeed[i]>FirstPartition[j])
// continue;
// else
// {
// FirstPartition[j]-=FFProcessNeed[i];//找到后把分区大小减去作业的大小
// cout<<"进程"<<ProcessName[i]<<"在第"<<j+1<<"块分区中"<<endl;
// break;
// }
// }
// cout<<endl;
for(int i=0;i<N;i++)
FirstPartition[i]=FreePartition[i];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
//找到第一个合适的空闲分区
if((FFProcessNeed[i] <=FirstPartition[j]) && (!state[i]))
{
for(k=0;k<3;k++) //记录作业分配
{
if(order[j][k]==NULL)
{
order[j][k]=ProcessName[i];
break;
}
else
continue;
}
FirstPartition[j]=FirstPartition[j]-FFProcessNeed[i];
state[i]=1;
}
}
}
}
void NextFit()
{
int i=0;
int j=0;
int k;
int NFProcessNeed[MaxNumber];
for(i=0;i<M;i++)
NFProcessNeed[i]=ProcessNeed[i];
for(i=0;i<N;i++)
CycleFirstPartition[i]=FreePartition[i];
while((i<N) && (j<M))
{
if((NFProcessNeed[i] <= CycleFirstPartition[j]) && (!state[i]))
{
for(k=0;k<3;k++) //记录作业分配
{
if(order[j][k]==NULL)
{
order[j][k]=ProcessName[i];
break;
}
else
continue;
}
CycleFirstPartition[j]=CycleFirstPartition[j]-NFProcessNeed[i];
state[i]=1;
i++;
}
else
j++;
}
}
//void NextFit()
//{
// int i=0,j=0,k;
// int NFProcessNeed[MaxNumber];
// for(i=0;i<M;i++)
// NFProcessNeed[i]=ProcessNeed[i];
// for(i=0;i<N;i++)
// CycleFirstPartition[i]=FreePartition[i];
// while((i<N) && (j<M))
// {
// if((NFProcessNeed[i] <=CycleFirstPartition[j]) && (!state[i]))
// {
// for(k=0;k<3;k++) //记录作业分配
// {
// if(order[j][k]==NULL)
// {
// order[j][k]=ProcessName[i];
// break;
// }
// else
// continue;
// }
// CycleFirstPartition[j]=CycleFirstPartition[j]-NFProcessNeed[i];
// state[i]=1;
// i++;
// }
// else
// j++;
// }
//}
////void BestFit()
//{
// int i,j,k;
// int BFProcessNeed[MaxNumber];
// int BFFreePartition[MaxNumber];
// for(i=0;i<M;i++)
// BFProcessNeed[i]=ProcessNeed[i];
// for(i=0;i<N;i++)
// BFFreePartition[i]=FreePartition[i];
// for(i=0;i<N;i++)
// BestPartition[i]=BFFreePartition[i];
// for(i=0;i<M;i++)
// {
// k=0;
// for(j=0;j<N;j++)
// {
// if(BestPartition[j]>=BFProcessNeed[i])
// {
// k=j;
// break;
// }
// }
// for(int n=0;n<N;n++)
// {
// if(BestPartition[n]<BestPartition[k]&&BestPartition[n]>=BFProcessNeed[i])
// k=n;
// }
// BestPartition[k]-=BFProcessNeed[i];
// cout<<"进程"<<ProcessName[i]<<"在第"<<j+1<<"块分区中"<<endl;
// }
//}
//void WorstFit()
//{
// int i,j,k;
// int WFProcessNeed[MaxNumber];
// int WFFreePartition[MaxNumber];
// for(i=0;i<M;i++)
// WFProcessNeed[i]=ProcessNeed[i];
// for(i=0;i<N;i++)
// WFFreePartition[i]=FreePartition[i];
// for(i=0;i<N;i++)
// WorstPartition[i]=WFFreePartition[i];
// for(i=0;i<M;i++)
// {
// k=0;
// for(j=0;j<N;j++)
// {
// if(WorstPartition[j]>WorstPartition[k])
// k=j;
// }
// WorstPartition[k]-=WFProcessNeed[i];
// cout<<"进程"<<ProcessName[i]<<"在第"<<j+1<<"块分区中"<<endl;
// }
//}
void BestFit()
{
int i,j,k,d,temp;
int BFProcessNeed[MaxNumber];
for(i=0;i<M;i++)
BFProcessNeed[i]=ProcessNeed[i];
for(i=0;i<N;i++)
BestPartition[i]=FreePartition[i];
for(i=0;i<M;i++)
{
temp=BestPartition[0];
k=0;
//找到第一个合适的空闲分区
while(BFProcessNeed[i] > temp)
{
k++;
temp=BestPartition[k];
}
for(j=0;j<N;j++)
{
//按最佳适应算法找到符合的空闲区
if((BFProcessNeed[i] <= BestPartition[j]) && (temp > BestPartition[j]) && (!state[i]))
{
temp=BestPartition[j];
k=j;
}
else
continue;
}
for(d=0;d<3;d++) //记录作业分配
{
if(order[k][d]==NULL)
{
order[k][d]=ProcessName[i];
break;
}
else
continue;
}
BestPartition[k]=BestPartition[k]-BFProcessNeed[i];
state[i]=1;
}
}
void WorstFit() //最坏适应算法
{
int i,j,k,d,temp;
int WFProcessNeed[MaxNumber];
for(i=0;i<M;i++)
WFProcessNeed[i]=ProcessNeed[i];
for(i=0;i<N;i++)
WorstPartition[i]=FreePartition[i];
for(i=0;i<M;i++)
{
temp=WorstPartition[0];
k=0;
for(j=0;j<N;j++)
{
//按最坏适应算法找到合适的空闲分区
if((WFProcessNeed[i] <= WorstPartition[j]) && (temp < WorstPartition[j]) && (!state[i]))
{
temp=WorstPartition[j];
k=j;
}
else
continue;
}
for(d=0;d<3;d++) //记录作业分配
{
if(order[k][d]==NULL)
{
order[k][d]=ProcessName[i];
break;
}
else
continue;
}
WorstPartition[k]=WorstPartition[k]-WFProcessNeed[i];
state[i]=1;
}
}
void showResult()
{
int i,j;
for(i=0;i<N;i++)
{
cout<<"|";
for(j=0;j<3;j++)
{
if(order[i][j]==' ')
cout<<" ";
else
printf("%2c",order[i][j]);
}
}
cout<<"|\n";
}
int main()
{
int choice;
cout<<"\n============================动态分区分配算法==========================\n\n";
inputData();
cout<<"===============================显示配置信息=============================\n";
outputData();
cout<<endl;
while(1)
{
cout<<"\n====1-首次适应算法====\n";
cout<<"====2-循环首次适应算法\n";
cout<<"====3-最佳适应算法====\n";
cout<<"====4-最坏适应算法====\n";
cout<<"====其他任意键退出====\n\n";
cout<<"请输入功能键:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n-----------------------------首次适应(FirstFit)算法-----------------------------\n";
outputData();
FirstFit();
showResult();
break;
case 2:
cout<<"\n---------------------------循环首次适应(NextFit)算法----------------------------\n";
outputData();
NextFit();
showResult();
break;
case 3:
cout<<"\n----------------------------