思路:
外部“上”按钮方法:
外部“下”按钮方法:
(需要的参数是电梯现在的状态(上升或者下降:如上升则等待响应下按钮事件,否则等待;下降同理……)、电梯所在的楼层和触发外部按钮的所在楼层)
内部:
数字按钮(实现输入楼层)
开门:到达楼层,则响应开门时间,否则等待到达再响应事件(实际情况不可能是走到两层之间,你按开门,他也开门,是吧)
关门:user输入楼层后,根据现在的位置判断上或者下的运行 *** 作(如果没有输入,就安静不“动”(实际坐电梯进去不按也是这样的吧))
为了简单,分配了1到9层.另外也没加入有几人同时按,不过那样也不难,再分配10个每层的变量,随机按.再处理,是先向上还是直接向下.不过那倒真的可以用到真的上了.#include <stdio.h>
#include <dos.h>
#include <ctype.h>
#define ON 1
#define OFF 0
main(){
int power,i
char oldkey,key
struct date today
struct time now
getdate(&today)/*把系统当前日期存入today所指向的date结构中*/
gettime(&now)
gotoxy(25,3)
printf("Today's date is %d-%d-%d\n",today.da_year,today.da_mon,today.da_day)
gotoxy(25,6)
printf("Current time is %02d:%02d:%02d\n",now.ti_hour,now.ti_min,now.ti_sec)
if( now.ti_hour>=8||(now.ti_hour<=23&&now.ti_min<59)){
power=ON
printf("\n\n\nLift power is on.Lift stop in 1 floor.")
}
else {
power=OFF
printf("Lift power is off.")
}
oldkey=0
while(power==ON) {
printf("\n\n\nFloor")
for(i=1i<10i++)
printf("\n %d",i)
printf("\nPress floor number or q to shut down power :")
if( now.ti_hour>=8||(now.ti_hour<=23&&now.ti_min<59))
power=ON
else
power=OFF
key=bioskey(0)
key=key&0x00ff
system("cls")
if(key=='0'){
power=OFF
printf("\n\n Lift Power off")
sleep(1)
break
}
if(key>oldkey)
printf(" %c Go up!...",24)
if(key<oldkey)
printf(" %c Go down!...",25)
if(key==oldkey)
printf(" %c ok you are here",22)
sleep(2)
system("cls")
printf("\n\n ")
switch(key){
case '1':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '2':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '3':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '4':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '5':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '6':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '7':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '8':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
case '9':
if(key==oldkey)
printf(" %c ok you are here",22)
else if(key>oldkey)
printf("%c Go up!...stop in %c floor.",24,key)
else
printf("%c Go down!...stop in %c floor.",25,key)
oldkey=key
break
}
}
getch()
}
按你的要求写了这个程序。试运行正常。
程序运行截图如下:
希望对你有帮助!
【百度知道】土堆上的石头
#include<stdio.h>
void main()
{
int n=10,set,end,i
printf("本电梯一共%d层。\n",n)
printf("请输入你所在的楼层:\n")
while(1)
{
scanf("%d",&set)
if(set>=1&&set<=n)
break
printf("没有第%d层,请重新输入:\n",set)
}
printf("你现在在第%d层。\n",set)
printf("请选择:\n1.上行\n2.下行\n")
while(1)
{
scanf("%d",&i)
if(i>=1&&i<=2)
break
printf("选择数字错误,请重新输入:\n")
}
if(i==1)
{
while(1)
{
printf("请输入你要去的楼层:\n")
scanf("%d",&end)
if(end>=1&&end<=n)
{
if(end>=set)
break
else
printf("此为上行,请输入较高层的层数。\n")
}
else
printf("没有第%d层,请重新输入:\n",end)
}
printf("电梯上行中……\n")
for(i=seti<=endi++)
printf("现在到了第%d层。\n",i)
printf("第%d层已到!\n",end)
}
if(i==2)
{
while(1)
{
printf("请输入你要去的楼层:\n")
scanf("%d",&end)
if(end>=1&&end<=n)
{
if(end<=set)
break
else
printf("此为下行,请输入较低层的层数。\n")
}
else
printf("没有第%d层,请重新输入:\n",end)
}
printf("电梯上行中……\n")
for(i=seti>=endi--)
printf("现在到了第%d层。\n",i)
printf("第%d层已到!\n",end)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)