银行家算法

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<stdlib.h>

#include<iomanip>

#include<conio.h>

usingnamespacestd;

constintMAX_P=20;

constintMAXA=10;//定义A类资源的数量

constintMAXB=5;

constintMAXC=7;

typedefstructnode{

inta;

intb;

intc;

intremain_a;

intremain_b;

intremain_c;

}bank;

typedefstructnode1{

charname[20];

inta;

intb;

intc;

intneed_a;

intneed_b;

intneed_c;

}process;

bankbanker;

processprocesses[MAX_P];

intquantity;

//初始化函数

voidinitial()

{

inti;

banker.a=MAXA;

banker.b=MAXB;

banker.c=MAXC;

banker.remain_a=MAXA;

banker.remain_b=MAXB;

banker.remain_c=MAXC;

for(i=0;i<MAX_P;i++){

strcpy(processes[i].name,"");

processes[i].a=0;

processes[i].b=0;

processes[i].c=0;

processes[i].need_a=0;

processes[i].need_b=0;

processes[i].need_c=0;

}

}

//新加作业

voidadd()

{

charname[20];

intflag=0;

intt;

intneed_a,need_b,need_c;

inti;

cout<<endl;

cout<<"新加作业"<<endl;

cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;

cout<<"请输入新加作业名:";

cin>>name;

for(i=0;i<quantity;i++){

if(!strcmp(processes[i].name,name)){

flag=1;

break;

}

}

if(flag){

cout<<"错误,作业已存在"<<endl;

}

else{

cout<<"本作业所需A类资源:";

cin>>need_a;

cout<<"本作业所需B类资源:";

cin>>need_b;

cout<<"本作业所需C类资源:";

cin>>need_c;

t=1;

cout<<need_a<<banker.remain_a;

if(need_a>banker.remain_a){

cout<<"错误,所需A类资源大于银行家所剩A类资源"<<endl;

t=0;

}

if(need_b>banker.remain_b){

cout<<"错误,所需B类资源大于银行家所剩B类资源"<<endl;

t=0;

}

if(need_c>banker.remain_c){

cout<<"错误,所需C类资源大于银行家所剩C类资源"<<endl;

t=0;

}

if(t){

strcpy(processes[quantity].name,name);

processes[quantity].need_a=need_a;

processes[quantity].need_b=need_b;

processes[quantity].need_c=need_c;

quantity++;

cout<<"新加作业成功"<<endl;

}

else{

cout<<"新加作业失败"<<endl;

}

}

}

//为作业申请资源

voidbid()

{

charname[20];

inti,p;

inta,b,c;

intflag;

cout<<endl<<"为作业申请资源"<<endl;

cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;

cout<<"要申请资源的作业名:";

cin>>name;

p=-1;

for(i=0;i<quantity;i++){

if(!strcmp(processes[i].name,name)){

p=i;

break;

}

}

if(p!=-1){

cout<<"该作业要申请A类资源数量:";

cin>>a;

cout<<"该作业要申请B类资源数量:";

cin>>b;

cout<<"该作业要申请C类资源数量:";

cin>>c;

flag=1;

if((a>banker.remain_a)||(a>processes[p].need_a-processes[p].a)){

cout<<"错误,所申请A类资源大于银行家所剩A类资源或该进程还需数量"<<endl;

flag=0;

}

if((b>banker.remain_b)||(b>processes[p].need_b-processes[p].b)){

cout<<"错误,所申请B类资源大于银行家所剩B类资源或该进程还需数量"<<endl;

flag=0;

}

if((c>banker.remain_c)||(c>processes[p].need_c-processes[p].c)){

cout<<"错误,所申请C类资源大于银行家所剩C类资源或该进程还需数量"<<endl;

flag=0;

}

if(flag){

banker.remain_a-=a;

banker.remain_b-=b;

banker.remain_c-=c;

processes[p].a+=a;

processes[p].b+=b;

processes[p].c+=c;

cout<<"为作业申请资源成功"<<endl;

}

else{

cout<<"为作业申请资源失败"<<endl;

}

}

else{

cout<<"该作业不存在"<<endl;

}

}

//撤消作业

voidfinished()

{

charname[20];

inti,p;

cout<<endl<<"撤消作业"<<endl;

cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;

cout<<"要撤消作业名:";

cin>>name;

p=-1;

for(i=0;i<quantity;i++){

if(!strcmp(processes[i].name,name)){

p=i;

break;

}

}

if(p!=-1){

banker.remain_a+=processes[p].a;

banker.remain_b+=processes[p].b;

banker.remain_c+=processes[p].c;

for(i=p;i<quantity-1;i++){

processes[i]=processes[i+1];

}

strcpy(processes[quantity-1].name,"");

processes[quantity-1].a=0;

processes[quantity-1].b=0;

processes[quantity-1].c=0;

processes[quantity-1].need_a=0;

processes[quantity-1].need_b=0;

processes[quantity-1].need_c=0;

quantity--;

cout<<"撤消作业成功"<<endl;

}

else{

cout<<"撤消作业失败"<<endl;

}

}

//查看资源情况

voidview()

{

inti;

cout<<endl<<"查看资源情况"<<endl;

cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;

cout<<"银行家所剩资源(剩余资源/总共资源)"<<endl;

cout<<"A类:"<<banker.remain_a<<"/"<<banker.a;

cout<<"B类:"<<banker.remain_b<<"/"<<banker.b;

cout<<"C类:"<<banker.remain_c<<"/"<<banker.c;

cout<<endl<<endl<<"作业占用情况(已占用资源/所需资源)"<<endl<<endl;

if(quantity>0){

for(i=0;i<quantity;i++){

cout<<"作业名:"<<processes[i].name<<endl;

cout<<"A类:"<<processes[i].a<<"/"<<processes[i].need_a;

cout<<"B类:"<<processes[i].b<<"/"<<processes[i].need_b;

cout<<"C类:"<<processes[i].c<<"/"<<processes[i].need_c;

cout<<endl;

}

}

else{

cout<<"当前没有作业"<<endl;

}

}

//显示版权信息函数

voidversion()

{

cout<<endl<<endl;

cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;

cout<<"┃银行家算法┃"<<endl;

cout<<"┠———————————————————————┨"<<endl;

cout<<"┃(c)AllRightReservedNeo┃"<<endl;

cout<<"┃[email protected]┃"<<endl;

cout<<"┃version2004build1122┃"<<endl;

cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;

cout<<endl<<endl;

}

intmain(void)

{

intchioce;

intflag=1;

initial();

version();

while(flag){

cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;

cout<<"1.新加作业2.为作业申请资源3.撤消作业"<<endl;

cout<<"4.查看资源情况0.退出系统"<<endl;

cout<<"请选择:";

cin>>chioce;

switch(chioce){

case1:

add();

break;

case2:

bid();

break;

case3:

finished();

break;

case4:

view();

break;

case0:

flag=0;

break;

default:

cout<<"选择错误"<<endl<<endl;

}

}

return0;

}

相关推荐