选了这个题目的同学有福了,可以参考一下~~~
有一些注释……😄
另外……如果执行结果是乱码,把#define N 40改为更大的数。
多说一句:如果真的有帮助,麻烦给个评论再走呗~~~
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#define N 40
#define MM "Max_Matrix.txt"
#define AM "Allocation_Matrix.txt"
using namespace std;
typedef struct Banker{
int Max[N][N];
int Allocation[N][N];
int Need[N][N];
int Availiable[N][N];
int Work[N][N];
int All_Resource[N][N];
int Safe_Sequel[N];
bool Visited[N][N];
bool Finish[N][N];
int Request[N];
}BK;
void Create_Random_Progress(){
FILE *fp1,*fp2;
fp1=fopen(MM,"w+");
fp2=fopen(AM,"w+");
if(fp1&&fp2){
system("cls");
cout<<"开始初始化进程……"<<endl;
int number;
srand((unsigned) time(NULL)); //用时间做种,每次产生随机数不一样
char Space=' ';
char Enter='\n';
for (int i=1; i<28; i++){
number = rand() % 7; //产生0-6随机数
fprintf(fp1,"%d",number);
if(i%3!=0&&i!=1)
fprintf(fp1,"%c",Space);
if(i%3==0)
fprintf(fp1,"%c",Enter);
if(i==1)
fprintf(fp1,"%c",Space);
}
for (int i=1; i<28; i++){
number = rand() % 7; //产生0-6的随机数
fprintf(fp2,"%d",number);
if(i%3!=0&&i!=1)
fprintf(fp2,"%c",Space);
if(i%3==0)
fprintf(fp2,"%c",Enter);
if(i==1)
fprintf(fp2,"%c",Space);
}
cout<<"初始化完成。"<<endl;
}
else
cout<<"出现错误,请检查磁盘剩余空间。"<<endl;
fclose(fp1);
fclose(fp2);
}
void Banker_Get_Matrix_Max(){
Banker b;
FILE *fp;
fp=fopen(MM,"r");
int a;
int i=1;
while(i<5){
for(int j=1;j<4;j++){
if(fscanf(fp,"%d",&a)!=EOF)
b.Max[i][j]=a;
}
i++;
}
fclose(fp);
}
void Banker_Get_Matrix_Allocation(){
Banker b;
FILE *fp;
fp=fopen(AM,"r");
int a;
int i=1;
while(i<5){
for(int j=1;j<4;j++){
if(fscanf(fp,"%d",&a)!=EOF)
b.Allocation[i][j]=a;
}
i++;
}
fclose(fp);
}
void Banker_Get_Matrix_Completed(){
Banker b;
for(int i=1;i<5;i++)
for(int j=1;j<4;j++){
while(b.Max[i][j]-b.Allocation[i][j]<0)
b.Allocation[i][j]--;
}
}
void Banker_Get_Matrix_Need(){
Banker b;
for(int i=1;i<5;i++)
for(int j=1;j<4;j++)
b.Need[i][j]=b.Max[i][j]-b.Allocation[i][j];
}
void Banker_Print_InitList(){
Banker b;
printf("进程号 最 大 需 求 量 Max 已分配资源Allocation\n");
for(int i=1;i<5;i++){
cout<<" "<<i<<"\t";
for(int j=1;j<4;j++)
cout<<b.Max[i][j]<<" ";
for(int j=1;j<4;j++){
cout<<b.Allocation[i][j]<<" ";
if(j%3==0)
cout<<endl;
}
}
}
void Banker_Rest_Resource(){
Banker b;
cout<<"请输入此时系统可用资源向量:";
for(int i=1;i<4;i++)
cin>>b.Availiable[1][i];
}
void Banker_Get_All_Resource(){
Banker b;
int t=0;
for(int i=1;i<4;i++)
for(int j=1;j<5;j++){
t+=b.Allocation[j][i];
if(j%4==0){
b.All_Resource[1][i]=t;
t=0;
}
}
for(int i=1;i<4;i++)
b.All_Resource[1][i]+=b.Availiable[1][i];
cout<<"系统资源向量总数:(";
for(int i=1;i<4;i++){
cout<<b.All_Resource[1][i];
if(i<3)
cout<<",";
}
cout<<")"<<endl;
}
void Banker_Print_Need(){
Banker b;
cout<<"进程对资源的需求矩阵:"<<endl;
for(int i=1;i<5;i++)
for(int j=1;j<4;j++){
cout<<b.Need[i][j]<<" ";
if(j%3==0)
cout<<endl;
}
}
/*int Banker_Safety_Algorithm_Test(){
Banker b;
for(int i=1;i<4;i++)
b.Work[1][i]=b.Availiable[1][i];
for(int i=1;i<5;i++){
int counter=0;
for(int j=1;j<4;j++){
if(b.Work[1][j]-b.Need[i][j]>=0)
counter++;
if(counter==3)
b.Finish[i][j]=true;
}
}
int t=0;
for(int i=1;i<5;i++)
if(b.Finish[i][j]==true)
t++;
if(t==0){
cout<<"没有任何资源能让这些任务执行完成。"<<endl;
return 0;
}else
return 1;
}*/
int Banker_Safety_Algorithm(){//int test
Banker b;
memset(b.Finish,false,sizeof(b.Finish));
memset(b.Visited,false,sizeof(b.Visited));
memset(b.Safe_Sequel,0,sizeof(b.Safe_Sequel));
// if
// if(test==0)
// return 0;
int res=1;
for(int i=1;i<4;i++)
b.Work[1][i]=b.Availiable[1][i];
for(int r=1;r<5;r++){
for(int i=1;i<5;i++){
int counter=0;
for(int j=1;j<4;j++){
if(b.Work[1][j]-b.Need[i][j]>=0)
counter++;
if(counter==3)
b.Finish[i][j]=true;
if(b.Finish[i][3]==true&&b.Visited[i][3]==false){
for(int k=1;k<4;k++){
b.Work[1][k]+=b.Allocation[i][k];
b.Visited[i][3]=true;
}
b.Safe_Sequel[res]=i;
res++;
}
}
}
}
int safe=1;
for(int i=1;i<5;i++)
if(b.Finish[i][3])
safe++;
if(safe==5){
cout<<"至少存在安全序列:";
for(int i=1;i<safe;i++)
cout<<b.Safe_Sequel[i]<<"\t";
cout<<"可使进程安全执行完毕。"<<endl;
return 1;
}else
cout<<"系统资源不足。"<<endl;
return 0;
}
/* for(int i=1;i<5;i++){
for(int j=1;j<4;j++)
}*/
// }
void Banker_Get_Request(){
Banker b;
int n;
cout<<"请输入要申请资源的进程号和申请的各项资源数量:";
cin>>n;
for(int i=1;i<4;i++){
cin>>b.Request[i];
b.Need[n][i]+=b.Request[i];
}
}
int main()
{
int n;
char ch='y';
while(ch=='y'){
Create_Random_Progress();
Banker_Get_Matrix_Max();
Banker_Get_Matrix_Allocation();
Banker_Get_Matrix_Completed();
Banker_Get_Matrix_Need();
Banker_Print_InitList();
Banker_Rest_Resource();
Banker_Get_All_Resource();
Banker_Print_Need();
if(Banker_Safety_Algorithm()){
Banker_Get_Request();
Banker_Print_Need();
Banker_Safety_Algorithm();
}else
cout<<"系统资源不足,禁止申请。"<<endl;
cout<<"您还要做什么事情吗?"<<endl;
ch=getchar();
ch=getchar();
system("cls");
}
cout<<"再见。"<<endl;
return 0;
}