銀行家算法的數組方式實現

 2023-12-06 阅读 29 评论 0

摘要:#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種資源各自
#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;
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/188719.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息