printf("上下选择:1-上楼,2-下楼\t")
scanf("%d",&input_direction)
fflush(stdin)
printf("输入该层的乘客想到达的楼层,以-1结束\n")
void check(liftor &myliftor) int flag1=0,flag2=0,flag3=0
for(i=0i<10i++)
{
if(myliftor.stored_flare[i]!=0)
flag1=1
if(myliftor.stored_flare[i]==1)
flag2=1
if(myliftor.stored_flare[i]==2)
flag3=1
}
if(!flag1) myliftor.set_direction(0)
else if(!flag2&&flag3) myliftor.set_direction(2)
else if(!flag3&&flag2) myliftor.set_direction(1)
int main(int argc, char *argv[])
liftor myliftor
char flag
int j
myliftor.direction=1
printf("**************************************\n")
printf("该程序用于模拟10(0-9)层的电梯控桐渗制\n")
printf("先输入按下电梯的层号及上下选\n择(类似电梯外的按钮)\n")
printf("每到一层时,输入乘客要到的楼槐宴\n层号(类铅轮银似电梯内的按钮)\n")
/*建立一座两层楼,一部谈陵电梯的模拟世侍谨程序。为简化起见,每部电梯限乘一人,电梯每天在一楼关门等待模拟程序包括一个时钟,每天从零开始。模拟程序得调度器组件随机设置每一层第一个人到来的时间,当时钟的时间等于第一个人到来的时间时,模拟程序生成一个新到的人将该人放到这一层。然后这个人按下按钮,请求电梯开门。这个人的目的地楼层不能与他上电梯的楼层相同。
如果第一个人到达第一层,则可以在按下按钮、等待电梯开门之后立即进入电梯。如果第一个人在第2层,则电梯要升到第2层去接他。电梯从1层移到2层需要5秒。
电梯到达一层时,打开电梯门上的灯,并在搜基电梯内发出铃声,该层的按钮和电梯中表示该层的按钮复位,电梯门打开,乘客走出电梯(如果有到该层的乘客)。另一乘客(如果该层有人等待)进入电梯按下目的层按钮,电梯们关上。电梯移动前确定移动方向(两层很容易判断),为简单起见,电梯到达一层直到关门所花时间为0。
任何时间每层最多只能有一人等待,如果新到的人(不在电梯中)要到达该层时该层被占用,则一秒后才能安排新到达者。假设每隔5到20秒人们随机到达每层*/
#include <iostream >
#include <ctime >
#include <iomanip >
#include <string >
#include <stdlib.h>
#include <windows.h >
using namespace std
class person
{
public:
string sex//定义人员类
person()
void newset()
}
person::person()
{sex="man" }
void person::newset()
{
int i
srand((unsigned)time(NULL))
for(i=0i++)
{
if(rand()==1)
{sex="man" break }
if(rand()==2)
{sex="woman" break }
}
}
class elevator
{
public:
int now //定义电梯类
elevator()
}
elevator::elevator()
{now=1 }
class Time
{
public:
static void outtime()//定义时间类,用于输出电梯每次运行时的时间
{
SYSTEMTIME t
GetLocalTime(&t)
cout<<" //"<<t.wYear<<"."<<t.wMonth<<"."<<t.wDay<<". "<<t.wHour<<":"<<t.wMinute<<":"<<t.wSecond<<endl
}
}
void last(long n)//用于等待时输出.的函数
{
long i
i=n/500
for(i=0i<n/500i++)
{
cout<<'.' Sleep(500)
}
cout<<endl
}
void waiting()
{long m //模拟随机等待的时间函数
srand((unsigned)time(NULL))
cout<<"elevator waiting"
for(int i=0i<1000i++)
m=rand()%(20000-5000+1)+5000
last(m)
}
void output(long t,person &p,elevator &e) //电梯模拟函数
{
static n=1 //定义乘客的个数
if( t==1)//有人在一楼按铃
{
p.newset()
if(e.now!=1) //但是此时电梯停留在二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrived floor 1."//输出有人到达一楼
Time::outtime() //输出此时的准确时间
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator." //输出乘客进入电梯
Time::outtime() //输出此时的准确时间
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2." //输入乘客到达二楼
Time::outtime() //输出此时的准确时间
e.now=2
n++
system("color 0C") waiting() system("color 0F")
}
else//此时电梯就是一楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1." //输出有人到达一楼
Time::outtime() //输出此时的准确时间
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."<<endl
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2."
Time::outtime()
e.now=2
n++
system("color 0C") waiting() system("color 0F")
}
}
if(t==2)//有人在二楼按铃
{
if(e.now!=2)//但是电梯此时不在二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrived floor 2."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1."
Time::outtime()
e.now=1
n++
system("color 0C") waiting() system("color 0F")
}
else//此时电梯就是二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2."
Time::outtime()
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1."
Time::outtime()
e.now=1
n++
system("color 0C") waiting() system("color 0F")
}
}
}
int main()
{
cout.fill('*')
cout<<setw(80)<<'*'
cout.fill(' ')
system("color 0C")
cout<<setw(50)<<"***欢迎使用电梯模拟程序***"<<endl //输出头菜单
cout.fill('*')
cout<<setw(80)<<'*'
cout.fill(' ')
int i
person p
elevator e
srand((unsigned)time(NULL)) //定义随机数种
waiting()
system("color 0F")
for(i=0i++)
output(rand(),p,e)//调用电梯模拟函数
return 0
}
为了简单,分配了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()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)