#include<stdio.h>
#include<stdlib.h>
#include<iostream>
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 **********************"<<endl;
for(int i=0;i<n;i++)
{
cout<<Available[i]<<" ";
}
cout<<endl;
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void init_bank(Bank *p)
{
int x,y,z;
cout<<"Please input Max 、Allocation and Need:"<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;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 ***********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Max[i][j]<<" ";
if(j == M-1)
{
cout<<" ";
cout<<"the process["<<i<<"]is finished:"<<p->is_finished[i];
cout<<endl;
}
ch=ch+1;
if(k == M)
{
ch = 'A';
k=0;
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void Print_Bank_Allocation(Bank *p)
{
cout<<"********************** Allocation *********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Allocation[i][j]<<" ";
if(j == M-1)
{
cout<<endl;
}
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void Print_Bank_Need(Bank *p)
{
cout<<"********************** Need **********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Need[i][j]<<" ";
if(j == M-1)
{
cout<<endl;
}
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
bool Check_safe(Bank *bk,int *a)
{
int i,j,k=0,m,sign;
int flag ;
for(m=0;m<M;m++)
{
for(i=0;i<N;i++)
{
flag = 1;
for(j=0;j<M;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<M;j++)
{/
Available[j] = Available[j] + bk->Allocation[i][j];
}
}
}
int tag = 1;
for(j=0;j<N && i<=N;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:"<<endl;
int t,i;
for( i=0;i<M;i++)
{
cin>>t;
if(t > bk->Need[index][i] || t > Available2[i])
{
cout<<"apply > Need or apply > Available!"<<endl;
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<N;i++)
{
if(bk->is_finished[i] == true)
{
bk->is_finished[i] = false;
}
}
for(i=0;i<M;i++)
{
Available[i] = Available2[i];
}
return true;
}
void retrieve(Bank *bk,int *tmp,int index)
{
int i;
for(i=0;i<M;i++)
{
Available2[i] +=tmp[i];
bk->Allocation[index][i] -= tmp[i];
bk->Need[index][i] += tmp[i];
}
}
bool Request(Bank *bk)
{
cout<<" "<<endl;
cout<<"****** Welcome to the page check and request ******"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
while(1)
{
int index=-1,tmp[M],a[N]={0};
cout<<endl;
cout<<"please input the number of request:"<<endl;
cin>>index;
if(index == -1)
{
exit(0);
}
if(index > N || index < -1)
{
cout<<"no this process"<<endl;
continue;
}
bool g = try_allocation(bk,tmp,index);
if(!g)
{
continue;
}
cout<<"***************************** the update request is followed: ******************"<<endl;
Print(Available2,M);
cout<<endl;
Print_Bank_Max(bk);
cout<<endl;
Print_Bank_Allocation(bk);
cout<<endl;
Print_Bank_Need(bk);
cout<<endl;
bool f = Check_safe(bk,a);
// cout<<"********************** distrubution and Available **********************"<<endl;
// Print(Available,M);
if(f)
{
cout<<"the safe order is followed:"<<endl;
for(int i=0;i<N;i++)
{
cout<<a[i]<<" ";
}
// retrieve(bk,tmp,index);
/*
for(i=0;i<M;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!"<<endl;
retrieve(bk,tmp,index);
for(int i=0;i<M;i++)
{
Available[i] = Available2[i];
}
}
}
}
int main()
{
Bank bk;
int i,x,a[N];
cout<<"Please input the number Available:"<<endl;
for(i=0;i<M;i++)
{
cin>>x;
Available[i] = x;
}
for(i=0;i<M;i++)
{
Available2[i] = Available[i];
}
init_bank(&bk);
Print(Available,M);
cout<<endl;
Print_Bank_Max(&bk);
cout<<endl;
Print_Bank_Allocation(&bk);
cout<<endl;
Print_Bank_Need(&bk);
cout<<endl;
bool f = Check_safe(&bk,a);
cout<<"********************** distrubution and Available **********************"<<endl;
Print(Available,M);
if(f)
{
cout<<"the safe order is followed:"<<endl;
for(int i=0;i<N;i++)
{
cout<<a[i]<<" ";
}
}
else
{
cout<<"the safe order not find!"<<endl;
}
for(i=0;i<M;i++)
{
Available[i]=Available2[i];
}
Request(&bk);
return 0;
}
#include<stdlib.h>
#include<iostream>
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 **********************"<<endl;
for(int i=0;i<n;i++)
{
cout<<Available[i]<<" ";
}
cout<<endl;
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void init_bank(Bank *p)
{
int x,y,z;
cout<<"Please input Max 、Allocation and Need:"<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;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 ***********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Max[i][j]<<" ";
if(j == M-1)
{
cout<<" ";
cout<<"the process["<<i<<"]is finished:"<<p->is_finished[i];
cout<<endl;
}
ch=ch+1;
if(k == M)
{
ch = 'A';
k=0;
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void Print_Bank_Allocation(Bank *p)
{
cout<<"********************** Allocation *********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Allocation[i][j]<<" ";
if(j == M-1)
{
cout<<endl;
}
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
void Print_Bank_Need(Bank *p)
{
cout<<"********************** Need **********************"<<endl;
char ch = 'A';
int k=1;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cout<<ch<<':';
cout<<p->Need[i][j]<<" ";
if(j == M-1)
{
cout<<endl;
}
ch=ch+1;
if(k == M)
{
k=0;
ch = 'A';
}
k++;
}
}
cout<<"** ** ** *** ** ** ** ** **"<<endl;
}
bool Check_safe(Bank *bk,int *a)
{
int i,j,k=0,m,sign;
int flag ;
for(m=0;m<M;m++)
{
for(i=0;i<N;i++)
{
flag = 1;
for(j=0;j<M;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<M;j++)
{/
Available[j] = Available[j] + bk->Allocation[i][j];
}
}
}
int tag = 1;
for(j=0;j<N && i<=N;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:"<<endl;
int t,i;
for( i=0;i<M;i++)
{
cin>>t;
if(t > bk->Need[index][i] || t > Available2[i])
{
cout<<"apply > Need or apply > Available!"<<endl;
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<N;i++)
{
if(bk->is_finished[i] == true)
{
bk->is_finished[i] = false;
}
}
for(i=0;i<M;i++)
{
Available[i] = Available2[i];
}
return true;
}
void retrieve(Bank *bk,int *tmp,int index)
{
int i;
for(i=0;i<M;i++)
{
Available2[i] +=tmp[i];
bk->Allocation[index][i] -= tmp[i];
bk->Need[index][i] += tmp[i];
}
}
bool Request(Bank *bk)
{
cout<<" "<<endl;
cout<<"****** Welcome to the page check and request ******"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
while(1)
{
int index=-1,tmp[M],a[N]={0};
cout<<endl;
cout<<"please input the number of request:"<<endl;
cin>>index;
if(index == -1)
{
exit(0);
}
if(index > N || index < -1)
{
cout<<"no this process"<<endl;
continue;
}
bool g = try_allocation(bk,tmp,index);
if(!g)
{
continue;
}
cout<<"***************************** the update request is followed: ******************"<<endl;
Print(Available2,M);
cout<<endl;
Print_Bank_Max(bk);
cout<<endl;
Print_Bank_Allocation(bk);
cout<<endl;
Print_Bank_Need(bk);
cout<<endl;
bool f = Check_safe(bk,a);
// cout<<"********************** distrubution and Available **********************"<<endl;
// Print(Available,M);
if(f)
{
cout<<"the safe order is followed:"<<endl;
for(int i=0;i<N;i++)
{
cout<<a[i]<<" ";
}
// retrieve(bk,tmp,index);
/*
for(i=0;i<M;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!"<<endl;
retrieve(bk,tmp,index);
for(int i=0;i<M;i++)
{
Available[i] = Available2[i];
}
}
}
}
int main()
{
Bank bk;
int i,x,a[N];
cout<<"Please input the number Available:"<<endl;
for(i=0;i<M;i++)
{
cin>>x;
Available[i] = x;
}
for(i=0;i<M;i++)
{
Available2[i] = Available[i];
}
init_bank(&bk);
Print(Available,M);
cout<<endl;
Print_Bank_Max(&bk);
cout<<endl;
Print_Bank_Allocation(&bk);
cout<<endl;
Print_Bank_Need(&bk);
cout<<endl;
bool f = Check_safe(&bk,a);
cout<<"********************** distrubution and Available **********************"<<endl;
Print(Available,M);
if(f)
{
cout<<"the safe order is followed:"<<endl;
for(int i=0;i<N;i++)
{
cout<<a[i]<<" ";
}
}
else
{
cout<<"the safe order not find!"<<endl;
}
for(i=0;i<M;i++)
{
Available[i]=Available2[i];
}
Request(&bk);
return 0;
}