写了一电梯,咋不运行,各位高人帮帮忙啊!!!!!!!!
#include<stdio.h>#include<stdlib.h>
#include<winsock.h>
#include<winbase.h>
#include"time.h"
#define free 0 //电梯处于空闲状态
#define up 1 //电梯处于上行状态
#define down 2 //电梯处于下行状态
#define stop 3 //电梯处于停止状态
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; //创建线程1,用来接收输入
HANDLE hRead1=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getInput,NULL,0,&ThreadID1);
DWORD ThreadID2=2; //创建线程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=up;//上行
else if(goalfloor!=0&&goalfloor<currentfloor)
state=down;//下行
//start=clock();
//while(k--);
else if(goalfloor!=0&&goalfloor==currentfloor)
state=stop;//停止
break;
case up: //上行
//time_count();
currentfloor++;//楼层+1
break;
case down: //下行
// time_count();
currentfloor--;//楼层-1
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; //m用来标记电梯运行方向
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);
}
}