1. 程式人生 > >c++實現銀行家演算法

c++實現銀行家演算法

大三上學期的時候自己寫的銀行家演算法的程式碼,複製貼上到編譯器即可執行,希望幫助到其他人!

/*
    Financier algorithm.(銀行家演算法) 
*/ 

//=====<head file>==============================================================
#include<stdio.h>
#include<windows.h>
#include<conio.h>

//=====<data structure>=========================================================
struct systemInformation { int proceeding; //number of preceedings. int resource; //number of resources. int available[6];//six kinds of resources. int maxResource[6];//each max element stand for the max number of one kind of resources . int allocation[10][6];//allocation[i][j]=k stand for proceeding[i] has been allocated k resource[j].
int need[10][6];//need[i][j]=k stands for proceeding[i] need k resource[j] for finishing. int finish[10];//finish[i]=1 => proceeding[i] have been finished . }; struct currentProceedingInformation { int proceedingID; int need[6]; }; //=====<static variable>========================================================
struct systemInformation information; struct currentProceedingInformation informationForCheck; int haveBeenInitializedTag = 0; //=====<function declare>======================================================= char menu();//show menu UI. void drawTable();//to show users the current system information. void defaultInput();//use default information to run this algorithm. void input(); void financier();//financier algorithm int securityCheck();//a subordinate function of financier . //=====<mian function>========================================================== int main() { do { char option = menu(); switch(option) { case'1': { input(); drawTable(); break; } case'2': { defaultInput(); system("cls"); drawTable(); printf("\n[Enter]繼續\n"); getch(); break; } case'3': { ;//nothing break; } case'4': { system("cls"); drawTable(); printf("\n[Enter]繼續\n"); getch(); goto sign; } } system("cls"); financier(); sign:; }while(1); return 0; } //=====<functions>============================================================== char menu() { do { system("cls"); printf(" 銀行家演算法\n"); printf(" ----------------------------\n"); printf("\n"); printf(" (1)---輸入資料\n"); printf("\n"); printf(" (2)---使用預設資料\n"); printf("\n"); if(haveBeenInitializedTag == 1) { printf(" (3)--繼續\n"); printf("\n"); printf(" (4)--顯示系統狀態\n"); printf("\n"); } printf(" (0)---退出程式\n"); printf("\n"); printf(" ----------------------------\n"); printf("\n請選擇:"); char optionTag = getch(); if(optionTag == '0') { system("cls"); printf("\n\n\t\t<歡迎下次使用>\n\n\t\t"); Sleep(1000); exit(0); } else if(optionTag == '1' || optionTag == '2') return optionTag; else if((optionTag == '3' ||optionTag == '4') && haveBeenInitializedTag == 1) return optionTag; else { printf("\n#輸入錯誤#"); Sleep(500); } }while(1); } void defaultInput() { information.proceeding = 5; information.resource = 3; information.available[0] = 3; information.available[1] = 3; information.available[2] = 2; information.maxResource[0] = 10; information.maxResource[1] = 5; information.maxResource[2] = 7; information.allocation[0][0] = 0; information.allocation[0][1] = 1; information.allocation[0][2] = 0; information.allocation[1][0] = 2; information.allocation[1][1] = 0; information.allocation[1][2] = 0; information.allocation[2][0] = 3; information.allocation[2][1] = 0; information.allocation[2][2] = 2; information.allocation[3][0] = 2; information.allocation[3][1] = 1; information.allocation[3][2] = 1; information.allocation[4][0] = 0; information.allocation[4][1] = 0; information.allocation[4][2] = 2; information.need[0][0] = 7; information.need[0][1] = 4; information.need[0][2] = 3; information.need[1][0] = 1; information.need[1][1] = 2; information.need[1][2] = 2; information.need[2][0] = 6; information.need[2][1] = 0; information.need[2][2] = 0; information.need[3][0] = 0; information.need[3][1] = 1; information.need[3][2] = 1; information.need[4][0] = 4; information.need[4][1] = 3; information.need[4][2] = 1; //finish[]=0; for(int i=0;i<5;i++) { information.finish[i] = 0; } haveBeenInitializedTag = 1; } void input() { system("cls"); printf("輸入資訊\n"); printf("---------------------------\n"); printf("\n"); //input proceeding printf("\n請輸入程序的個數:"); int temporary=0; scanf("%d",&temporary); if(temporary<=0||temporary>10) { printf("\n#輸入錯誤,再見#\n"); getch(); exit(0); } else information.proceeding = temporary; printf("\n#以下用 P0-p%d 來表示\n",temporary); //input resource printf("\n請輸入資源的個數:"); scanf("%d",&temporary); if(temporary<=0 ||temporary>6) { printf("\n#輸入錯誤,再見#\n"); getch(); exit(0); } else information.resource = temporary; printf("\n#以下用 A-%c 來表示",temporary+65); //input maxResource for(int i=0;i<information.resource;i++) { printf("\n請輸入%c類資源總數:",i+65); scanf("%d",&temporary); if(temporary<=0) { printf("\n#輸入錯誤,再見#"); getch(); exit(0); } else information.maxResource[i] = temporary; } //input allocation for(int i=0;i<information.proceeding;i++) { for(int j=0;j<information.resource;j++) { printf("\n請輸入P%d 當前已佔有%c 類資源的數目:",i,j+65); if(temporary<=0) { printf("\n#輸入錯誤,再見#"); getch(); exit(0); } else information.allocation[i][j] = temporary; } } //input need for(int i=0;i<information.proceeding;i++) { for(int j=0;j<information.resource;j++) { printf("\n請輸入P%d 當前尚需要%c 類資源的數目:",i,j+65); if(temporary<=0) { printf("\n#輸入錯誤,再見#"); getch(); exit(0); } else information.allocation[i][j] = temporary; } } //finish[]=0; for(int i=0;i<information.proceeding;i++) { information.finish[i] = 0; } haveBeenInitializedTag = 1; } void drawTable() { printf("\n\n當前系統狀態:\n\n"); printf("P :A allication/need ...\n"); for(int i=0;i<information.proceeding;i++) { printf("\n"); printf("P%d: ",i); if(information.finish[i] == 0) { // for(int j=0;j<information.resource;j++) { printf("%c %d/%d ",j+65,information.allocation[i][j],information.need[i][j]); } printf(";"); } else printf("已完成"); } printf("\n系統剩餘: "); for(int i=0;i<information.resource;i++) { printf("%c:%d ",i+65,information.available[i]); } printf("\n"); } void financier() { system("cls"); printf("銀行家演算法\n"); printf("----------------------------\n\n"); fflush(stdin);//empty input block. printf("請輸入要申請資源的程式(如 P1): "); int temporary = -1; char p; scanf("%c%d",&p,&temporary); if(temporary<0||temporary>=information.proceeding) { printf("\n#輸入錯誤,再見#"); Sleep(1000); exit(0); } else informationForCheck.proceedingID = temporary; printf("\n"); for(int i=0;i<information.resource;i++) { printf("請輸入 P%d 要申請 %c 類資源的數目:",informationForCheck.proceedingID,i+65); scanf("%d",&temporary); if(temporary < 0) { printf("\n#輸入錯誤,再見#"); Sleep(1000); exit(0); } else if(temporary > information.need[informationForCheck.proceedingID][i]) { printf("\n#輸入錯誤#\n錯誤資訊: 申請資源量大於該程序需求量\n"); Sleep(1000); exit(0); } else informationForCheck.need[i] = temporary; } if(1 == securityCheck())//the request can be satisfied . { for(int i=0;i<information.resource;i++)//allocate { //refresh need information.need[informationForCheck.proceedingID][i] = information.need[informationForCheck.proceedingID][i] - informationForCheck.need[i]; //refresh allocation information.allocation[informationForCheck.proceedingID][i] = information.allocation[informationForCheck.proceedingID][i]+informationForCheck.need[i]; //refresh available information.available[i] = information.available[i] - informationForCheck.need[i]; } } else { ;//nothing. } } int securityCheck() { system("cls"); printf("\n安全性檢查\n"); printf("----------------------------\n"); struct systemInformation copyInformation = information,temporaryInformation;//to protect original data. //STEP ONE:experimental allocate for(int i=0;i<copyInformation.resource;i++) { //refresh need copyInformation.need[informationForCheck.proceedingID][i] = copyInformation.need[informationForCheck.proceedingID][i] - informationForCheck.need[i]; //refresh allocation copyInformation.allocation[informationForCheck.proceedingID][i] = copyInformation.allocation[informationForCheck.proceedingID][i]+informationForCheck.need[i]; //refresh available copyInformation.available[i] = copyInformation.available[i] - informationForCheck.need[i]; } printf("\n\n第一步:試探性分配"); //temporaryInformation <- information <- copyInformation --- to drawTable temporaryInformation = information; information = copyInformation; drawTable(); //copyInformation <- information <- temporaryInformation -- return to the original condition. copyInformation = information; information = temporaryInformation; printf("\n[Enter]下一步\n"); getch(); //STEP TWO:to check whether all of proceedings can finish . system("cls"); printf("\n安全性檢查\n"); printf("----------------------------\n"); printf("\n\n第二步:檢測是否所有程序都能夠完成.\n"); int haveUnfinishedProceedingTag = 0;//Are there unfinished proceedings ? int changeTag = 0;//whether finishTag has changed. int eachSatisfiedTag = 0;//eachSatisfiedTag = resource => changeTag = 1; for(int i=0;i<copyInformation.proceeding;i++)//scan proceedings for those times. { //set variables in order changeTag = 0; eachSatisfiedTag = 0; for(int j=0;j<copyInformation.proceeding;j++)//scan all proceedings each time. { if(copyInformation.finish[j] == 0)//this proceeding is unfinished now . { eachSatisfiedTag = 0; //if(need:<=available) for(int k=0;k<copyInformation.resource;k++) { if(copyInformation.need[j][k] <= copyInformation.available[k]) eachSatisfiedTag++; } if(eachSatisfiedTag == copyInformation.resource)//this proceeding can finish. { copyInformation.finish[j] = 1;//to record its finish . changeTag = 1;//finish[] was changed . //then refresh copyInformation for(int k=0;k<copyInformation.resource;k++)//retrieve its resource . { copyInformation.available[k] = copyInformation.available[k] + copyInformation.allocation[j][k]; } break; }//if-finish }//if }//for-eachTime. //whether are all proceedings finished ? haveUnfinishedProceedingTag = 1; for(int k=0;k<copyInformation.proceeding;k++)//have unfinished proceedings ? { if(copyInformation.finish[k] == 0) { haveUnfinishedProceedingTag = 0;//yes break; } } if(haveUnfinishedProceedingTag == 0 && changeTag == 0)//have no way to continue running. { printf("\n無法完成分配!\n"); getch(); return 0; } else { //temporaryInformation <- information <- copyInformation --- to drawTable temporaryInformation = information; information = copyInformation; drawTable(); //copyInformation <- information <- temporaryInformation -- return to the original condition. copyInformation = information; information = temporaryInformation; printf("\n[Enter]繼續\n"); getch(); } }//for-scan system("cls"); printf("\n所有程式都可以執行完成!\n"); Sleep(500); printf("\n#系統為安全狀態,可以分配#"); Sleep(500); printf("\n\n[Enter]完成"); getch(); return 1; }