当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 银行家算法C++程序

银行家算法C++程序

2019年04月21日  | 移动技术网IT编程  | 我要评论

9420,恒大地产老总,抵抗 惩罚

此程序在windows10    codeblocks17.12环境下测试运行,其他编程环境未经测试!

 

 


 

作业需求↓↓↓↓↓↓

 


 

运行效果图如下

 

 


 

(codeblocks下载地址http://www.codeblocks.org/downloads/binaries)


 

c++代码

  1 #include<iostream>
  2 #include<string>
  3 #include<stdlib.h>
  4 using namespace std;
  5 
  6 int **all,**ma,*work,**need,*a,*sum,*finish;//定义已分配矩阵,最大需求矩阵,工作向量,需求矩阵,可利用资源向量,各类资源总个数,finish标志
  7 string *pn,*rn;//定义进程名称数组,资源名称数组
  8 int row,col;//定义未知矩阵的行数和列数,动态输入
  9 
 10 
 11 //------安全性算法-----
 12 void issafe(int row,int col){
 13     int t2,t3,t4;//整型中间变量
 14     string t1;//字符型中间变量
 15     int i,j,g,k;//循环变量i,j,标志位g,k
 16 
 17     //finish标志全部置零
 18     for(i=0;i<row;i++){
 19         finish[i]=0;
 20     }
 21 
 22     //令work向量等于可利用资源向量
 23     for(j=0;j<col;j++){
 24         work[j]=a[j];
 25     }
 26 
 27     g=0;
 28     for(i=0;i<row;i++){
 29         k=0;
 30         //用以判断某类资源的需求是否全部小于可分配资源
 31         for(j=0;j<col;j++){
 32             if(need[i][j]<=work[j])
 33                 {k++;}
 34         }
 35         if(k==col){
 36             for(j=0;j<col;j++){
 37                 work[j]=work[j]+all[i][j];
 38             }
 39 
 40 
 41             //对need矩阵进行排序
 42             for(j=0;j<col;j++){
 43                 t2=need[i][j];
 44                 for(int temp=i;temp>g;temp--){
 45                     need[temp][j]=need[temp-1][j];
 46                 }
 47                 need[g][j]=t2;
 48             }
 49 
 50             //对已分配矩阵进行排序
 51             for(j=0;j<col;j++){
 52                 t3=all[i][j];
 53                 for(int temp=i;temp>g;temp--){
 54                     all[temp][j]=all[temp-1][j];
 55                 }
 56                 all[g][j]=t3;
 57             }
 58             t1=pn[i];
 59 
 60             //对进程名称数组进行排序
 61             for(int temp=i;temp>g;temp--){
 62                 pn[temp]=pn[temp-1];
 63             }
 64             pn[g]=t1;
 65 
 66             finish[i]=1;//置标志位为1
 67 
 68             //对标志位finish进行排序
 69             t4=finish[i];
 70             for(int temp=i;temp>g;temp--){
 71                 finish[temp]=finish[temp-1];
 72             }
 73             finish[g]=t4;
 74             i=g;
 75             g++;
 76         }
 77 
 78     }
 79 
 80     //判断标志位,只要有finish标志位0的进程,结束整个程序,若全为1,则输出安全序列
 81     for(i=0;i<row;i++){
 82         if(finish[i]==0){
 83             cout<<"(不安全!!!!!!!)"<<endl<<"(程序已退出!)<<endl";
 84             exit(1);
 85         }
 86     }
 87     cout<<"(存在安全序列为){";
 88     for(i=0;i<row;i++){
 89         cout<<pn[i]<<",";
 90     }
 91     cout<<"}"<<endl;
 92 }
 93 
 94 
 95 
 96 //------银行家算法-----
 97 void request(int col){
 98     int *py,i,j;//定义t0后进程请求资源的资源数组和循环变量i,j
 99     string px;//资源名变量
100     py=new int[col];//分配数组大小
101     cout<<"(请输入请求资源的进程名)"<<endl;
102     cin>>px;//输入t0后进程请求资源的进程名
103 
104     //寻找请求资源的进程名在进程名数组中的位置
105     for(i=0;i<row;i++){
106         if(px==pn[i]){
107             cout<<"(请依次输入对各类资源的请求数目,数之间以空格隔开)"<<endl;
108             for(j=0;j<col;j++){
109                 cin>>py[j];
110             }
111          break;
112         }
113     }
114 
115     int re1=0,re2=0;//定义标志位,分别用以判断某进程请求的各类资源是否全部小于等于最大需求和可分配资源
116     for(j=0;j<col;j++){
117         if(py[j]<=need[i][j])
118             {re1++;}
119         if(py[j]<=a[j])
120             {re2++;}
121     }
122 
123 
124     //若符合标志位判断标准,对资源进行修改
125     if(re1==col&&re2==col){
126         for(j=0;j<col;j++){
127             need[i][j]=need[i][j]-py[j];
128             a[j]=a[j]-py[j];
129             all[i][j]=all[i][j]+py[j];
130         }
131         issafe(row,col);//对资源重新分配后 调用安全性算法
132     }else{cout<<"(不安全!!!)"<<endl;}
133 
134     delete[] py;//释放
135 }
136 int main()
137 {
138     int i,j,ch;//定义循环变量i,j和判断变量ch(判断是否有进程请求资源)
139     cout<<"(请依次输入进程个数和资源个数(以空格隔开))"<<endl;
140     cin>>row>>col;
141 
142     //以下是对数组或者矩阵分配大小
143     pn=new string[row];//进程名数组
144     rn=new string[col];//资源名数组
145     a=new int[col];//可利用资源数组
146     sum=new int[col];//各类资源总个数
147     finish=new int[row];//各类资源总个数
148     work=new int[col];
149 
150     //动态分配 分配矩阵
151     all=new int*[row];
152     for(i=0;i<row;i++){
153         all[i]=new int[col];
154     }
155 
156     //动态分配最大需求矩阵
157     ma=new int*[row];
158     for(i=0;i<row;i++){
159         ma[i]=new int[col];
160     }
161 
162     //动态分配需求矩阵
163     need=new int*[row];
164     for(i=0;i<row;i++){
165         need[i]=new int[col];
166     }
167 
168 
169     cout<<"(请输入进程名(以空格隔开,按回车键结束))"<<endl;
170     for(i=0;i<row;i++){
171         cin>>pn[i];
172     }
173 
174     cout<<"(请输入资源名(以空格隔开,按回车键结束))"<<endl;
175     for(i=0;i<col;i++){
176         cin>>rn[i];
177     }
178 
179     cout<<"(请输入各类资源的总数量)"<<endl;
180     for(i=0;i<col;i++){
181         cin>>sum[i];
182     }
183 
184 
185     //分配矩阵
186     for(i=0;i<row;i++){
187         cout<<"(请输入进程 "+pn[i]+" 依次分配的资源情况(以空格隔开,按回车键结束))"<<endl;
188         for(j=0;j<col;j++){
189             cin>>all[i][j];
190         }
191     }
192 
193     //最大矩阵
194     for(i=0;i<row;i++){
195         cout<<"(请输入进程 "+pn[i]+" 对各类资源的最大需求(以空格隔开,按回车键结束))"<<endl;
196         for(j=0;j<col;j++){
197             cin>>ma[i][j];
198         }
199     }
200 
201     //需求矩阵
202     for(i=0;i<row;i++){
203         for(j=0;j<col;j++){
204             need[i][j]=ma[i][j]-all[i][j];
205         }
206     }
207 
208 
209     //可利用资源
210     for(j=0;j<row;j++){
211             a[j]=sum[j]-all[0][j];
212         }
213     for(j=0;j<col;j++){
214         for(i=1;i<row;i++){
215             a[j]=a[j]-all[i][j];
216         }
217     }
218 
219     issafe(row,col);//调用安全性算法
220     cout<<"(是否有进程对资源发出请求,是---1,否---0)"<<endl;
221     cin>>ch;
222     if(ch==1){
223         request(col);//调用银行家算法
224     }
225 
226 
227 
228     //释放
229     for(i=0;i<row;i++){
230         delete[] all[i];
231         delete[] ma[i];
232         delete[] need[i];
233     }
234     delete[] all,ma,need,pn,rn,a,sum,work,finish;
235     return 0;
236 }

 

 2019-04-20-20:39:05

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网