# C语言实现银行家算法

#include
#include
#include
using namespace std;

#define M 2
#define N 3

#define false 0
#define true 1

int Available[M];

int Available2[M];

typedef struct _Bank
{
int Max[N][M];
int Allocation[N][M];
int Need[N][M];
bool is_finished[N];
}Bank;

void Print(int *a,int n)
{
cout<<"********************** Available **********************"< for(int i=0;i {
cout< }
cout< cout<<"**      **      **      ***    **     **    **   **   **"< }

void init_bank(Bank *p)
{
int x,y,z;
cout<<"Please input Max 、Allocation and Need:"< for(int i=0;i {
for(int j=0;j {
cin>>x;
cin>>y;
cin>>z;
p->Max[i][j] = x;
p->Allocation[i][j] = y;
p->Need[i][j] = z;
p->is_finished[i] = false;
}
}
}

void Print_Bank_Max(Bank *p)
{
cout<<"**********************   Max    ***********************"< char ch = 'A';
int k=1;
for(int i=0;i {
for(int j=0;j {
cout< cout<Max[i][j]<<" ";
if(j == M-1)
{
cout<<"        ";
cout<<"the process["<is_finished[i];
cout< }
ch=ch+1;

if(k == M)
{
ch = 'A';
k=0;
}
k++;
}
}
cout<<"**      **      **      ***    **     **    **   **   **"< }

void Print_Bank_Allocation(Bank *p)
{
cout<<"********************** Allocation *********************"< char ch = 'A';
int k=1;
for(int i=0;i {
for(int j=0;j {
cout< cout<Allocation[i][j]<<" ";
if(j == M-1)
{
cout< }
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"**      **      **      ***    **     **    **   **   **"< }
void Print_Bank_Need(Bank *p)
{
cout<<"**********************  Need     **********************"< char ch = 'A';
int k=1;
for(int i=0;i {
for(int j=0;j {
cout< cout<Need[i][j]<<" ";
if(j == M-1)
{
cout< }
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"**      **      **      ***    **     **    **   **   **"< }

bool Check_safe(Bank *bk,int *a)
{
int i,j,k=0,m,sign;
int flag ;
for(m=0;m {
for(i=0;i {
flag = 1;
for(j=0;j {
if(bk->is_finished[i] == false && bk->Need[i][j] > Available[j])//break
{
flag = 0;
break;
}
}
if(flag && i != N  && bk->is_finished[i] == false)
{
a[k++] = i;
bk->is_finished[i] = true;
for(j=0;j {/
Available[j] = Available[j] + bk->Allocation[i][j];
}
}
}
int tag = 1;
for(j=0;j {
if(bk->is_finished[j] == false  && i == N)
{
tag = 0;
}
}
if(tag == 0)
{
sign = 0;
}
else
{
sign = 1;
}
}
if(sign == 1)
return true;
else
return false;
}

bool try_allocation(Bank *bk,int *tmp,int index)
{

cout<<"please input the request resource:"< int t,i;
for( i=0;i {
cin>>t;
if(t > bk->Need[index][i] || t > Available2[i])
{
cout<<"apply > Need or apply > Available!"< return false;
}
tmp[i] = t;
Available2[i]-=tmp[i];
bk->Allocation[index][i] += tmp[i];
bk->Need[index][i] -=tmp[i];
if(bk->Need[index][i] == 0)//
{
Available2[i] += bk->Allocation[index][i];
bk->Allocation[index][i] = 0;
}
}
for(i=0;i {
if(bk->is_finished[i] == true)
{
bk->is_finished[i] = false;
}
}
for(i=0;i {
Available[i] = Available2[i];
}
return true;
}

void retrieve(Bank *bk,int *tmp,int index)
{
int i;
for(i=0;i {
Available2[i] +=tmp[i];
bk->Allocation[index][i] -= tmp[i];
bk->Need[index][i] += tmp[i];
}
}

bool Request(Bank *bk)
{
cout<<"                                                                      "< cout<<"******  Welcome to the page check  and request  ******"< cout<<"                                                                      "< cout<<"                                                                      "<
while(1)
{
int index=-1,tmp[M],a[N]={0};
cout< cout<<"please input the number of request:"< cin>>index;
if(index == -1)
{
exit(0);
}
if(index > N  || index < -1)
{
cout<<"no this process"< continue;
}

bool g = try_allocation(bk,tmp,index);
if(!g)
{
continue;
}

cout<<"*****************************      the update request is followed: ******************"<
Print(Available2,M);
cout< Print_Bank_Max(bk);
cout< Print_Bank_Allocation(bk);
cout< Print_Bank_Need(bk);
cout<

bool f = Check_safe(bk,a);
//  cout<<"********************** distrubution and  Available **********************"<      // Print(Available,M);
if(f)
{
cout<<"the safe  order is followed:"< for(int i=0;i {
cout< }

// retrieve(bk,tmp,index);
/*
for(i=0;i {
Available2[i] -=tmp[i];
// bk->Need[index][i] -= tmp[i];
bk->Allocation[index][i] += tmp[i];
Available[i] =Available2[i];
// bk->Allocation[index][i] -=tmp[i];
}
*/

}
else
{
cout<<"the safe  order not find!"< retrieve(bk,tmp,index);
for(int i=0;i {
Available[i] = Available2[i];
}
}

}

}

int main()
{
Bank bk;
int i,x,a[N];
cout<<"Please input the number Available:"< for(i=0;i {
cin>>x;
Available[i] = x;
}
for(i=0;i {
Available2[i] = Available[i];
}
init_bank(&bk);
Print(Available,M);
cout< Print_Bank_Max(&bk);
cout< Print_Bank_Allocation(&bk);
cout< Print_Bank_Need(&bk);
cout<
bool f = Check_safe(&bk,a);
cout<<"********************** distrubution and  Available **********************"<     Print(Available,M);
if(f)
{
cout<<"the safe  order is followed:"< for(int i=0;i {
cout< }
}
else
{
cout<<"the safe  order not find!"< }
for(i=0;i {
Available[i]=Available2[i];
}
Request(&bk);
return 0;
}