?? 44.c
字號:
#include <stdio.h>
struct YH
{
int Allocation[5][3];
int Need[5][3];
int Available[3];
int G[3][3];
};
int u,B=0,A=0;
int Work[3];
int Request[3];
struct YH st;
void W(int i) //系統(tǒng)安全檢測函數(shù)
{int j;
for(j=0;j<=2;j++)
{
if(Request[j]<=st.Need[i][j])
A++;
}
if(A==3)
for(j=0;j<=2;j++)
{
if(Request[j]<=st.Available[j])
B++;
}
else {printf("不安全");}
if(B==3)
for(j=0;j<=2;j++)
{
st.Available[j]=st.Available[j]-Request[j];
st.Allocation[i][j]=st.Allocation[i][j]+Request[j];
st.Need[i][j]=st.Need[i][j]-Request[j];
}
else {printf("不安全");}
}
void F() //分配后安全檢測函數(shù)
{
int j,t,C=0,D=0,P[5];
for(j=0;j<=2;j++)
Work[j]=st.Available[j];
for(t=0;t<=4;t++)
P[t]=0;
Loop:
for(t=0;t<=4;t++)
{
for(j=0;j<=2;j++)
{
if(Work[j]>=st.Need[t][j]&&P[t]==0)
C++;
}
if(C==3)
{
P[t]=1;
D++;
for(j=0;j<=2;j++)
Work[j]=Work[j]+st.Allocation[t][j];
printf("%d ",t);
if(P[t]==1)
{
C=0;
goto Loop;
}
}
C=0;
}
if(D==5)
{ printf("安全");u=1;}
else
{printf("不安全");u=0;}
for(j=0;j<=2;j++)
Work[j]=st.Available[j];
for(t=0;t<=4;t++)
P[t]=0;
D=0;
t=0;
}
void Q(int a[],int b[])
{
int k;
printf("輸入Request的值\n");
for(k=0;k<=2;k++)
scanf("%d",&a[k]);
printf("i=?\n");
scanf("%d",&b[0]);
}
main()
{
int i, j,c[1];
printf("輸入Allocation的值\n");
for(i=0;i<=4;i++)
for(j=0;j<=2;j++)
scanf("%d",&st.Allocation[i][j]);
printf("輸入Need的值\n");
for(i=0;i<=4;i++)
for(j=0;j<=2;j++)
scanf("%d",&st.Need[i][j]);
printf("輸入Available的值\n");
for(j=0;j<=2;j++)
scanf("%d",&st.Available[j]);
F();
Loop1:
{
Q(Request,c);
if (Request[0]==0&&Request[1]==0&&Request[2]==0)
printf("測試結(jié)束");
else
{
switch(c[0])
{case 0:
W(0);
break;
case 1:
W(1);
break;
case 2:
W(2);
break;
case 3:
W(3);
break;
case 4:
W(4);
break;
}
if(B==3)
{F();}
A=0;
B=0;
if(u==1)
{
for(j=0;j<=2;j++)
{
st.G[0][j]=st.Allocation[c[0]][j] ;
st.G[1][j]=st.Need[c[0]][j] ;
st.G[2][j]=st.Available[j];
//printf("%d ",st.Available[j]);
}
// printf("%d",B);
//printf("%d",t);
goto Loop1;}
if(u==0)
{
for(j=0;j<=2;j++)
{
st.Allocation[c[0]][j]=st.G[0][j];
st.Need[c[0]][j]=st.G[1][j];
st.Available[j]=st.G[2][j];
}
goto Loop1;}
}}
scanf("%d",&i);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -