我写了一模拟电梯程序,咋运行不了,各位帮帮 忙
#include<stdio.h>#include<stdlib.h>
#include<winsock.h>
#include<winbase.h>
#include"time.h"
#define free 0
#define RUN 1
#define up 2
#define down 3
#define stop 4
void getInput(void);
void output(void);
void print_message(void);
void Control(void);
void state_trans(void);
void time_count(void);
char ch;
int callin[10],callup[10],calldown[10],goalfloor=0,currentfloor=1,state=free;
clock_t start,finish;
double duration;
main()
{
DWORD ThreadID1=1;
HANDLE hRead1=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getInput,NULL,0,&ThreadID1);
DWORD ThreadID2=2;
HANDLE hRead2=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)output,NULL,0,&ThreadID2);
int i;
for(i=0;i<=9;i++)
{
callin[i]=0;
callup[i]=0;
calldown[i]=0;
}
while(1)
{
state_trans();
print_message();
Control();
// time_count();
}
system("pause");
return 0;
}
void getInput(void)
{
while(1)
{
printf("please enter call\n");
ch=getchar();
fflush(stdin);
if(ch=='1')
callin[1]=1;
else if(ch=='2')
callin[2]=1;
else if(ch=='3')
callin[3]=1;
else if(ch=='4')
callin[4]=1;
else if(ch=='5')
callin[5]=1;
else if(ch=='6')
callin[6]=1;
else if(ch=='7')
callin[7]=1;
else if(ch=='8')
callin[8]=1;
else if(ch=='9')
callin[9]=1;
else if(ch=='I')
callup[1]=1;
else if(ch=='U')
callup[2]=1;
else if(ch=='Y')
callup[3]=1;
else if(ch=='T')
callup[4]=1;
else if(ch=='R')
callup[5]=1;
else if(ch=='E')
callup[6]=1;
else if(ch=='W')
callup[7]=1;
else if(ch=='Q')
callup[8]=1;
else if(ch=='A')
calldown[9]=1;
else if(ch=='S')
calldown[8]=1;
else if(ch=='D')
calldown[7]=1;
else if(ch=='F')
calldown[6]=1;
else if(ch=='G')
calldown[5]=1;
else if(ch=='H')
calldown[4]=1;
else if(ch=='J')
calldown[3]=1;
else if(ch=='K')
calldown[2]=1;
else
printf("输入要求无效\n");
time_count();
}
}
void output(void)
{
long int i;
FILE *cfptr=NULL;
if((cfptr=fopen("date.txt","w"))==NULL)
printf("can't open file\n");
else
while(1)
{
for(i=1;i<=2222222;i++);
fputc(ch,cfptr);
fflush(cfptr);
}
}
void state_trans(void)
{
int i;
long k=10000000l;
switch(state)
{
case free:
for(i=1;i<=9&&callin[i]==0&&callup[i]==0&&calldown[i]==0;i++);
if(callin[i]!=0||callup[i]!=0||calldown[i]!=0)
goalfloor=i;
if(goalfloor!=0&&goalfloor!=currentfloor)
{
state=RUN;
start=clock();
while(k--);
}
if(goalfloor!=0&&goalfloor==currentfloor)
state=stop;
break;
case RUN:
if(goalfloor>currentfloor)
state=up;
if(goalfloor<currentfloor)
state=down;
break;
case up:
time_count();
currentfloor++;
break;
case down:
time_count();
currentfloor--;
break;
case stop:
finish=clock();
duration=(double)(finish-start);
printf("%f seconds\n",duration);
printf("当前停靠楼层是%d\n",currentfloor);
break;
}
}
void Control(void)
{
int m,i;
if(state==up)
{
m=1;
for(i=currentfloor;i<goalfloor;i++)
if(callup[i]==1||callin[i]==1)
{goalfloor=i;
break;
}
}
if(state==down)
{
m=-1;
for(i=currentfloor;i>goalfloor;i--)
if(calldown[i]==1||callin[i]==1)
{goalfloor=i;
break;
}
}
if(state==stop)
{
callin[currentfloor]=0;
if(m==1)
{
callup[currentfloor]=0;
for(i=currentfloor+1;i<=9&&callin[i]==0&&callup[i]==0;i++);
if(callin[i]==1||callup[i]==1)
goalfloor=i;
else
{
for(i=9;i>=1&&calldown[i]==0&&callin[i]==0;i--);
if(calldown[i]==1||callin[i]==1)
goalfloor=i;
else
{
for(i=1;i<=8&&callup[i]==0;i++);
if(callup[i]==1)
goalfloor=i;
else
state=free;
}
}
}
if(m==-1)
{
calldown[currentfloor]=0;
for(i=currentfloor-1;i>=1&&calldown[i]==0&&callin[i]==0;i--);
if(calldown[i]==1||callin[i]==1)
goalfloor=i;
else
{
for(i=1;i<=9&&callin[i]==0&&callup[i]==0;i++);
if(callin[i]==1||callup[i]==1)
goalfloor=i;
else
{
for(i=9;i>=1&&calldown[i]==0;i--);
if(calldown[i]==1)
goalfloor=i;
else
state=free;
}
}
}
}
}
void print_message(void)
{
switch(state)
{
case free:
printf("电梯处于空闲状态\n");
break;
case up:
printf("电梯处于上行状态\n");
break;
case down:
printf("电梯处于下行状态\n");
break;
}
}
void time_count(void)
{
clock_t start1,finish1;
double duration1;
start1=clock();
duration1=0.0;
while(duration1<5.0)
{
finish1=clock();
duration1=(double)(finish1-start1);
}
}