银行家算法的数组方式实现

来源:互联网 发布:haproxy acl 端口复用 编辑:程序博客网 时间:2024/06/05 02:54
#include<stdio.h>
int sounumber;
int source[100];//银行家最多有100种资源
int need[100][100];//前100代表顾客最多100个。后100代表最多对100种资源的需求
int inneed[100];
int request[100][100];//同上
int customer=0;
int requestsum[100];//100种资源各自的求和
char finish[100][100];
int control;
int deleteto;
int i=0;
void change()
{
int ch;
do{
printf("tap 1:add source;tap 2:delete source;tap 3:change source;tap 4:stop change\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
sounumber++;
printf("please input the quantity of source%d \n",sounumber);
scanf("%d",&source[sounumber-1]);
for( i=0;i<customer;i++)
{
printf("please input the customer%d's need of source%d \n",i+1,sounumber);
scanf("%d",&need[i][sounumber-1]);
if(need[i][sounumber-1]>source[sounumber-1])
{
printf("can not satisfy,please input less than %d.\n",source[sounumber-1]);
scanf("%d",&need[i][sounumber-1]);
}
printf("please input the every customer%d's first-request of source%d \n",i+1,sounumber);
scanf("%d",&request[i][sounumber-1]);
requestsum[sounumber-1]+=request[i][sounumber-1];
if(request[i][sounumber-1]>need[i][sounumber-1]||requestsum[sounumber-1]>source[sounumber-1])
{
printf("can not satisfy,please input less.\n");
scanf("%d",&request[i][sounumber-1]);
}
}
break;
case 2:
printf("please input which source you want to delete:\n");
//int deleteto;
scanf("%d",&deleteto);
for(i=deleteto-1;i<sounumber-1;i++)
{
source[i]=source[i+1];
requestsum[i]=requestsum[i+1];
}
for( i=0;i<customer;i++)
{
int ja;
for(ja=deleteto-1;ja<sounumber-1;ja++)
{
need[i][ja]=need[i][ja+1];
request[i][ja]=request[i][ja+1];
}
}
sounumber--;
break;
case 3:
printf("please input which source you want to change:\n");
int changeto;
scanf("%d",&changeto);
printf("please input the number of source%d you want to change:\n",changeto);
scanf("%d",&source[changeto-1]);
printf("succeed to change!\n");
break;
case 4:
break;
}
}while(ch!=4);
}
void service()
{
int p=0;
for(p=0;p<sounumber;p++)//以每个资源为单位查找
{
int leave[100];
leave[p]=source[p]-requestsum[p];
int j=0;
for(j=0;j<customer;j++)//为每个顾客服务一遍
{
for( i=0;i<customer;i++)//找到可以服务的顾客
{

if(finish[i][p]=='k')continue;
if(leave[p]>=(need[i][p]-request[i][p]))
{
leave[p]+=need[i][p];
finish[i][p]='k';
if(p+1>=deleteto)
{
printf("this time give customer%d [%d] source%d and his source%d service finished.return %d to bank.\n",i+1,need[i][p]-request[i][p],p+2,p+2,need[i][p]);
}
else
{
printf("this time give customer%d [%d] source%d and his source%d service finished.return %d to bank.\n",i+1,need[i][p]-request[i][p],p+1,p+1,need[i][p]);
}
i=customer;//每次找到一个就停止
}

}
}//for
}
}
int main()
{

printf("please input the number of banker's source.\n");
scanf("%d",&sounumber);

for(i=0;i<sounumber;i++)
{
printf("please input the quantity of source%d \n",i+1);
scanf("%d",&source[i]);
}
for( i=0;i<=99;i++)
{
int r=0;
printf("please input customer's need for every source(no more than 100 people),continue with'0',end with'1'\n");
int m=0;
for(m=0;m<sounumber;m++)
{
scanf("%d",&inneed[m]);
}

scanf("%d",&control);
for(m=0;m<sounumber;m++)
{
if(inneed[m]>source[m])
{
printf("banker can not satisfy your need.\n");
}
else
{
need[i][m]=inneed[m];//第i个顾客的第m个需求为需求列inneed中的第m个
//customer++;
r=1;
}
}
if(r==1)customer++;
if(control==1)break;
}
printf("there are %d customers.\n",customer);

for( i=0;i<customer;i++)
{
printf("please input customer%d's first-request of every source.\n",i+1);
int q=0;//作用等同于前面的m
for(q=0;q<sounumber;q++)
{
scanf("%d",&request[i][q]);//第i个顾客对第q个资源的需求
requestsum[q]+=request[i][q];
if(request[i][q]>need[i][q]||requestsum[q]>source[q])
{
printf("can not satisfy,please input less.\n");
scanf("%d",&request[i][q]);
}
}


}
change();
service();
return 0;
}
0 0
原创粉丝点击