自己寫的銀行家算法代碼,界面好看點,代碼還沒完善。
#include <iostream>
#include <windows.h>
#include <stdlib.h>
const unsigned short PROCESS_COUNT=5;//程序數。
const unsigned short SORT_COUNT=3;//資源種類。 這個不能改動!
const unsigned short TITLE_SIZE=12;
const unsigned short THE_LIST_NAME=11;
const unsigned short INFO_SIZE=45;
#define NEED_ERROR 0
#define AVAILABLE_ERROR 1
int Flag[PROCESS_COUNT]={0};
int TheList[PROCESS_COUNT][SORT_COUNT]={0};
int Allocation[PROCESS_COUNT][SORT_COUNT]={0};
int Need[PROCESS_COUNT][SORT_COUNT]={0};
int Available[3]={0};
char cTitle[TITLE_SIZE];
char cTheListName[THE_LIST_NAME];
char info[INFO_SIZE]={'/0'};
void ShowInfoForm();//顯示資源資訊
void InitForm(char);
void TryToAlloc(int,int*);
bool SecurityAnalysis(int);
int BankCheck(int iProc,int*Request);
/*_____________________________________________________________________________________________*/
int main()
{
strcpy(cTitle,"資源配置設定表");
strcpy(cTheListName," TheMax ");
strcpy(info,"程式初始化,請輸入各項數值。 ");
ShowInfoForm();
//ShowInfoForm(Flag,cTitle,cTheListName,TheList,Allocation,Need,Available,info);
std::cout<<" 請輸入可用資源最大值,按X X X格式:";
for (int i=0;i<SORT_COUNT;i++)
{
int temp=0;
std::cin>>temp;
if (0<=temp&&temp<10)
{
Available[i]=temp;
}
else
{
std::cout<<"請輸入0-10之間的數/n";
i--;
}
}
ShowInfoForm(/*Flag,cTitle,cTheListName,TheList,Allocation,Need,Available,info*/);
InitForm('M');
InitForm('A');
std::cout<<" 請輸入發起資源申請的程序号(0-"<<PROCESS_COUNT-1<<"):";
int iProc=0;
std::cin>>iProc;
if (0>iProc||iProc>PROCESS_COUNT)std::cout<<"無此程序!/n";
std::cout<<" 請輸入申請資源的數量,按X X X格式:";
int Request[3]={0};
for (int i=0;i<3;i++)
{
int temp=0;
std::cin>>temp;
if (0<=temp&&temp<10)
Request[i]=temp;
}
if (NEED_ERROR==BankCheck(iProc,Request))
strcpy(info,"輸入的需求值Need不對! ");
if (AVAILABLE_ERROR==BankCheck(iProc,Request))
strcpy(info,"輸入的已配置設定值Allocation不對! ");
ShowInfoForm();
return 0;
}
/*_____________________________________________________________________________________________*/
int BankCheck(int iProc,int*Request)
{
for (int i=0;i<3;i++)
{
if (Request[i]>Need[iProc][i])
return NEED_ERROR;
if (Request[i]>Available[i])
return AVAILABLE_ERROR;
}
//嘗試配置設定
TryToAlloc(iProc,Request);
}
/*_____________________________________________________________________________________________*/
void TryToAlloc(int iProc,int*tempRequest)
{
//儲存原來資料:
int oldAvailable[3]={0};
int oldAllocation[PROCESS_COUNT][SORT_COUNT]={0};
int oldNeed[PROCESS_COUNT][SORT_COUNT]={0};
for (int i=0;i<3;i++)
{
oldAvailable[i]=Available[i];
for (int x=0;x<PROCESS_COUNT;x++)
{
oldAllocation[x][i]=Allocation[x][i];
oldNeed[x][i]=Need[x][i];
}
}
//更新Available Allocation[],Need[]
for (int i=0;i<3;i++)
{
Available[i]-=tempRequest[i];
Allocation[iProc][i]+=tempRequest[i];
Need[iProc][i]-=tempRequest[i];
}
strcpy(cTitle,"安全性分析");
strcpy(info,"嘗試配置設定中... ");
ShowInfoForm();
//安全性分析
if (false==SecurityAnalysis(iProc))
{//不安全,資料複原。
for (int i=0;i<3;i++)
{
Available[i]=oldAvailable[i];
for (int x=0;x<PROCESS_COUNT;x++)
{
Allocation[x][i]=oldAllocation[x][i];
Need[x][i]=oldNeed[x][i];
}
}
Sleep(1500);
strcpy(info,"此次資源申請不能通過安全性檢查,資料恢複! ");
ShowInfoForm();
}
}
/*_____________________________________________________________________________________________*/
bool SecurityAnalysis(int iproc)
{
strcpy(cTheListName," Work ");
ShowInfoForm();
bool finish[PROCESS_COUNT];
int safelist[PROCESS_COUNT];
for (int i=0;i<PROCESS_COUNT;i++)
{
finish[i]=false;
safelist[i]=0;
}
int sl=0;
for (int x=0;x<SORT_COUNT;x++)
{
int y=iproc;
while (y<PROCESS_COUNT)
{
if (finish[y]==false&&Need[y][x]<=Available[x])
{
Available[x]=Available[x]+Allocation[y][x];
finish[y]=true;
Flag[y]=1;
safelist[sl]=y;
sl++;
y=0;
ShowInfoForm();
Sleep(1000);
}
else
{
y++;
}
}
}
for (int i=0;i<PROCESS_COUNT;i++)
if (finish[i]==false)return false;
strcpy(info,"通過安全性檢查! ");
std::cout<<" 安全序列:";
for (int i=0;i<PROCESS_COUNT;i++)
{
std::cout<<safelist[i];
}
// ShowInfoForm();
return true;
}
/*_____________________________________________________________________________________________*/
void InitForm(char NameFlag)
{
int (*temp)[SORT_COUNT]=NULL;
int itemp=0;
int AvaFlag=0;
char infoName[11]={'/0'};
switch (NameFlag)
{
case 'M':
temp=TheList;
strcpy(infoName,"TheMax");
break;
case 'A':
AvaFlag=1;
temp=Allocation;
strcpy(infoName,"Allocation");
break;
default:
return;
}
for (int x=0;x<PROCESS_COUNT;x++)
{
for (int y=0;y<SORT_COUNT;y++)
{
std::cout<<" 請輸入程序["<<x<<"]."<<infoName<<"的資料,按X X X的格式:";
std::cin>>itemp;
if (0<=itemp&&itemp<10)
{
temp[x][y]=itemp;
if (AvaFlag==1)
{
Available[y]-=temp[x][y];
Need[x][y]=TheList[x][y]-Allocation[x][y];
if (Need[x][y]<0)Need[x][y]=0;
}
if (Available[y]<0)
std::cout<<"/nError!";
}
else
{
strcpy(info,"請輸入大于0,小于10的整數! ");
y--;
}
ShowInfoForm();
}
}
}
/*_____________________________________________________________________________________________*/
void ShowInfoForm()
{
system("cls");
cout<<endl<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 銀行家算法 Software1083::cheyoca@2009-10-13 ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ [ "<<cTitle<<" ] ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ 資源 ┃"<<cTheListName<<"┃Allocation┃ Need ┃ "<<endl;
cout<<" ┃ 程序 ┃ A B C ┃ A B C ┃ A B C ┃ "<<endl;
for (int i=0;i<PROCESS_COUNT;i++)
{
cout<<" ┃ "<<"Process["<<i;
if (Flag[i]==0)cout<<"] ";
else cout<<"]-";
cout<<"┃ "<<TheList[i][0]<<" "<<TheList[i][1]<<" "<<TheList[i][2];
cout<<" ┃ "<<Allocation[i][0]<<" "<<Allocation[i][1]<<" "<<Allocation[i][2];
cout<<" ┃ "<<Need[i][0]<<" "<<Need[i][1]<<" "<<Need[i][2]<<" ┃ "<<endl;
}
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ Available: ┃ A:"<<Available[0]<<" B:"<<Available[1];
cout<<" C: "<<Available[2]<<" ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃Info:"<<info<<"┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
}
/*
void ShowSecurityInfo()
{
cout<<endl<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 銀行家算法 Software1083::cheyoca@2009-10-13 ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ [ 安全性分析 ] ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ 資源 ┃ TheMax ┃Allocation┃ Need ┃ "<<endl;
cout<<" ┃ 程序 ┃ A B C ┃ A B C ┃ A B C ┃ "<<endl;
cout<<" ┃ Process[1] ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ "<<endl;
cout<<" ┃ Process[1] ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ "<<endl;
cout<<" ┃ Process[1] ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ "<<endl;
cout<<" ┃ Process[1] ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ "<<endl;
cout<<" ┃ Process[1] ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ "<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃ Available: ┃ A:0 B:0 C: 0 ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃Info: 進入安全性檢測... ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;*/
/*
cout<<endl<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 銀 行 家 算 法 Software1083::cheyoca@2009-10-13 ┃"<<endl;
cout<<" ┠ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ┨"<<endl;
cout<<" ┃ 銀 行 家 算 法 [ 安全性分析 ] ┃"<<endl;
cout<<" ┠ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ┨"<<endl;
cout<<" ┃ 資源 ┃ Work ┃Allocation┃ Need ┃ Wor+Ava ┃"<<endl;
cout<<" ┃ 程序 ┃ A B C ┃ A B C ┃ A B C ┃ A B C ┃"<<endl;
cout<<" ┃Process[1]√┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃"<<endl;
cout<<" ┃Process[1]√┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃"<<endl;
cout<<" ┃Process[1]√┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃"<<endl;
cout<<" ┃Process[1]√┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃"<<endl;
cout<<" ┃Process[1]√┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃ 0 0 0 ┃"<<endl;
cout<<" ┠━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┨"<<endl;
cout<<" ┃Info: 安全序列:1 -> 3 -> 2 -> 5 ->4 OK! ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
}*/