各位高手帮分析分析,这一问题我搞了两天也弄不出来!解析一下!
#define count 10#define lie 14
#include "time.h"
#include "stdio.h"
#include "conio.h"
int k;
int a[count];
int hour,mini;
int s;
int pdone;
int b[180];
int r=0;
int ptim1=0,pttim1=0;
int ptim2=0,pttim2=0;
int pover1=1,pover2=1;
int p;
int vtim1=0,vttim1=0;
int vtim2=0,vttim2=0;
int vover1=1,vover2=1;
int q=0;
int p1_ok=0,v1_ok=0;
int p2_ok=0,v2_ok=0;
void tim()
{window(lie,3,lie+8,3);
textbackground(5);
clrscr();
printf(" 0%d:",hour);
if(mini<10)
printf("0");
printf("%d\n",mini);
if(mini==0)
{
hour--;
mini=60;
}
mini--;
}
void num0_1()
{int i;
i=rand()%10;
if(a[i]==1)
num0_1();
a[i]=1;
}
void num1_0()
{int i;
i=rand()%10;
if(a[i]==0)
num1_0();
a[i]=0;
}
void pshow(ptim,pttim,x)
int ptim,pttim,x;
{window(lie,2*x+5,ptim+lie-1,2*x+5);
textbackground(7);
clrscr();
window(lie,2*x+5,pttim+lie-1,2*x+5);
if(pdone==1)
textbackground(7);
else
textbackground(2);
clrscr();
pdone=0;
printf("%3d/%d\n",pttim,ptim);
}
void sheng(pover,ptim,pttim,x)
int pover,ptim,pttim,x;
{if(pover==1)
{if(ptim==pttim)
{ptim=rand()%4+5;
pttim=0;
}
pttim++;
}
if(pttim==ptim)
{if(k!=count)
{if(p==count) p=0;
/*a[p]=1;*/
num0_1();
k++;
p++;
pover=1;
pdone=1;
}
else
pover=0;
}
pshow(ptim,pttim,x);
if(s==0)
{ptim1=ptim;
pttim1=pttim;
pover1=pover;
}
else
{ptim2=ptim;
pttim2=pttim;
pover2=pover;
}
}
void vshow(vtim,vttim,x)
int vtim,vttim,x;
{window(lie,2*x+5,vtim+lie-1,2*x+5);
textbackground(7);
clrscr();
if(vttim!=0)
{window(lie,2*x+5,lie+vttim-1,2*x+5);
textbackground(4);
clrscr();
}
printf("%3d/%d\n",vttim,vtim);
}
void xiao(vover,vtim,vttim,x)
int vover,vtim,vttim,x;
{
if(vttim==0)
{if(k!=0)
{/*a[q]=0;*/
num1_0();
q++;
k--;
if(q==count)
q=0;
vover=1;
}
else
{vover=0;
vshow(vtim,vttim,x);
}
}
if(vover==1)
{if(vttim==0)
{vtim=rand()%4+5;
vttim=vtim;
}
vttim--;
vshow(vtim,vttim,x);
}
if(s==2)
{vtim1=vtim;
vttim1=vttim;
vover1=vover;
}
else
{vtim2=vtim;
vttim2=vttim;
vover2=vover;
}
}
void memshow()
{
int i;
for(i=0;i<count;i++)
{window(lie+4*i,13,lie+4*i+1,13);
if(a[i]==1)
textbackground(2);
else
textbackground(4);
clrscr();
}
}
void cputime()
{int i;
int m,h;
for(i=0;i<r;i++)
{m=i/30;
h=m*60;
window(lie+2*i-h,15+m,lie+2*i+2-h,15+m);
switch(b[i])
{case 0: {textbackground(2);
clrscr();
cprintf("P1");
break;
}
case 1: {textbackground(2);
clrscr();
cprintf("P2");
break;
}
case 2: {textbackground(4);
clrscr();
cprintf("C1");
break;
}
case 3: {textbackground(4);
clrscr();
cprintf("C2");
break;
}
}
}
}
main()
{
int i;
time_t t;
srand((unsigned) time(&t));
hour=3;
mini=0;
for(i=0;i<count;i++)
a[i]=0;
k=rand()%(count+1);
p=k;
for(i=0;i<k;i++)
/*a[i]=1;*/
num0_1();
while(1)
{window(1,1,80,25);
textbackground(1);
clrscr();
textcolor(YELLOW);
clrscr();
printf("\n\n Now-Time:\n\n");
printf(" Producer 1:\n\n");
printf(" Producer 2:\n\n");
printf(" Consumer 1:\n\n");
printf(" Consumer 2:\n\n");
printf(" Buffer:\n\n");
printf(" CPU-Time:\n\n");
if(hour==0&&mini==0)
{window(1,1,80,25);
textbackground(1);
clrscr();
cprintf("\n\n\n\n\n\n\n\n\n\n\n\n Press any key to exit!");
getch();
return;
}
tim();
s=rand()%4;
b[r]=s;
r++;
switch(s)
{
case 0:{p1_ok=1;
sheng(pover1,ptim1,pttim1,0);
if(p2_ok==1)
{if(ptim2==pttim2)
pdone=1;
pshow(ptim2,pttim2,1);
}
if(v1_ok==1)
vshow(vtim1,vttim1,2);
if(v2_ok==1)
vshow(vtim2,vttim2,3);
break;
}
case 1:{if(p1_ok==1)
{if(ptim1==pttim1)
pdone=1;
pshow(ptim1,pttim1,0);
}
p2_ok=1;
sheng(pover2,ptim2,pttim2,1);
if(v1_ok==1)
vshow(vtim1,vttim1,2);
if(v2_ok==1)
vshow(vtim2,vttim2,3);
break;
}
case 2:{if(p1_ok==1)
{if(ptim1==pttim1)
pdone=1;
pshow(ptim1,pttim1,0);
}
if(p2_ok==1)
{if(ptim2==pttim2)
pdone=1;
pshow(ptim2,pttim2,1);
}
v1_ok=1;
xiao(vover1,vtim1,vttim1,2);
if(v2_ok==1)
vshow(vtim2,vttim2,3);
break;
}
case 3:{if(p1_ok==1)
{if(ptim1==pttim1)
pdone=1;
pshow(ptim1,pttim1,0);
}
if(p2_ok==1)
{if(ptim2==pttim2)
pdone=1;
pshow(ptim2,pttim2,1);
}
if(v1_ok==1)
vshow(vtim1,vttim1,2);
v2_ok=1;
xiao(vover2,vtim2,vttim2,3);
break;
}
}
memshow();
cputime();
sleep(1);
}
}