选了这个题目的同学有福了,可以参考一下~~~
有一些注释……?
另外……如果执行结果是乱码,把#define N 40改为更大的数。
多说一句:如果真的有帮助,麻烦给个评论再走呗~~~
1 #include2 #include 3 #include 4 #include 5 #define N 40 6 #define MM "Max_Matrix.txt" 7 #define AM "Allocation_Matrix.txt" 8 using namespace std; 9 typedef struct Banker{ 10 int Max[N][N]; 11 int Allocation[N][N]; 12 int Need[N][N]; 13 int Availiable[N][N]; 14 int Work[N][N]; 15 int All_Resource[N][N]; 16 int Safe_Sequel[N]; 17 bool Visited[N][N]; 18 bool Finish[N][N]; 19 int Request[N]; 20 }BK; 21 void Create_Random_Progress(){ 22 FILE *fp1,*fp2; 23 fp1=fopen(MM,"w+"); 24 fp2=fopen(AM,"w+"); 25 if(fp1&&fp2){ 26 system("cls"); 27 cout<<"开始初始化进程……"< >b.Availiable[1][i];122 }123 void Banker_Get_All_Resource(){124 Banker b;125 int t=0;126 for(int i=1;i<4;i++)127 for(int j=1;j<5;j++){128 t+=b.Allocation[j][i];129 if(j%4==0){130 b.All_Resource[1][i]=t;131 t=0; 132 }133 }134 for(int i=1;i<4;i++)135 b.All_Resource[1][i]+=b.Availiable[1][i];136 cout<<"系统资源向量总数:("; 137 for(int i=1;i<4;i++){138 cout< =0)162 counter++;163 if(counter==3)164 b.Finish[i][j]=true;165 }166 } 167 int t=0;168 for(int i=1;i<5;i++)169 if(b.Finish[i][j]==true)170 t++;171 if(t==0){172 cout<<"没有任何资源能让这些任务执行完成。"< =0)193 counter++;194 if(counter==3)195 b.Finish[i][j]=true;196 if(b.Finish[i][3]==true&&b.Visited[i][3]==false){197 for(int k=1;k<4;k++){198 b.Work[1][k]+=b.Allocation[i][k];199 b.Visited[i][3]=true;200 201 }202 b.Safe_Sequel[res]=i;203 res++; 204 }205 }206 }207 }208 int safe=1;209 for(int i=1;i<5;i++)210 if(b.Finish[i][3])211 safe++;212 if(safe==5){213 cout<<"至少存在安全序列:";214 for(int i=1;i >n;232 for(int i=1;i<4;i++){233 cin>>b.Request[i];234 b.Need[n][i]+=b.Request[i];235 }236 }237 int main()238 {239 int n;240 char ch='y';241 while(ch=='y'){242 Create_Random_Progress();243 Banker_Get_Matrix_Max();244 Banker_Get_Matrix_Allocation();245 Banker_Get_Matrix_Completed();246 Banker_Get_Matrix_Need();247 Banker_Print_InitList();248 Banker_Rest_Resource();249 Banker_Get_All_Resource();250 Banker_Print_Need();251 if(Banker_Safety_Algorithm()){252 Banker_Get_Request();253 Banker_Print_Need();254 Banker_Safety_Algorithm();255 }else256 cout<<"系统资源不足,禁止申请。"<