当前位置: 代码迷 >> C语言 >> 银行家算法
  详细解决方案

银行家算法

热度:934   发布时间:2008-06-22 12:02:03.0
银行家算法
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-22 04:02:03
StarWing83

来 自:湖北工业大学
等 级:贵宾
威 望:19
帖 子:3946
专家分:748
注 册:2007-11-16
  得分:0 
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http://yzfy.org **
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define N 3
#define M 3
#define F 1000
int issafe(int need1[N][M],int allo1[N][M],int max1[N][M],int avai1[M])
{
   
int need[N][M],allo[N][M],max[N][M],avai[M];
    int i,j,flag,m=0,p[M],z;

    for (i=0;i<N;i++)
    {
        
for (j=0;j<N;j++)
        {
            
need[i][j]=need1[i][j];
            allo[i][j]=allo1[i][j];
            max[i][j]=max1[i][j];
        }
        
avai[i]=avai1[i];
    }
   
while (m<N)
    {
        
z=m;
        for (i=0;i<N;i++)
        {
            
flag=1;
            for (j=0;j<M;j++)
            {
               
if (need[i][j]>avai[j])
                {
                    
flag=0;break;
                }
            }
            
if (flag==1)
            {

               
for (j=0;j<M;j++)
                {
                    
max[i][j]=F;

                    need[i][j]=F;
                }
               
for (j=0;j<N;j++) {avai[j]+=allo[i][j];}
               
p[m]=i;  printf("p:%d-->",p[m]);
                m++;
            }

        }
        
if (z==m) return 0;
    }
   
return 1;
}

void printinit(int max[N][M],int allo[N][M],int need[N][M],int avai[M])
{
   
int i,j;
    printf("\nThe max\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",max[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe allocation\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",allo[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe need\n");
    for (i=0;i<N;i++)
    {
        
printf("p%d: ",i);
        for (j=0;j<N;j++)
        {
            
printf("%4d",need[i][j]);

        }
        
printf("\n");
    }
   
printf("\nThe available\n    ");
    for (i=0;i<N;i++)
    {
        
printf("%4d",avai[i]);
    }

}
void init(int max[N][M],int allo[N][M],int need[N][M],int avai[M])
{
   
int i,j;
    printf("\nplease input the max!");
    for (i=0;i<N;i++)
    {
        
printf("\ninput the max of p:%d\n",i);
        for (j=0;j<M;j++)
            scanf("%d",&max[i][j]);
    }
   
printf("\nplease input the allocation!");
    for (i=0;i<N;i++)
    {
        
printf("\ninput the max of p:%d\n",i);
        for (j=0;j<M;j++)
            scanf("%d",&allo[i][j]);
    }
   
printf("\nplease input the available!\n");
    for (i=0;i<M;i++)
        scanf("%d",&avai[i]);
    for (i=0;i<N;i++)
        for (j=0;j<M;j++)
            need[i][j]=max[i][j]-allo[i][j];
}
int main()
{
   
int i,j,t,d[N];
    int y,e=1,repay=0;
    char r;
    int max[N][M]={{7,5,3},{3,2,2},{9,0,2}};
    int allo[N][M]={{0,1,0},{2,0,0},{3,0,2}};
    int need[N][M];
    int avai[M]={5,5,2};
    for (i=0;i<N;i++)
        for (j=0;j<M;j++)
            need[i][j]=max[i][j]-allo[i][j];
    printinit(max,allo,need,avai);
    printf("\nif you want to reset it please input R,or put other key to continue\n");
    r=getchar();
    if (r=='R'||r=='r')
        init(max,allo,need,avai);
    for (;;)
    {
        
printinit(max,allo,need,avai);
        printf("\nplese any key to check the now station\n");
        getchar();
        if (issafe(need,allo,max,avai))
        {
            
printf("\nnow the station is safe!\n");
            printf("\npealse input which pocess(0,1,2......N-1)\n");
            scanf("%d",&t);
            printf("pealse input the pocess want to apply resource\n");
            while (e==1)
            {
               
for (i=0;i<M;i++)
                    scanf("%d",&d[i]);
                for (i=0;i<M;i++)
                    if (d[i]>need[t][i])
                        {e=0;}
               
if (e==0)
                {
                    
printf("\nerror!\nthe apply is exceed the need!please reset!\n");
                    printf("the need:") ;
                    for (j=0;j<M;j++)
                        printf("%4d",need[t][j]) ;
                    printf("\nbe sure unexceed the need\npealse input the pocess want to apply resource\n");
                    e=1;
                }
               
else
                    
e=0;
            }
            
e=1;
            printf("is checking now.....");
            for (i=0;i<M;i++)
            {
               
need[t][i]-=d[i];
                allo[t][i]+=d[i];
                avai[i]-=d[i];
            }
            
if (issafe(need,allo,max,avai))
            {
               
for (i=0;i<M;i++)

                    if (need[t][i]!=0)
                        {repay=1;break;}

               
if (repay==0)
                    for (i=0;i<M;i++)
                    {
                        
avai[i]+=max[t][i];
                        max[t][i]=0;
                        allo[t][i]=0;
                    }


               
printf("\nsuccess!\n");
            }

            
else
            
{
               
printf("\nfail\n");
                for (i=0;i<M;i++)
                {
                    
need[t][i]+=d[i];
                    allo[t][i]-=d[i];
                    avai[i]+=d[i];
                }
            }
        }
        
else
            
printf("\nnow the station is unsafe!\n");


        printf("\nIf you want to continue,please input 1\n");
        printf("If you want to exit,please input 0\ncontinue or not?[1/0]");
        scanf("%d",&y);
        if (y==0)
            break;
    }
   
return 0;
}


编译出N个错误,已经解决了。
----------------解决方案--------------------------------------------------------
  相关解决方案